diff --git a/.Net Core/MSRewards/Constants.cs b/.Net Core/MSRewards/Constants.cs index d8605b4..635ede4 100644 --- a/.Net Core/MSRewards/Constants.cs +++ b/.Net Core/MSRewards/Constants.cs @@ -16,6 +16,8 @@ public static class Constants public const string ID_P = "id_p"; + public const string ID_A = "id_a"; + public const string B_Results = "b_results"; public const string H2 = "h2"; diff --git a/.Net Core/MSRewards/MSRewards.csproj b/.Net Core/MSRewards/MSRewards.csproj index c7b1efb..ac5f40d 100644 --- a/.Net Core/MSRewards/MSRewards.csproj +++ b/.Net Core/MSRewards/MSRewards.csproj @@ -8,10 +8,10 @@ - + - - - + + + \ No newline at end of file diff --git a/.Net Core/MSRewards/PointStatus.cs b/.Net Core/MSRewards/PointStatus.cs new file mode 100644 index 0000000..6b399b8 --- /dev/null +++ b/.Net Core/MSRewards/PointStatus.cs @@ -0,0 +1,15 @@ +namespace MSRewards +{ + public class PointStatus + { + public int Current { get; set; } + + public int Maximum { get; set; } + + public PointStatus(int current, int max) + { + this.Current = current; + this.Maximum = max; + } + } +} \ No newline at end of file diff --git a/.Net Core/MSRewards/Program.cs b/.Net Core/MSRewards/Program.cs index a20c5ab..64906dd 100644 --- a/.Net Core/MSRewards/Program.cs +++ b/.Net Core/MSRewards/Program.cs @@ -23,7 +23,7 @@ private static async Task RunOptions(Options opts) bool useFirefox = opts.Firefox; var program = new Program(); - await program.Run(useFirefox); + await program.RunAsync(useFirefox); } private static void HandleParseError(IEnumerable errs) @@ -41,18 +41,17 @@ private static async Task Main(string[] args) { var result = Parser.Default.ParseArguments(args); await result.WithParsedAsync(async (x) => await RunOptions(x)); - result - .WithNotParsed(HandleParseError); + result.WithNotParsed(HandleParseError); } - private T DownloadJsonData(string url) where T : new() + private async Task DownloadJsonDataAsync(string url) where T : new() { using (var w = new WebClient()) { var json_data = string.Empty; try { - json_data = w.DownloadString(url); + json_data = await w.DownloadStringTaskAsync(url); } catch (Exception) { } @@ -60,7 +59,7 @@ private static async Task Main(string[] args) } } - private async Task Login(IWebDriver driverlocal, WebDriverWait localwait) + private async Task LoginAsync(IWebDriver driverlocal, WebDriverWait localwait) { //page 1 driverlocal.Navigate().GoToUrl(Constants.LiveLoginUrl); @@ -69,7 +68,7 @@ private async Task Login(IWebDriver driverlocal, WebDriverWait localwait) username.SendKeys(email); username.SendKeys(Keys.Enter); - await Task.Delay(3000); + await Task.Delay(1000); //page2 var passwordEntry = localwait?.Until(d => d.FindElement(By.Id(Constants.PasswordEntryId))); @@ -112,56 +111,82 @@ private async Task Login(IWebDriver driverlocal, WebDriverWait localwait) Debug.WriteLine(ex2.StackTrace); } } - - await Task.Delay(3000); - - if (localwait.Until(e => e.Title.Equals(Constants.RewardsPageTitle))) - driverlocal.SwitchTo().DefaultContent(); + finally + { + if (FindElementSafely(localwait, d => d.Title.Equals(Constants.RewardsPageTitle))) + { + driverlocal.SwitchTo().DefaultContent(); + } + } } - private async Task Run(bool useFirefox = false) + private bool FindElementSafely(WebDriverWait localwait, Func action) { - wordList = DownloadJsonData>(Constants.WordsListUrl); - DriverOptions driverOptions; - RemoteWebDriver driver; + var result = false; - if (useFirefox) + try { - var firefoxOptions = new FirefoxOptions(); - firefoxOptions.SetPreference(Constants.PrivateBrowsingKey, true); - driverOptions = firefoxOptions; - - driver = new FirefoxDriver(firefoxOptions); + result = localwait.Until(action); } - else + catch (Exception ex) { - driverOptions = new EdgeOptions { UseChromium = true }; - driver = new EdgeDriver(driverOptions as EdgeOptions); + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); } - WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120)); - wordList = DownloadJsonData>(Constants.WordsListUrl); - - await Login(driver, wait); - var result = CheckBreakDown(driver, wait); - driver?.Dispose(); - driver?.Quit(); - foreach (var keyvalue in result) + + return result; + } + + private async Task RunAsync(bool useFirefox = false) + { + try { - var current = keyvalue.Value.x; - var expected = keyvalue.Value.y; - Console.WriteLine($"{keyvalue.Key} : {current} of {expected} completed"); - if (current < expected) + RemoteWebDriver driver; + + if (useFirefox) + { + var firefoxOptions = new FirefoxOptions(); + firefoxOptions.SetPreference(Constants.PrivateBrowsingKey, true); + + driver = new FirefoxDriver(firefoxOptions); + } + else { - Console.WriteLine("Starting Bing Search for " + keyvalue.Key); - await BingSearch(keyvalue.Key, current, expected, useFirefox); + DriverOptions driverOptions = new EdgeOptions { UseChromium = true }; + driver = new EdgeDriver(driverOptions as EdgeOptions); } + Dictionary result; + using (driver) + { + WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); + wordList = await DownloadJsonDataAsync>(Constants.WordsListUrl); + + await LoginAsync(driver, wait); + result = CheckBreakDown(driver, wait); + + driver?.Quit(); + } + foreach (var keyvalue in result) + { + var current = keyvalue.Value.Current; + var expected = keyvalue.Value.Maximum; + Console.WriteLine($"{keyvalue.Key} : {current} of {expected} completed"); + if (current < expected) + { + Console.WriteLine("Starting Bing Search for " + keyvalue.Key); + await BingSearchAsync(keyvalue.Key, current, expected, useFirefox); + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex.StackTrace); } - Environment.Exit(0); } - private Dictionary CheckBreakDown(IWebDriver webDriver, WebDriverWait waiter) + private Dictionary CheckBreakDown(IWebDriver webDriver, WebDriverWait waiter) { - var result = new Dictionary(); + var result = new Dictionary(); webDriver.Navigate().GoToUrl(new Uri(Constants.PointsBreakdownUrl)); webDriver.SwitchTo().DefaultContent(); webDriver.SwitchTo().ActiveElement(); @@ -193,7 +218,7 @@ private async Task Run(bool useFirefox = false) key = RewardType.EdgeBonus; else key = RewardType.None; - result.Add(key, (x: current, y: total)); + result.Add(key, new PointStatus(current, total)); } } catch (Exception ex) @@ -205,13 +230,14 @@ private async Task Run(bool useFirefox = false) return result; } - private async Task BingSearch(RewardType rewardType, int current, int target, bool useFirefox = false) + private async Task BingSearchAsync(RewardType rewardType, int current, int target, bool useFirefox = false) { try { var rand = new Random(); - wordList = DownloadJsonData>(Constants.WordsListUrl); + wordList = await DownloadJsonDataAsync>(Constants.WordsListUrl); + //Edge browser if (rewardType == RewardType.EdgeBonus || !useFirefox) { var options = new EdgeOptions @@ -222,62 +248,70 @@ private async Task BingSearch(RewardType rewardType, int current, int target, bo if (rewardType == RewardType.Mobile) options.AddArgument($"{ Constants.EdgeUserAgentArgument}={Constants.MobileUserAgent}"); - var edgeDriver = new EdgeDriver(options); - edgeDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15); - var edgeWait = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(60)); + using (var edgeDriver = new EdgeDriver(options)) + { + var edgeWait = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(30)); - await Login(edgeDriver, edgeWait); + await LoginAsync(edgeDriver, edgeWait); - Search(edgeDriver, edgeWait, Constants.BingSearchURL + "Give me Edge points"); + Search(edgeDriver, edgeWait, Constants.BingSearchURL + "Give me Edge points"); - await Task.Delay(2000); + await Task.Delay(2000); - try - { - if (rewardType == RewardType.Mobile) + try { - var hamburg = edgeWait.Until(d => d.FindElement(By.Id(Constants.MHamburger))); - hamburg?.Click(); + if (rewardType == RewardType.Mobile) + { + var hamburg = edgeWait.Until(d => d.FindElement(By.Id(Constants.MHamburger))); + hamburg?.Click(); - var signin = edgeWait.Until(d => d.FindElement(By.Id(Constants.HbS))); - signin?.Click(); + var signin = edgeWait.Until(d => d.FindElement(By.Id(Constants.HbS))); + signin?.Click(); - await Task.Delay(3000); - } - else - { - var id_p = edgeWait.Until(d => d.FindElement(By.Id(Constants.ID_P))); - if (id_p != null) + await Task.Delay(1500); + } + else { - id_p?.Click(); + try + { + var id_p = edgeWait.Until(d => d.FindElement(By.Id(Constants.ID_P))); + if (id_p != null) + id_p?.Click(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + Debug.WriteLine(ex.StackTrace); + var id_a = edgeWait.Until(d => d.FindElement(By.Id(Constants.ID_A))); + if (id_a != null) + id_a?.Click(); + } } } - } - catch (Exception ex) - { - Debug.WriteLine(ex.Message); - Debug.WriteLine(ex.StackTrace); - } - - while (current < target) - { - var nextInt = rand.Next(wordList.Count); - Search(edgeDriver, edgeWait, Constants.BingSearchURL + wordList[nextInt <= wordList.Count ? nextInt : 0]); - current += 5; - if (current >= target) + catch (Exception ex) { - var currentBreakDown = CheckBreakDown(edgeDriver, edgeWait); + Debug.WriteLine(ex.Message); + Debug.WriteLine(ex.StackTrace); + } - if (currentBreakDown.ContainsKey(rewardType)) + while (current < target) + { + var nextInt = rand.Next(wordList.Count); + Search(edgeDriver, edgeWait, Constants.BingSearchURL + wordList[nextInt <= wordList.Count ? nextInt : 0]); + current += 5; + if (current >= target) { - current = currentBreakDown[rewardType].x; - Console.WriteLine("{0} points of {1} completed", currentBreakDown[rewardType].x, currentBreakDown[rewardType].y); + var currentBreakDown = CheckBreakDown(edgeDriver, edgeWait); + + if (currentBreakDown.ContainsKey(rewardType)) + { + current = currentBreakDown[rewardType].Current; + Console.WriteLine("{0} points of {1} completed", currentBreakDown[rewardType].Current, currentBreakDown[rewardType].Maximum); + } } } + edgeDriver?.Quit(); } - edgeDriver?.Dispose(); - edgeDriver?.Quit(); - //} } //Use Firefox else @@ -291,59 +325,69 @@ private async Task BingSearch(RewardType rewardType, int current, int target, bo } options.SetPreference(Constants.PrivateBrowsingKey, true); - using var firefoxDriver = new FirefoxDriver(options); - WebDriverWait driverWait = new WebDriverWait(firefoxDriver, timeout); - await Login(firefoxDriver, driverWait); + using (var firefoxDriver = new FirefoxDriver(options)) + { + WebDriverWait driverWait = new WebDriverWait(firefoxDriver, timeout); + await LoginAsync(firefoxDriver, driverWait); - firefoxDriver.Navigate().GoToUrl(Constants.BingSearchURL + "Give me edge points"); + firefoxDriver.Navigate().GoToUrl(Constants.BingSearchURL + "Edge Points"); - await Task.Delay(2000); + await Task.Delay(2000); - try - { - if (rewardType == RewardType.Mobile) + try { - var hamburg = driverWait.Until(d => d.FindElement(By.Id(Constants.MHamburger))); - hamburg?.Click(); + if (rewardType == RewardType.Mobile) + { + var hamburg = driverWait.Until(d => d.FindElement(By.Id(Constants.MHamburger))); + hamburg?.Click(); - var signin = driverWait.Until(d => d.FindElement(By.Id(Constants.HbS))); - signin?.Click(); + var signin = driverWait.Until(d => d.FindElement(By.Id(Constants.HbS))); + signin?.Click(); - await Task.Delay(3000); - } - else - { - var id_p = driverWait.Until(d => d.FindElement(By.Id(Constants.ID_P))); - if (id_p != null) + await Task.Delay(1500); + } + else { - id_p.Click(); + try + { + var id_p = driverWait.Until(d => d.FindElement(By.Id(Constants.ID_P))); + if (id_p != null) + id_p.Click(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + Debug.WriteLine(ex.StackTrace); + var id_a = driverWait.Until(d => d.FindElement(By.Id(Constants.ID_A))); + if (id_a != null) + id_a.Click(); + } } } - } - catch (Exception ex) - { - Debug.WriteLine(ex.Message); - Debug.WriteLine(ex.StackTrace); - } - - while (current < target) - { - var nextInt = rand.Next(wordList.Count); - Search(firefoxDriver, driverWait, Constants.BingSearchURL + wordList[nextInt <= wordList.Count ? nextInt : 0]); - current += 5; - if (current >= target) + catch (Exception ex) { - var currentBreakDown = CheckBreakDown(firefoxDriver, driverWait); + Debug.WriteLine(ex.Message); + Debug.WriteLine(ex.StackTrace); + } - if (currentBreakDown.ContainsKey(rewardType)) + while (current < target) + { + var nextInt = rand.Next(wordList.Count); + Search(firefoxDriver, driverWait, Constants.BingSearchURL + wordList[nextInt <= wordList.Count ? nextInt : 0]); + current += 5; + if (current >= target) { - current = currentBreakDown[rewardType].x; - Console.WriteLine("{0} points of {1} completed", currentBreakDown[rewardType].x, currentBreakDown[rewardType].y); + var currentBreakDown = CheckBreakDown(firefoxDriver, driverWait); + + if (currentBreakDown.ContainsKey(rewardType)) + { + current = currentBreakDown[rewardType].Current; + Console.WriteLine($"{currentBreakDown[rewardType].Current} points of {currentBreakDown[rewardType].Maximum} completed"); + } } } + firefoxDriver?.Quit(); } - firefoxDriver?.Dispose(); - firefoxDriver?.Quit(); } } catch (Exception ex)