Ejemplo n.º 1
0
        public override ProgrammeInfo GetProgrammeInfo(string progExtId)
        {
            XmlDocument         rss          = this.LoadFeedXml(new Uri(progExtId));
            XmlNamespaceManager namespaceMgr = this.CreateNamespaceMgr(rss);

            XmlNode titleNode = rss.SelectSingleNode("./rss/channel/title");

            if (titleNode == null || string.IsNullOrEmpty(titleNode.InnerText))
            {
                throw new InvalidDataException("Channel title node is missing or empty");
            }

            ProgrammeInfo progInfo = new ProgrammeInfo();

            progInfo.Name = titleNode.InnerText;

            // If the channel has an itunes:summary tag use this for the description (as it shouldn't contain HTML)
            var descriptionNode = rss.SelectSingleNode("./rss/channel/itunes:summary", namespaceMgr);

            if (!string.IsNullOrEmpty(descriptionNode?.InnerText))
            {
                progInfo.Description = this.TidyUpWhitespace(descriptionNode.InnerText);
            }
            else
            {
                // Fall back to the standard description tag, but strip the HTML
                descriptionNode = rss.SelectSingleNode("./rss/channel/description");

                if (!string.IsNullOrEmpty(descriptionNode?.InnerText))
                {
                    progInfo.Description = this.TidyUpWhitespace(HtmlToText.ConvertHtml(descriptionNode.InnerText));
                }
            }

            progInfo.Image = this.FetchImage(rss, "./rss/channel/itunes:image/@href", namespaceMgr);

            if (progInfo.Image == null)
            {
                progInfo.Image = this.FetchImage(rss, "./rss/channel/image/url/text()", namespaceMgr);
            }

            return(progInfo);
        }
        public ProgrammeInfo GetProgrammeInfo(string progExtId)
        {
            XmlDocument         rss          = this.LoadFeedXml(new Uri(progExtId));
            XmlNamespaceManager namespaceMgr = this.CreateNamespaceMgr(rss);

            XmlNode titleNode = rss.SelectSingleNode("./rss/channel/title");

            if (titleNode == null || string.IsNullOrEmpty(titleNode.InnerText))
            {
                throw new InvalidDataException("Channel title node is missing or empty");
            }

            ProgrammeInfo progInfo = new ProgrammeInfo();

            progInfo.Name = titleNode.InnerText;

            XmlNode descriptionNode = null;

            // If the channel has an itunes:summary tag use this for the description (as it shouldn't contain HTML)
            if (namespaceMgr.HasNamespace("itunes"))
            {
                descriptionNode = rss.SelectSingleNode("./rss/channel/itunes:summary", namespaceMgr);
            }

            if (descriptionNode != null && !string.IsNullOrEmpty(descriptionNode.InnerText))
            {
                progInfo.Description = descriptionNode.InnerText;
            }
            else
            {
                // Fall back to the standard description tag, but strip the HTML
                descriptionNode = rss.SelectSingleNode("./rss/channel/description");

                if (descriptionNode != null && !string.IsNullOrEmpty(descriptionNode.InnerText))
                {
                    progInfo.Description = HtmlToText.ConvertHtml(descriptionNode.InnerText);
                }
            }

            progInfo.Image = this.RSSNodeImage(rss.SelectSingleNode("./rss/channel"), namespaceMgr);

            return(progInfo);
        }
