internal void TryScrapePoolEntries() { bool entriesKnown = true; List<PlayerEntry> newEntries = new List<PlayerEntry>(); try { _poolEntriesDirty = false; Console.Write("Scraping CBS: "); using (CookieAwareWebClient wc = new CookieAwareWebClient()) { wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36"); var loginData = new NameValueCollection { {"dummy::login_form", "1"}, {"form::login_form", "login_form"}, {"xurl", @"http://statsnfl.football.cbssports.com/"}, {"master_product", "150"}, {"vendor", "cbssports"}, {"userid", _cbsUser}, {"password", _cbsPass}, {"_submit", "Sign In"} }; wc.UploadValues(@"http://www.cbssports.com/login", "POST", loginData); string fullHtml = wc.DownloadString(@"http://statsnfl.football.cbssports.com/office-pool/standings/live"); Match data = Regex.Match(fullHtml, @"new CBSi.app.OPMLiveStandings\(.*?({.*?})\s*\);", RegexOptions.IgnoreCase | RegexOptions.Singleline); string cleanData = Regex.Replace(data.Groups[1].ToString(), @"""time"":""[^""]*"",?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); var ser = new JavaScriptSerializer(); Dictionary<string, object> result = (Dictionary<string, object>)ser.DeserializeObject(cleanData); if (!((object[])result["teams"]).Any()) return; _poolEntriesDirty = true; foreach (Dictionary<string, object> team in (object[])result["teams"]) { if (!team.ContainsKey("picks")) continue; string name = team["name"].ToString(); PlayerEntry entry = new PlayerEntry(name); foreach (KeyValuePair<string, object> pick in (Dictionary<string, Object>)team["picks"]) { if (pick.Key == "mnf") continue; Dictionary<string, object> pickData = (Dictionary<string, object>)pick.Value; string winner = pickData["winner"].ToString(); int points = int.Parse(pickData["weight"].ToString(), CultureInfo.InvariantCulture); entry.AddPick(winner, points); } newEntries.Add(entry); if (!entry.GamePicks.Any()) entriesKnown = false; } Console.Write(newEntries.Count + ". "); } PlayerEntries = newEntries; } catch (Exception ex) { Console.WriteLine(ex); } finally { _playerEntriesKnown = entriesKnown; } }
internal void TryScrapePoolEntries() { bool entriesKnown = true; List <PlayerEntry> newEntries = new List <PlayerEntry>(); try { _poolEntriesDirty = false; Console.Write("Scraping CBS: "); using (CookieAwareWebClient wc = new CookieAwareWebClient()) { wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36"); var loginData = new NameValueCollection { { "dummy::login_form", "1" }, { "form::login_form", "login_form" }, { "xurl", @"http://statsnfl.football.cbssports.com/" }, { "master_product", "150" }, { "vendor", "cbssports" }, { "userid", _cbsUser }, { "password", _cbsPass }, { "_submit", "Sign In" } }; wc.UploadValues(@"http://www.cbssports.com/login", "POST", loginData); string fullHtml = wc.DownloadString(@"http://statsnfl.football.cbssports.com/office-pool/standings/live"); Match data = Regex.Match(fullHtml, @"new CBSi.app.OPMLiveStandings\(.*?({.*?})\s*\);", RegexOptions.IgnoreCase | RegexOptions.Singleline); string cleanData = Regex.Replace(data.Groups[1].ToString(), @"""time"":""[^""]*"",?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); var ser = new JavaScriptSerializer(); Dictionary <string, object> result = (Dictionary <string, object>)ser.DeserializeObject(cleanData); if (!((object[])result["teams"]).Any()) { return; } _poolEntriesDirty = true; foreach (Dictionary <string, object> team in (object[])result["teams"]) { if (!team.ContainsKey("picks")) { continue; } string name = team["name"].ToString(); PlayerEntry entry = new PlayerEntry(name); foreach (KeyValuePair <string, object> pick in (Dictionary <string, Object>)team["picks"]) { if (pick.Key == "mnf") { continue; } Dictionary <string, object> pickData = (Dictionary <string, object>)pick.Value; string winner = pickData["winner"].ToString(); int points = int.Parse(pickData["weight"].ToString(), CultureInfo.InvariantCulture); entry.AddPick(winner, points); } newEntries.Add(entry); if (!entry.GamePicks.Any()) { entriesKnown = false; } } Console.Write(newEntries.Count + ". "); } PlayerEntries = newEntries; } catch (Exception ex) { Console.WriteLine(ex); } finally { _playerEntriesKnown = entriesKnown; } }