private async void btnDownload_Click(object sender, EventArgs e) { string email = txtEMail.Text; string password = txtPassword.Text; //Task<int> response = await WebOps.CheckCredentials(email, password); int response = await WebOps.CheckCredentials(email, password); switch (response) { case 1: MessageBox.Show("You must enter an email.", "MWO Credential Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; case 2: MessageBox.Show("You must enter a password.", "MWO Credential Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; case 3: MessageBox.Show("Your MWO Credentials are invalid." + Environment.NewLine + "Check your eMail and Password and try again.", "MWO Credential Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; case 4: break; case 5: MessageBox.Show("There has been an error while trying to connect." + Environment.NewLine + "Check your connection and try again.", "MWO Credential Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; } int tempInt = 0; int season = int.TryParse(cmbSeasonNumber.Text, out tempInt) ? tempInt : ConfigFile.SEASON_LAST; if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password)) { if (string.IsNullOrWhiteSpace(email)) { Mex.AddMessage("Email field is not valid.", Mex.ERROR); } if (string.IsNullOrWhiteSpace(password)) { Mex.AddMessage("Password field is not valid.", Mex.ERROR); } Mex.AddMessage("Will not proceed.", Mex.WARNING); Mex.PrintErrorMessagesInForm(); Mex.RemoveAll(); return; } // saves email & password to a local file if (ckbSaveCredentials.Checked) { if (!new FileInfo(ConfigFile.LOCAL_CREDENTIALS).Exists) { File.WriteAllLines(ConfigFile.LOCAL_CREDENTIALS, new string[] { email, password }); } else { try { File.Delete(ConfigFile.LOCAL_CREDENTIALS); } catch { } File.WriteAllLines(ConfigFile.LOCAL_CREDENTIALS, new string[] { email, password }); } } DownloadData baseData = new DownloadData(user: email, password: password, season: season); if (chbGeneral.Checked) { int startGeneral; int endGeneral; if (chbFullGeneral.Checked) { startGeneral = (int)ConfigFile.MIN_PAGES - 1; endGeneral = ConfigFile.MAX_PAGES; } else { startGeneral = int.TryParse(txtStartGeneral.Text, out tempInt) ? tempInt - 1 : (int)ConfigFile.MIN_PAGES; endGeneral = int.TryParse(txtEndGeneral.Text, out tempInt) ? tempInt - 1 : ConfigFile.MAX_PAGES; } WebOps.LoginAndDownload(dData: baseData, type: 0, startPage: startGeneral, finishPage: endGeneral, bar: prbPageProgressGeneral, writeDB: true); } if (chbLight.Checked) { int startLight; int endLight; if (chbFullLight.Checked) { startLight = (int)ConfigFile.MIN_PAGES - 1; endLight = ConfigFile.MAX_PAGES; } else { startLight = int.TryParse(txtStartLight.Text, out tempInt) ? tempInt - 1: (int)ConfigFile.MIN_PAGES; endLight = int.TryParse(txtEndLight.Text, out tempInt) ? tempInt - 1 : ConfigFile.MAX_PAGES; } WebOps.LoginAndDownload(dData: baseData, type: 1, startPage: startLight, finishPage: endLight, bar: prbPageProgressLight); } if (chbMedium.Checked) { int startMedium; int endMedium; if (chbFullMedium.Checked) { startMedium = (int)ConfigFile.MIN_PAGES - 1; endMedium = ConfigFile.MAX_PAGES; } else { startMedium = int.TryParse(txtStartMedium.Text, out tempInt) ? tempInt - 1: (int)ConfigFile.MIN_PAGES; endMedium = int.TryParse(txtEndMedium.Text, out tempInt) ? tempInt - 1 : ConfigFile.MAX_PAGES; } WebOps.LoginAndDownload(dData: baseData, type: 2, startPage: startMedium, finishPage: endMedium, bar: prbPageProgressMedium); } if (chbHeavy.Checked) { int startHeavy; int endHeavy; if (chbFullHeavy.Checked) { startHeavy = (int)ConfigFile.MIN_PAGES - 1; endHeavy = ConfigFile.MAX_PAGES; } else { startHeavy = int.TryParse(txtStartHeavy.Text, out tempInt) ? tempInt - 1: (int)ConfigFile.MIN_PAGES; endHeavy = int.TryParse(txtEndHeavy.Text, out tempInt) ? tempInt - 1 : ConfigFile.MAX_PAGES; } WebOps.LoginAndDownload(dData: baseData, type: 3, startPage: startHeavy, finishPage: endHeavy, bar: prbPageProgressHeavy); } if (chbAssault.Checked) { int startAssault; int endAssault; if (chbFullAssault.Checked) { startAssault = (int)ConfigFile.MIN_PAGES - 1; endAssault = ConfigFile.MAX_PAGES; } else { startAssault = int.TryParse(txtStartAssault.Text, out tempInt) ? tempInt - 1: (int)ConfigFile.MIN_PAGES; endAssault = int.TryParse(txtEndAssault.Text, out tempInt) ? tempInt - 1 : ConfigFile.MAX_PAGES; } WebOps.LoginAndDownload(dData: baseData, type: 4, startPage: startAssault, finishPage: endAssault, bar: prbPageProgressAssault); } }
public static bool LoadConfig() { try { ADDRESS = ConfigurationSettings.AppSettings["Address Start"]; int tempInt = 0; double tempDouble = 2.0; START_PAGE = int.TryParse(ConfigurationSettings.AppSettings["Start Page"], out tempInt) ? tempInt : 0; END_PAGE = int.TryParse(ConfigurationSettings.AppSettings["End Page"], out tempInt) ? tempInt : 0; SEPARATOR = ConfigurationSettings.AppSettings["Separator"]; SEASON_FIRST = int.TryParse(ConfigurationSettings.AppSettings["First Season"], out tempInt) ? tempInt : 1; SEASON_LAST = int.TryParse(ConfigurationSettings.AppSettings["Last Season"], out tempInt) ? tempInt : 7; MAX_PAGES = int.TryParse(ConfigurationSettings.AppSettings["Default Max Page"], out tempInt) ? tempInt : 4000; MIN_PAGES = int.TryParse(ConfigurationSettings.AppSettings["Default Min Page"], out tempInt) ? tempInt : 0; string directory = ConfigurationSettings.AppSettings["Output Folder"]; DirectoryInfo dirInfo = new DirectoryInfo(directory); if (!dirInfo.Exists) { Mex.AddMessage("'Output Folder' (in the configuration file) doesn't exist. Using the application folder instead.", Mex.ERROR); //// string output = Path.Combine(APP_PATH, "Output"); DirectoryInfo tempInfo = Directory.CreateDirectory(output); if (tempInfo != null) { DIRECTORY_OUTPUT = tempInfo; FILE_OUTPUT = ConfigurationSettings.AppSettings["Output File"]; } else { return(false); } } else { DIRECTORY_OUTPUT = dirInfo; FILE_OUTPUT = Path.Combine(DIRECTORY_OUTPUT.FullName, ConfigurationSettings.AppSettings["Output File"]); } GENERAL = false; LIGHT = false; MEDIUM = false; HEAVY = false; ASSAULT = false; EXPECTED_TIME = double.TryParse(ConfigurationSettings.AppSettings["Expected download time per page"], out tempDouble) ? tempDouble : 2.0; // trims all Players Names in list ACTUAL_LIST_NAME.ToList().ForEach(x => x = x.Trim()); return(true); } catch (Exception exp) { Logger.PrintLC("Failed to load Settings from the configuration file.\nLoading the default settings instead."); ADDRESS = ""; START_PAGE = 1; END_PAGE = 200; SEASON_FIRST = 1; SEASON_LAST = 7; MAX_PAGES = 1700; MIN_PAGES = 0; GENERAL = false; LIGHT = false; MEDIUM = false; HEAVY = false; ASSAULT = false; EXPECTED_TIME = 2.0; return(false); } }
/// <summary> /// Downloads all data inside a range determined by /// parameters. /// </summary> /// <param name="season"></param> /// <param name="type"></param> /// <param name="email"></param> /// <param name="password"></param> /// <param name="startPage"></param> /// <param name="finishPage"></param> /// <param name="taskNumber"></param> /// <param name="dData"></param> /// <param name="bar"></param> /// <param name="writeDB"></param> public static async void LoginAndDownload(int?season = null, int?type = null, string email = null, string password = null, int?startPage = null, int?finishPage = null, int?taskNumber = null, DownloadData dData = null, ProgressBar bar = null, bool writeDB = false) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; ConfigFile.IncrementTaskStarted(); if (dData != null) { if (dData.Season != null && season == null) { season = dData.Season; } if (dData.Type != null && type == null) { type = dData.Type; } if (dData.User != null && email == null) { email = dData.User; } if (dData.Password != null && password == null) { password = dData.Password; } if (dData.StartPage != null && startPage == null) { startPage = dData.StartPage; } if (dData.EndPage != null && finishPage == null) { finishPage = dData.EndPage; } if (dData.TaskNumber != null && taskNumber == null) { taskNumber = dData.TaskNumber; } } if (string.IsNullOrWhiteSpace(email)) { email = ConfigFile.DEFAULT_USER; } if (string.IsNullOrWhiteSpace(password)) { password = ConfigFile.DEFAULT_PASS; } if (season == null) { season = 1; } if (type == null) { type = 0; } if (startPage == null) { startPage = ConfigFile.MIN_PAGES; } if (finishPage == null) { finishPage = ConfigFile.MAX_PAGES; } if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password)) { if (string.IsNullOrWhiteSpace(email)) { Mex.AddMessage("Task " + (taskNumber != null ? taskNumber : 0) + " is missing the user email. Skipping", Mex.ERROR); } if (string.IsNullOrWhiteSpace(password)) { Mex.AddMessage("Task " + (taskNumber != null ? taskNumber : 0) + " is missing the user password. Skipping", Mex.ERROR); } Mex.PrintErrorMessagesInForm(); Mex.RemoveAll(); return; } // END VALIDATION SECTION string typeStr = null; switch (type) { case 0: typeStr = "GENERAL"; break; case 1: typeStr = "LIGHT"; break; case 2: typeStr = "MEDIUM"; break; case 3: typeStr = "HEAVY"; break; case 4: typeStr = "ASSAULT"; break; } string fileName; if (season < 10) { fileName = "S0" + season + "_" + typeStr + ".txt"; } else { fileName = "S" + season + "_" + typeStr + ".txt"; } //string DirDestination = @"C:\TEST\Output\"; string DirDestination = ConfigFile.DIRECTORY_OUTPUT.FullName; FileInfo fileOutput = new FileInfo(Path.Combine(DirDestination, fileName)); FileOps.CheckFile(fileOutput); season = season - 1; //adjust to 'base 0' web request finishPage += 1; //adjust to include last page string BaseAddress = "https://mwomercs.com/do/login"; var cookieContainer = new CookieContainer(); Uri uri = new Uri("https://mwomercs.com/profile/leaderboards"); var handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer; handler.CookieContainer.Add(uri, new System.Net.Cookie("leaderboard_season", season.ToString())); using (var client = new HttpClient(handler) { BaseAddress = new Uri(BaseAddress) }) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); HttpResponseMessage risposta = await client.PostAsync(BaseAddress, new FormUrlEncodedContent( new[] { new KeyValuePair <string, string> ("email", email), new KeyValuePair <string, string> ("password", password) }) ); string responseBodyAsText = await risposta.Content.ReadAsStringAsync(); Logger.PrintF(new FileInfo(Path.Combine(DirDestination, fileName)).FullName, "** STARTING DOWNLOAD", true); string ownRank = "<tr class=\"yourRankRow\">"; string endPages = "<td colspan='10'>No results found"; string resp = null; int lastPage = 0; int startPageReal = (startPage == null ? 0 : (int)startPage); int finishPageReal = (finishPage == null ? 10000 : (int)finishPage); bar.Maximum = finishPageReal - startPageReal; bar.Value = 0; for (int page = startPageReal; page < finishPageReal; page++) { risposta = await client.GetAsync("https://mwomercs.com/profile/leaderboards?page=" + page.ToString() + "&type=" + type.ToString()); responseBodyAsText = await risposta.Content.ReadAsStringAsync(); resp = DataOps.ParseHTML(responseBodyAsText); if (resp.Contains(ownRank)) { resp = resp.Replace(ownRank, string.Empty); } if (resp.Contains(endPages)) { lastPage = page; resp = resp.Replace(endPages, string.Empty); Logger.PrintF(Path.Combine(DirDestination, fileName), resp, false); if (bar != null) { bar.Value = bar.Maximum; } break; } Logger.PrintF(Path.Combine(DirDestination, fileName), resp, false); if (bar != null) { bar.Value++; } } Logger.PrintF(Path.Combine(DirDestination, fileName), "** FINISH DOWNLOADING", true); } if (writeDB) { //DataOps.WriteToDB(new FileInfo(Path.Combine(DirDestination, fileName).ToString()), (int)season + 1, (int)type); } ConfigFile.IncrementTaskFinished(); }