示例#1
0
        public override async Task <DBSubscription> CreateSubscriptionAsync(Uri uri)
        {
            var username = uri.Segments[1];

            await UpdateToken();

            var user = await PollyHelper.WebFallbackAsync(() => _client.UserByScreenName(username));

            if (user != null)
            {
                _lastRequest = DateTimeOffset.Now;
            }
            if (user?.Data.User == null)
            {
                return(null);
            }

            return(new DBSubscription
            {
                SourceId = user.Data.User.RestId.ToString(),
                Username = user.Data.User.Legacy.ScreenName,
                IsProtected = user.Data.User.Legacy.Protected,
                Source = ESource.Twitter
            });
        }
示例#2
0
        public override async Task <List <DBCard> > GetCardsAsync(DBSubscription subscription)
        {
            await UpdateToken();

            var timeline = await PollyHelper.WebFallbackAsync(() => _client.UserTweetsAsync(long.Parse(subscription.SourceId), Settings.Default.TwitterMaxLoadPerUser));

            if (timeline != null)
            {
                _lastRequest = DateTimeOffset.Now;
            }

            return(timeline?.GlobalObjects.Tweets.Values
                   .Where(w => w.UserId.ToString() == subscription.SourceId).Select(s =>
            {
                var media = s.ExtendedEntities?.Media?.FirstOrDefault();

                return new DBCard
                {
                    Timestamp = s.CreatedAt,
                    Text = TrimText(s.Text),
                    CardId = s.Id.ToString(),
                    Header = $"@{subscription.Username}",
                    HasImage = media?.Type == ETwitterMediaType.Photo || media?.Type == ETwitterMediaType.Gif,
                    HasVideo = media?.Type == ETwitterMediaType.Video,
                    HasRepost = s.QuotedId != null,
                    OriginalUrl = $"https://twitter.com/{subscription.Username}/status/{s.Id}",
                };
            }).ToList());
        }
示例#3
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);
        }
示例#4
0
        private async Task <bool> UpdateToken(bool force = false)
        {
            if (force || _lastRequest.AddMinutes(20) <= DateTimeOffset.Now)
            {
                _client.GuestToken = null;
                var token = await PollyHelper.WebFallbackAsync(() => _client.GetGuestTokenAsync());

                if (token != null)
                {
                    _client.GuestToken = token.Value;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }