Beispiel #1
0
        public override async Task <DBSubscription> CreateSubscriptionAsync(Uri uri)
        {
            var id = uri.Segments.ElementAtOrDefault(2);

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

            if (uri.Segments.ElementAt(1) == "c/" || uri.Segments.ElementAt(1) == "user/")
            {
                var channelId = await PollyHelper.WebFallbackAsync(() => _client.ChannelId(uri.AbsoluteUri));

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

                id = channelId;
            }

            using (var xmlReader = XmlReader.Create($"https://www.youtube.com/feeds/videos.xml?channel_id={id}", new XmlReaderSettings {
                Async = true
            }))
            {
                var feedReader = new AtomFeedReader(xmlReader);

                while (await feedReader.Read())
                {
                    switch (feedReader.ElementType)
                    {
                    case SyndicationElementType.Content:
                        var content = await feedReader.ReadContent();

                        if (string.Equals(content.Name, "title", StringComparison.OrdinalIgnoreCase))
                        {
                            return(new DBSubscription
                            {
                                Source = ESource.Youtube,
                                IsProtected = false,
                                SourceId = id,
                                Username = content.Value
                            });
                        }
                        break;
                    }
                }
            }

            return(null);
        }