Пример #1
0
        public static async Task <List <Tweet> > GetTweets(HttpClient client)
        {
            var accessToken = await GetAccessToken(client);

            var requestUserTimeline = new HttpRequestMessage(HttpMethod.Get,
                                                             $"https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name=shanselman&trim_user=0&exclude_replies=1");

            requestUserTimeline.Headers.Add("Authorization", "Bearer " + accessToken);

            var responseUserTimeLine = await client.SendAsync(requestUserTimeline);

            var json = await responseUserTimeLine.Content.ReadAsStringAsync();

            var tweetsRaw = TweetRaw.FromJson(json);

            return(tweetsRaw.Select(t => new Tweet
            {
                StatusID = t?.RetweetedStatus?.User?.ScreenName == "shanselman" ? t.RetweetedStatus.IdStr : t.IdStr,
                ScreenName = t?.RetweetedStatus?.User?.ScreenName ?? t.User.ScreenName,
                Text = t.Text,
                CurrentUserRetweet = (ulong)t.RetweetCount,
                CreatedAt = GetDate(t.CreatedAt, DateTime.MinValue),
                Image = t.RetweetedStatus != null && t.RetweetedStatus.User != null ?
                        t.RetweetedStatus.User.ProfileImageUrlHttps.ToString() : (t.User.ScreenName == "shanselman" ? "scott159.png" : t.User.ProfileImageUrlHttps.ToString())
            }).ToList());
        }
Пример #2
0
        public async Task <IEnumerable <Tweet> > GetTweetsAsync(string screenName, bool includeRetweets)
        {
            var accessToken = await GetAccessToken(client);

            var requestUserTimeline = new HttpRequestMessage(HttpMethod.Get,
                                                             $"https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name={screenName}&trim_user=0&exclude_replies=1");

            requestUserTimeline.Headers.Add("Authorization", "Bearer " + accessToken);

            var responseUserTimeLine = await client.SendAsync(requestUserTimeline);

            var json = await responseUserTimeLine.Content.ReadAsStringAsync();

            var tweetsRaw = TweetRaw.FromJson(json);

            List <Tweet> tweets = tweetsRaw.Select(t => new Tweet
            {
                IsReTweet     = t?.RetweetedStatus != null,
                StatusID      = t?.RetweetedStatus?.User?.ScreenName == screenName ? t.RetweetedStatus.IdStr : t.IdStr,
                ScreenName    = t?.RetweetedStatus?.User?.ScreenName ?? t.User.ScreenName,
                Text          = HttpUtility.HtmlDecode(t?.Text),
                RetweetCount  = t.RetweetCount,
                FavoriteCount = t.FavoriteCount,
                CreatedAt     = GetDate(t.CreatedAt, DateTime.MinValue),
                Image         = t?.RetweetedStatus != null && t?.RetweetedStatus?.User != null ?
                                t.RetweetedStatus.User.ProfileImageUrlHttps.ToString() : (t?.User?.ScreenName == screenName ? "liar_in_chief.png" : t?.User?.ProfileImageUrlHttps.ToString()),
                MediaUrl = t?.Entities?.Media?.FirstOrDefault()?.MediaUrlHttps?.AbsoluteUri
            }).ToList();

            if (!includeRetweets)
            {
                tweets = tweets.Where(t => !t.IsReTweet).ToList();
            }
            return(tweets);
        }
        async Task <IEnumerable <TweetRaw> > GetTweets(string accessToken = null)
        {
            if (accessToken == null)
            {
                accessToken = await GetAccessToken();
            }

            var requestUserTimeline = new HttpRequestMessage(HttpMethod.Get,
                                                             $"https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name=shanselman&trim_user=0&exclude_replies=1");

            requestUserTimeline.Headers.Add("Authorization", "Bearer " + accessToken);
            var httpClient = new HttpClient();
            HttpResponseMessage responseUserTimeLine = await httpClient.SendAsync(requestUserTimeline);

            string json = await responseUserTimeLine.Content.ReadAsStringAsync();

            return(TweetRaw.FromJson(json));
        }