Ejemplo n.º 3
0
        public override EpisodeInfo GetEpisodeInfo(string progExtId, ProgrammeInfo progInfo, string episodeExtId)
        {
            XmlDocument         rss          = this.LoadFeedXml(new Uri(progExtId));
            XmlNamespaceManager namespaceMgr = this.CreateNamespaceMgr(rss);
            XmlNode             itemNode     = this.ItemNodeFromEpisodeID(rss, episodeExtId);

            if (itemNode == null)
            {
                return(null);
            }

            EpisodeInfo episodeInfo = new EpisodeInfo();

            episodeInfo.Name = itemNode.SelectSingleNode("./title").InnerText;

            // If the item has an itunes:summary tag use this for the description (as it shouldn't contain HTML)
            var descriptionNode = itemNode.SelectSingleNode("./itunes:summary", namespaceMgr);

            if (!string.IsNullOrEmpty(descriptionNode?.InnerText))
            {
                episodeInfo.Description = this.TidyUpWhitespace(descriptionNode.InnerText);
            }
            else
            {
                // Fall back to the standard description tag, but strip the HTML
                descriptionNode = itemNode.SelectSingleNode("./description");

                if (!string.IsNullOrEmpty(descriptionNode?.InnerText))
                {
                    episodeInfo.Description = this.TidyUpWhitespace(HtmlToText.ConvertHtml(descriptionNode.InnerText));
                }
            }

            try
            {
                XmlNode durationNode = itemNode.SelectSingleNode("./itunes:duration", namespaceMgr);

                if (durationNode != null)
                {
                    string[] splitDuration = durationNode.InnerText.Split(new char[] { '.', ':' });

                    if (splitDuration.GetUpperBound(0) == 0)
                    {
                        episodeInfo.Duration = Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture);
                    }
                    else if (splitDuration.GetUpperBound(0) == 1)
                    {
                        episodeInfo.Duration = (Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture) * 60) + Convert.ToInt32(splitDuration[1], CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        episodeInfo.Duration = (((Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture) * 60) + Convert.ToInt32(splitDuration[1], CultureInfo.InvariantCulture)) * 60) + Convert.ToInt32(splitDuration[2], CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    episodeInfo.Duration = null;
                }
            }
            catch
            {
                episodeInfo.Duration = null;
            }

            XmlNode pubDateNode = itemNode.SelectSingleNode("./pubDate");

            if (pubDateNode != null)
            {
                string   pubDate = pubDateNode.InnerText.Trim();
                int      zonePos = pubDate.LastIndexOf(" ", StringComparison.Ordinal);
                TimeSpan offset  = new TimeSpan(0);

                if (zonePos > 0)
                {
                    string zone     = pubDate.Substring(zonePos + 1);
                    string zoneFree = pubDate.Substring(0, zonePos);

                    switch (zone)
                    {
                    case "GMT":
                        // No need to do anything
                        break;

                    case "UT":
                        offset  = new TimeSpan(0);
                        pubDate = zoneFree;
                        break;

                    case "EDT":
                        offset  = new TimeSpan(-4, 0, 0);
                        pubDate = zoneFree;
                        break;

                    case "EST":
                    case "CDT":
                        offset  = new TimeSpan(-5, 0, 0);
                        pubDate = zoneFree;
                        break;

                    case "CST":
                    case "MDT":
                        offset  = new TimeSpan(-6, 0, 0);
                        pubDate = zoneFree;
                        break;

                    case "MST":
                    case "PDT":
                        offset  = new TimeSpan(-7, 0, 0);
                        pubDate = zoneFree;
                        break;

                    case "PST":
                        offset  = new TimeSpan(-8, 0, 0);
                        pubDate = zoneFree;
                        break;

                    default:
                        int value;

                        if (zone.Length >= 4 && int.TryParse(zone, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value))
                        {
                            offset  = new TimeSpan(value / 100, value % 100, 0);
                            pubDate = zoneFree;
                        }

                        break;
                    }
                }

                // Strip the day of the week from the beginning of the date string if it is there,
                // as it can contradict the date itself.
                string[] days =
                {
                    "mon,",
                    "tue,",
                    "wed,",
                    "thu,",
                    "fri,",
                    "sat,",
                    "sun,",
                };

                foreach (string day in days)
                {
                    if (pubDate.StartsWith(day, StringComparison.OrdinalIgnoreCase))
                    {
                        pubDate = pubDate.Substring(day.Length).Trim();
                        break;
                    }
                }

                try
                {
                    episodeInfo.Date = DateTime.Parse(pubDate, null, DateTimeStyles.AssumeUniversal);
                    episodeInfo.Date = episodeInfo.Date.Value.Subtract(offset);
                }
                catch (FormatException)
                {
                    episodeInfo.Date = null;
                }
            }
            else
            {
                episodeInfo.Date = null;
            }

            episodeInfo.Image = this.FetchImage(itemNode, "./itunes:image/@href", namespaceMgr);

            if (episodeInfo.Image == null)
            {
                episodeInfo.Image = this.FetchImage(itemNode, "./media:thumbnail/@url", namespaceMgr);
            }

            return(episodeInfo);
        }