public async Task <TweetDisplay> GetTweetAsync() { using (StreamReader r = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), "config/config.json"))) { string json = r.ReadToEnd(); twitterConfig = JsonConvert.DeserializeObject <TwitterConfig>(json); } TwitterService service = new TwitterService(twitterConfig.key, twitterConfig.secret); ListTweetsOnUserTimelineOptions opt = new ListTweetsOnUserTimelineOptions { ScreenName = "realDonaldTrump", Count = 200, IncludeRts = false }; var twitterTimelineResult = service.BeginListTweetsOnUserTimeline(opt); var twitterTimeline = service.EndListTweetsOnUserTimeline(twitterTimelineResult).ToList(); Random rnd = new Random(); var tweet = twitterTimeline[rnd.Next(0, twitterTimeline.Count - 1)]; var client = new RestClient($"https://history.muffinlabs.com/date/{tweet.CreatedDate.Month}/{tweet.CreatedDate.Day}"); var response = await client.ExecuteTaskAsync(new RestRequest(Method.GET)); var eventsContent = JObject.Parse(response.Content).SelectTokens("$..Events..text"); var years = JObject.Parse(response.Content).SelectTokens("$..Events..year"); var index = rnd.Next(0, eventsContent.Count() - 1); var eventContent = eventsContent.ToList()[index]; var year = years.ToList()[index]; return(new TweetDisplay(tweet, eventContent.ToString(), year.ToString())); }
public List <TwitterStatus> GetTweetsFromServiceAsync(string account) { if (account == null || string.IsNullOrEmpty(account)) { return(new List <TwitterStatus>()); } var accountOptions = new ListTweetsOnUserTimelineOptions { ScreenName = account, Count = TweetsCount }; IAsyncResult asyncresult = twitterService.BeginListTweetsOnUserTimeline(accountOptions); var result = twitterService.EndListTweetsOnHomeTimeline(asyncresult, TimeOut); if (!asyncresult.IsCompleted) { throw new TimeoutException(); } return(result == null ? new List <TwitterStatus>() : result.ToList()); }