Exemple #1
0
        private bool LoadPodcastFeed(PodcastFeed sub, string rss, uint maxItems)
        {
            bool success = false;

            try
            {
                if (!string.IsNullOrEmpty(rss))
                {
                    FeedType feedType = GetFeedType(rss);
                    switch (feedType)
                    {
                    case FeedType.Atom:
                        success = LoadXMLAtom(sub, rss, maxItems);
                        break;

                    case FeedType.Rss1:
                        success = LoadXMLRSS1_0(sub, rss, maxItems);
                        break;

                    case FeedType.Rss2:
                        success = LoadXMLRSS2_0(sub, rss, maxItems);
                        break;
                    }
                }
                sub.IsLoaded = success;
            }
            catch (Exception ex)
            {
                sub.HasErrors = true;
                OnError(ex.ToString());
            }
            return(success);
        }
Exemple #2
0
        public bool LoadPodcastFeedAsync(PodcastFeed sub, uint maxItems)
        {
            string url     = sub.RssLink;
            bool   success = true;

            try
            {
                Stream rssStream;
                using (var client = new RssWebClient(sub))
                {
                    client.OpenReadCompleted += LoadSubscriptionOpenReadCompleted;
                    rssStream = client.OpenRead(url);
                }
                using (var reader = new StreamReader(rssStream))
                {
                    string rss = reader.ReadToEnd();
                    LoadPodcastFeed(sub, rss, maxItems);
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
                success = false;
            }
            return(success);
        }
Exemple #3
0
        public static List <PodcastFeed> DeserializeFeeds(string json, IRssParser parser)
        {
            //Ex.
            //https://itunes.apple.com/lookup?id=278981407&entity=podcast
            var subscriptions = new List <PodcastFeed>();

            string feedUrl      = string.Empty;
            JToken rootToken    = JObject.Parse(json);
            JToken resultsToken = rootToken["results"];

            foreach (var subToken in resultsToken)
            {
                var sub = new PodcastFeed();
                sub.RssLink  = (string)subToken["feedUrl"];
                sub.Category = "Podcasts";
                sub.Title    = (string)subToken["collectionName"];
                sub.ImageUrl = (string)subToken["artworkUrl100"];
                sub.MaxItems = 0;
                parser.LoadPodcastFeed(sub, sub.MaxItems);

                subscriptions.Add(sub);
            }

            return(subscriptions);
        }
Exemple #4
0
        private bool LoadXMLRSS1_0(PodcastFeed sub, string rss, uint maxItems)
        {
            bool success = true;

            try
            {
                var xmlDocument = new XmlDocument();
                var bytes       = Encoding.UTF8.GetBytes(rss);
                using (var stream = new MemoryStream(bytes))
                {
                    xmlDocument.Load(stream);
                }
                var channels = xmlDocument.GetElementsByTagName("channel");
                foreach (XmlElement channel in channels)
                {
                    int count = 0;
                    sub.Title       = GetXmlElementValue(channel, "title");
                    sub.Description = GetXmlElementValue(channel, "description");
                    sub.SiteLink    = GetXmlElementValue(channel, "link");

                    var items = channel.GetElementsByTagName("item");

                    foreach (XmlNode item in items)
                    {
                        if (count++ >= maxItems)
                        {
                            break;
                        }
                        string itemTitle = GetXmlElementValue(item, "title");

                        PodcastEpisode it     = sub.PodcastEpisodes.FirstOrDefault(i => i.Title == itemTitle);
                        bool           noItem = it == null;
                        if (noItem)
                        {
                            it       = new PodcastEpisode();
                            it.Title = itemTitle;
                        }

                        it.Title        = itemTitle;
                        it.Description  = GetXmlElementValue(item, "description");
                        it.Link         = GetXmlElementValue(item, "link");
                        it.Guid         = GetXmlElementValue(item, "guid");
                        it.PubDate      = GetXmlElementValue(item, "pubDate");
                        it.RowNum       = count;
                        it.ParentFeed   = sub;
                        sub.ItemsLoaded = true;
                        it.IsLoaded     = true;
                        sub.PodcastEpisodes.Add(it);
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
                success = false;
            }
            sub.HasErrors = !success;
            return(success);
        }
Exemple #5
0
        private void OnSubscriptionParsedComplete(PodcastFeed subscription)
        {
            var copy = SubscriptionParsedComplete;

            if (copy != null)
            {
                copy.Invoke(subscription);
            }
        }
Exemple #6
0
        private bool LoadXMLAtom(PodcastFeed sub, string rss, uint maxItems)
        {
            bool success = true;

            try
            {
                var doc = new XmlDocument();
                doc.LoadXml(rss);
                var feedNode = doc["feed"];
                sub.Title = GetXmlElementValue(feedNode, "title");
                if (string.IsNullOrEmpty(sub.ImageUrl))
                {
                    sub.ImageUrl = GetXmlElementValue(feedNode, "icon");
                }

                var entries = feedNode.GetElementsByTagName("entry");

                int count = 0;
                foreach (XmlNode entry in entries)
                {
                    if (count++ >= maxItems)
                    {
                        break;
                    }

                    string         itemTitle = GetXmlElementValue(entry, "title");
                    PodcastEpisode it        = sub.PodcastEpisodes.FirstOrDefault(i => i.Title == itemTitle);
                    bool           noItem    = it == null;
                    if (noItem)
                    {
                        it       = new PodcastEpisode();
                        it.Title = itemTitle;
                    }

                    it.Title       = itemTitle;
                    it.Description = GetXmlElementValue(entry, "summary");
                    it.PubDate     = GetXmlElementValue(entry, "updated");

                    it.Link         = GetXmlAttribute(entry["link"], "href");
                    it.RowNum       = count;
                    it.ParentFeed   = sub;
                    sub.ItemsLoaded = true;
                    it.IsLoaded     = true;
                    sub.PodcastEpisodes.Add(it);
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
                success = false;
            }
            sub.HasErrors = !success;
            return(success);
        }
Exemple #7
0
        private PodcastFeed GetiTunesPodcasts(PodcastGenre genre, uint limit)
        {
            var url = GetiTunesSourceUrl(genre, limit);

            var sourceSub = new PodcastFeed()
            {
                RssLink  = url,
                Title    = genre.ToString(),
                Category = "iTunes",
                MaxItems = limit,
            };

            _parser.LoadPodcastFeed(sourceSub, limit);

            return(sourceSub);
        }
Exemple #8
0
        public bool LoadSubscriptionAsync(PodcastFeed sub)
        {
            string url     = sub.RssLink;
            bool   success = true;

            try
            {
                using (var client = new RssWebClient(sub))
                {
                    client.OpenReadCompleted += LoadSubscriptionOpenReadCompleted;
                    client.OpenReadAsync(new Uri(url));
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
                success = false;
            }
            return(success);
        }
Exemple #9
0
        public bool LoadPodcastFeed(PodcastFeed sub, uint maxItems)
        {
            string url     = sub.RssLink;
            bool   success = true;

            try
            {
                Stream rssStream;
                using (var client = new RssWebClient(sub))
                {
                    rssStream = client.OpenRead(url);
                }
                using (var reader = new StreamReader(rssStream))
                {
                    string rss = reader.ReadToEnd();
                    LoadPodcastFeed(sub, rss, maxItems);
                }
            }
            catch (Exception)
            {
                success = false;
            }
            return(success);
        }
 public RssWebClient(PodcastFeed subscription)
 {
     Subscription = subscription;
     MaxItems     = subscription.MaxItems;
 }
Exemple #11
0
        private bool LoadXMLRSS2_0(PodcastFeed sub, string rss, uint maxItems)
        {
            bool success = true;

            try
            {
                var xmlDocument = new XmlDocument();
                var bytes       = Encoding.UTF8.GetBytes(rss);
                using (var stream = new MemoryStream(bytes))
                {
                    xmlDocument.Load(stream);
                }
                var channels = xmlDocument.GetElementsByTagName("channel");
                foreach (XmlElement channel in channels)
                {
                    int counter = 0;
                    sub.Title         = GetXmlElementValue(channel, "title");
                    sub.SiteLink      = GetXmlElementValue(channel, "link");
                    sub.Description   = GetXmlElementValue(channel, "description");
                    sub.PubDate       = GetXmlElementValue(channel, "pubDate");
                    sub.Ttl           = GetXmlElementValue(channel, "ttl");
                    sub.LastBuildDate = GetXmlElementValue(channel, "lastBuildDate");

                    if (string.IsNullOrEmpty(sub.ImageUrl))
                    {
                        var imageNodes = channel.GetElementsByTagName("image");
                        if (imageNodes.Count > 0)
                        {
                            var imageNode = imageNodes[0];
                            sub.ImageUrl = GetXmlElementValue(imageNode, "url");
                        }
                    }

                    var items = channel.GetElementsByTagName("item");
                    foreach (XmlNode item in items)
                    {
                        if (counter++ >= maxItems)
                        {
                            break;
                        }

                        string         itemTitle = GetXmlElementValue(item, "title");
                        PodcastEpisode it        = sub.PodcastEpisodes.FirstOrDefault(i => i.Title == itemTitle);
                        bool           noItem    = it == null;
                        if (noItem)
                        {
                            it       = new PodcastEpisode();
                            it.Title = itemTitle;
                        }
                        it.Link = GetXmlElementValue(item, "link");

                        if (item["enclosure"] != null)
                        {
                            it.Link = GetXmlAttribute(item["enclosure"], "url");
                        }

                        it.Description = processDescription(GetXmlElementValue(item, "description"));
                        it.Guid        = GetXmlElementValue(item, "guid");
                        it.PubDate     = GetXmlElementValue(item, "pubDate");

                        string durationString;
                        if (TryGetXmlElementValue(item, "itunes:duration", out durationString))
                        {
                            TimeSpan durationTimeSpan;
                            if (TimeSpan.TryParse(durationString, out durationTimeSpan))
                            {
                                it.DurationMs = (int)durationTimeSpan.TotalMilliseconds;
                            }
                        }

                        it.RowNum     = counter;
                        it.ParentFeed = sub;

                        it.IsLoaded     = true;
                        sub.ItemsLoaded = true;

                        if (noItem)
                        {
                            sub.PodcastEpisodes.Add(it);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
                success = false;
            }
            sub.HasErrors = !success;
            return(success);
        }