Ejemplo n.º 1
0
        private void LoadGamesPerDivision(BrowserSession b, KeyValuePair<long, string> page)
        {
            try
            {
                var teams = GetTeams(1, page.Key);

                int take = 1;
                take = take + teams.Count / 2;

                if (teams.Count % 2 > 0)
                    take++;

                var doc = b.GetDoc(page.Value);

                //Table  => Games table
                var table =
                    doc.DocumentNode.Descendants("table")
                       .FirstOrDefault(
                           t => t.Attributes.Contains("class") && t.Attributes["class"].Value.Contains("clubentrees"));
                if (table != null)
                {

                    using (var context = new ScoresEntities())
                    {
                        int skip = 1;
                        var rows = table.Descendants("tr").Skip(skip).Take(take).ToList();

                        while (rows.Any())
                        {
                            var dateRow = rows[0];
                            DateTime? date = ProcessDateRow(dateRow);

                            if (date.HasValue)
                            {
                                foreach (var row in rows.Skip(1))
                                {
                                    Trace.WriteLine(row.InnerText);
                                    games game = ProcessGameRow(row, date.Value, teams, context, page);

                                    if (game != null && game.GameId <= 0)
                                        context.games.Add(game);
                                }
                            }
                            Trace.WriteLine("----------");
                            skip = skip + take + 1;
                            rows = table.Descendants("tr").Skip(skip).Take(take + 1).ToList();
                        }
                        context.SaveChanges();
                    }
                }
                else
                    Trace.WriteLine("---WRONG---");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("There was an error fetching division {0}", page.Key));
                throw;
            }
        }