示例#1
0
 // for debug
 private void PrintProgramInfo(AGQRProgram program)
 {
     Console.WriteLine("Title: {0}", program.Title);
     Console.WriteLine("Hosts: {0}", string.Join(", ", program.Hosts));
     Console.WriteLine("Program type: {0}", program.ProgramType);
     Console.WriteLine("Stream type: {0}", program.StreamType);
     Console.WriteLine("Starts at: {0}", program.Start);
     Console.WriteLine("Ends at: {0}", program.End);
     Console.WriteLine("Length: {0}", program.Length);
     Console.WriteLine("Emails: {0}", string.Join(",\n", program.Emails));
     Console.WriteLine("URLs: {0}", string.Join(",\n", program.URLs));
     Console.WriteLine("Images: {0}", string.Join(",\n", program.Images));
     Console.WriteLine();
 }
示例#2
0
        private AGQRProgram GetPauseProgram()
        {
            AGQRProgram p = new AGQRProgram();

            p.Start       = DateTime.Now;
            p.End         = new DateTime(p.Start.Year, p.Start.Month, p.Start.Day, 6, 0, 0);
            p.Length      = (int)Math.Floor((p.End - p.Start).TotalMinutes);
            p.ProgramType = AGQRProgramType.Pause;
            p.StreamType  = AGQRStreamType.Movie;
            p.Title       = "放送休止";
            p.Emails      = new List <string>();
            p.Hosts       = new List <string>();
            p.Images      = new List <string>();
            p.URLs        = new List <string>();
            return(p);
        }
示例#3
0
        private List <DailyPrograms> Scraping()
        {
            List <DailyPrograms> res = new List <DailyPrograms>();

            HtmlDocument doc  = new HtmlDocument();
            string       html = GetHtml(TimetableUrl);

            doc.LoadHtml(html);

            this.UpdatedDateTime = DateTime.Now;

            // according to today.js, the timetable will be changed at 5 AM.
            DateTime today = DateTime.Now;

            if (today.Hour < 5)
            {
                today = today.AddDays(-1);
            }
            today = new DateTime(today.Year, today.Month, today.Day, 6, 0, 0);

            this.ExpiryDateTime = today.AddDays(7).AddHours(-1);

            List <DateTime> times = Enumerable.Repeat(new DateTime(), 7).ToList();

            for (int i = 0; i < 7; i++)
            {
                int p = (i + (int)today.DayOfWeek - 1) % 7;
                if (p < 0)
                {
                    p += 7;
                }
                times[p] = today + new TimeSpan(i, 0, 0, 0);
            }

            for (int i = 0; i < 7; i++)
            {
                DailyPrograms dp = new DailyPrograms();
                dp.Date     = times[i].Date;
                dp.Programs = new List <AGQRProgram>();
                res.Add(dp);
            }

            var tbody = doc.DocumentNode.SelectNodes("//table/tbody/tr").Where(x => x.InnerText != "\n");

            foreach (var tr in tbody)
            {
                foreach (var td in tr.SelectNodes("./td") ?? Enumerable.Empty <HtmlNode>())
                {
                    AGQRProgram program = new AGQRProgram();

                    // ProgramType
                    program.ProgramType = GetProgramType(td.GetAttributeValue("class", "").Trim());

                    // Title
                    program.Title = td.SelectSingleNode("./div[@class='title-p']").InnerText.Trim();

                    // Length
                    program.Length = int.Parse(td.GetAttributeValue("rowspan", "").Trim());
                    if (program.Title == "ラジオアニメージュ" && program.Length == 29)
                    {
                        program.Length = 30;
                    }

                    // URLs
                    program.URLs = new List <string>();
                    var titlepa = td.SelectSingleNode("./div[@class='title-p']/a");
                    if (titlepa != null)
                    {
                        program.URLs.Add(titlepa.GetAttributeValue("href", "").Trim());
                    }
                    foreach (var a in td.SelectNodes("./div[@class='bnr']/a") ?? Enumerable.Empty <HtmlNode>())
                    {
                        program.URLs.Add(a.GetAttributeValue("href", "").Trim());
                    }
                    program.URLs = program.URLs.Distinct().ToList();

                    // Images
                    program.Images = new List <string>();
                    foreach (var img in td.SelectNodes("./div[@class='bnr']//img") ?? Enumerable.Empty <HtmlNode>())
                    {
                        program.Images.Add(new Uri(TimetableUri, img.GetAttributeValue("src", "").Trim()).AbsoluteUri);
                    }
                    program.Images = program.Images.Distinct().ToList();

                    // Hosts
                    program.Hosts = td.SelectSingleNode("./div[@class='rp']").InnerText.Trim().Split('、').Distinct().ToList();

                    // Emails
                    program.Emails = new List <string>();
                    foreach (var s in new string[] { "./div[@class='rp']/a", "./a" })
                    {
                        foreach (var a in td.SelectNodes(s) ?? Enumerable.Empty <HtmlNode>())
                        {
                            if (a.SelectNodes("./img") != null)
                            {
                                program.Emails.Add(a.GetAttributeValue("href", "").Trim().Replace("mailto:", ""));
                            }
                        }
                    }
                    program.Emails = program.Emails.Distinct().ToList();

                    // StreamType
                    program.StreamType = AGQRStreamType.Audio;
                    if (td.SelectSingleNode("./div[@class='time']/span/img") != null)
                    {
                        program.StreamType = AGQRStreamType.Movie;
                    }

                    // Start, End
                    var min = getMinTimeSpan(times);
                    for (int i = 0; i < times.Count(); i++)
                    {
                        if (min == times[i].TimeOfDay)
                        {
                            program.Start = times[i];
                            times[i]      = times[i].AddMinutes(program.Length);
                            program.End   = times[i];
                            if (times[i].Hour == 6)
                            {
                                times.RemoveAt(i);
                            }
                            break;
                        }
                    }

                    var spDate = GetSpecializedDate(program.Start);
                    for (int i = 0; i < 7; i++)
                    {
                        if (res[i].Date == spDate.Date)
                        {
                            res[i].Programs.Add(program);
                            break;
                        }
                    }
                }
            }

            return(res);
        }