private void GetDailyBO(string movieID) { bool downloaded = false; int count = 0; HtmlDocument doc = new HtmlDocument(); TheNumbersDataContext dc = new TheNumbersDataContext(); List <DailyBO> dboDB = new List <DailyBO>(); // clean everything first dc.DailyBOs.DeleteAllOnSubmit(dc.DailyBOs.Where(dc1 => dc1.MovieID.Equals(movieID))); dc.SubmitChanges(); while (!downloaded) { try { doc.LoadHtml(Helper.GetHTML(url + @"#tab=box-office")); downloaded = true; } catch (Exception e) { downloaded = false; count++; if (count >= 3) { Helper.WriteToLog(ProgramStatus.Error, "error loading the movie"); Helper.WriteToError(url, e.StackTrace, year); } else { Task.Delay(5000); } } } if (downloaded) { try { var table1 = doc.DocumentNode.Descendants("div") .First(div => div.HasAttributes && div.Attributes.Any(attrib => attrib.Name.Equals("id")) && div.Attributes["id"].Value.Equals("box-office")); if (table1.Descendants("div").Count() > 1) { var table2 = table1 .Descendants("div").First(obj => obj.HasAttributes && obj.Attributes.Any(attr => attr.Name.Equals("id")) && obj.Attributes["id"].Value.Equals("box_office_chart")); var table = table2 .Descendants("table").FirstOrDefault(); List <HtmlNode> trs = table.Descendants("tr").ToList(); trs.RemoveAt(0); // remove header try { foreach (HtmlNode tr in trs) { var tds = tr.Descendants("td").ToArray(); DailyBO tempDBO = new DailyBO { MovieID = movieID }; tempDBO.DateCounted = DateTime.ParseExact(tds[0].Descendants("a").Single().InnerText, "yyyy/MM/dd", CultureInfo.InvariantCulture); tempDBO.Rank = tds[1].InnerText.Equals("-") ? 0 : Int32.Parse(tds[1].InnerText); tempDBO.Gross = Double.Parse(tds[2].InnerText.Replace("$", String.Empty).Replace(",", String.Empty).Replace(" ", string.Empty)); tempDBO.TheatersCount = Int32.Parse(tds[4].InnerText.Replace(",", string.Empty)); tempDBO.TotalGross = Double.Parse(tds[6].InnerText.Replace("$", string.Empty).Replace(",", string.Empty).Replace(" ", string.Empty)); tempDBO.NumDays = Int32.Parse(tds[7].InnerText.Replace("$", string.Empty).Replace(",", string.Empty).Replace(" ", string.Empty)); dboDB.Add(tempDBO); } } catch { throw; } dc.DailyBOs.InsertAllOnSubmit(dboDB); dc.SubmitChanges(); } } catch (Exception e) { Helper.WriteToLog(ProgramStatus.Error, "error loading the movie"); Helper.WriteToError(url, e.StackTrace, year); } } }
partial void UpdateDailyBO(DailyBO instance);
partial void DeleteDailyBO(DailyBO instance);
partial void InsertDailyBO(DailyBO instance);