public API(AccountInfo credentials, string gradesUrl) { Credentials = credentials; if (gradesUrl.Contains("//"))//protocol is already set { GradesUrl = gradesUrl; } else// add http:// { GradesUrl = "http://" + gradesUrl; } #region setting absence dates #region start + enddate setting DateTime startDate; DateTime endDate; DateTime today = DateTime.Now; if (today.Month == 1 || (today.Month >= 9 && today.Month <= 12))//first semester { startDate = new DateTime(today.Year, 9, 1); endDate = new DateTime(today.AddYears(1).Year, 1, 31); } else//second semester or holiday { startDate = new DateTime(today.Year, 2, 1); endDate = new DateTime(today.Year, 6, 30); } #endregion // ATTENTON: DO NOT TOUCH! //go through all months between start and end date for (DateTime helperDate = startDate; helperDate < endDate; helperDate = helperDate.AddMonths(1)) { //handling what year to set int year; if (helperDate.Month == 1)//working on Jan { if (today.Month == 1)//this year year = today.Year; else//last year year = today.Year + 1; } else { if (today.Month == 1) year = today.Year - 1; else year = today.Year; } DateTime added = new DateTime(year, helperDate.Month, 1); _absenceMonths.Add(added); } #endregion }
/// <summary> /// logs in with any credentials, current won't be overwritten /// </summary> /// <param name="credentials">credentials to use</param> /// <returns>whether the user is valid</returns> public bool Login(AccountInfo credentials) { if (isDisposed) throw new ObjectDisposedException("API"); if (string.IsNullOrWhiteSpace(credentials?.Password) || string.IsNullOrWhiteSpace(credentials.Username)) throw new ArgumentException("credentials cannot be empty"); Cookie logid = new Cookie("baklogid", CombineUrl("login.aspx"), "/", gradesUrlWthProtocol); _client.CookieContainer.Add(logid); string docT = _client.GETRequest(CombineUrl("login.aspx")); string usernameName = ((CQ)docT)["#hlavni > div.loginmain > div:nth-child(4) > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td > input"][0]["name"]; string passwordName = ((CQ)docT)["#cphmain_TextBoxHeslo > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > input:nth-child(1)"][0]["name"]; var loginVals = Client.GetFieldsFromHtml(docT); loginVals.Set(usernameName, credentials.Username); loginVals.Set(passwordName, credentials.Password); _client.POSTRequest(CombineUrl("login.aspx"), loginVals); string html = _client.GETRequest(CombineUrl("uvod.aspx")); return !html.Contains("nepřihlášen"); }