/// <summary> /// Returns the entire list of requested tweets on a user's timeline. /// </summary> /// <param name="username"></param> /// <param name="excludeReplies"></param> /// <param name="includeRetweets"></param> public List <ITweet> UserTimeline(string username, bool excludeReplies, bool includeRetweets) { var param = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200, ExcludeReplies = excludeReplies, IncludeRTS = includeRetweets }; var lastTweets = Timeline.GetUserTimeline(username, param); var allTweets = new List <ITweet>(lastTweets); while (lastTweets.Any() && allTweets.Count <= 3200) { long idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); int numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200; var timelineRequestParameters = new UserTimelineParameters { // MaxId ensures that we only get tweets that have been posted BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(username, timelineRequestParameters); allTweets.AddRange(lastTweets); } return(allTweets); }
public async Task UserInfo([Remainder] string username) { RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; var user = User.GetUserFromScreenName(username); if (user == null) { await ReplyAsync($"User with username {username} not found"); } var timelineparam = new UserTimelineParameters() { MaximumNumberOfTweetsToRetrieve = int.MaxValue, IncludeRTS = true, IncludeEntities = true, IncludeContributorDetails = true }; long tweetcount = (await user.GetUserTimelineAsync(1000)).Count(); long followercount = user.FollowersCount; long followingcount = (await user.GetFriendIdsAsync(int.MaxValue - 1)).Count(); long likecount = user.FavouritesCount; long listcount = user.ListedCount; var statistics = $"Tweets: {tweetcount}\nFollowing: {followingcount}\nFollowers: {followercount}\n" + $"Likes: {likecount}\nLists: {listcount}"; var E = new EmbedBuilder() .WithTitle($"{user.Name} @{user.ScreenName}") .WithThumbnailUrl(user.ProfileImageUrl) .WithDescription(user.Description) .AddField("Statistics", statistics, true) .AddField("Identifier", user.UserIdentifier, true); await ReplyAsync("", embed : E.Build()); }
/// <summary> /// Gets a number of tweets up to the maximum specified for a given user. Excludes retweets of other users. /// </summary> /// <param name="user"></param> /// <param name="maxNumberOfTweets"></param> /// <returns></returns> private static ITweet[] GetUserTimelineTweets(IUserIdentifier user, int maxNumberOfTweets = 200) { var tweets = new List <ITweet>(); var userTimelineParameter = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = maxNumberOfTweets, IncludeRTS = false }; userTimelineParameter.MaximumNumberOfTweetsToRetrieve = maxNumberOfTweets; var receivedTweets = Timeline.GetUserTimeline(user, userTimelineParameter).ToArray(); tweets.AddRange(receivedTweets); while (tweets.Count < maxNumberOfTweets && receivedTweets.Length == 200) { var oldestTweet = tweets.Min(x => x.Id); userTimelineParameter.MaxId = oldestTweet; userTimelineParameter.MaximumNumberOfTweetsToRetrieve = 200; receivedTweets = Timeline.GetUserTimeline(user, userTimelineParameter).ToArray(); tweets.AddRange(receivedTweets); } return(tweets.Distinct().ToArray()); }
private void button1_Click(object sender, EventArgs e) { // https://twitter.com/otonajyojifuku/media RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; RateLimit.QueryAwaitingForRateLimit += (s, args) => { Console.WriteLine($"Query : {args.Query} is awaiting for rate limits!"); }; long userId = 1706386254; var lastTweets = Timeline.GetUserTimeline(userId, 200).ToArray(); var allTweets = new List <ITweet>(lastTweets); var beforeLast = allTweets; while (lastTweets.Length > 0 && allTweets.Count <= 3200) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200; var timelineRequestParameters = new UserTimelineParameters { // MaxId ensures that we only get tweets that have been posted // BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(userId, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } }
public static List <ITweet> ListAllTweetsFromProfile(TwitterProfile profile) { RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; var lastTweets = Timeline.GetUserTimeline(profile.Name, smallTweetCount).ToArray(); var allTweets = new List <ITweet>(lastTweets); while (lastTweets.Length > 0 && allTweets.Count <= smallTweetCount) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : smallTweetCount; var timelineRequestParameters = new UserTimelineParameters { MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(profile.Name, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } return(allTweets); }
private void AddTimeLineTweets() { IEnumerable <Tweetinvi.Core.Interfaces.ITweet> tweets; if (User.GetAuthenticatedUser().ScreenName == this.Selecteduser.ScreenName) { var homeTimelineParameter = new HomeTimelineParameters { MaxId = this.TimeLineTweets.Last().Id, MaximumNumberOfTweetsToRetrieve = 15 }; tweets = Timeline.GetHomeTimeline(homeTimelineParameter); } else { var userTimelineParameters = new UserTimelineParameters { MaxId = this.TimeLineTweets.Last().Id, MaximumNumberOfTweetsToRetrieve = 15 }; tweets = Timeline.GetUserTimeline(this.Selecteduser.Id, userTimelineParameters); } var tweetsList = tweets.ToList(); tweetsList.RemoveAt(0); foreach (var tweet in tweetsList) { this.TimeLineTweets.Add((Tweet)tweet); } }
public void GetTweets() { UserTimelineParameters userTimelineParameters = new UserTimelineParameters(); userTimelineParameters.MaximumNumberOfTweetsToRetrieve = 50; try { var tweets = Auth.ExecuteOperationWithCredentials(_credentials, () => { return(Timeline.GetUserTimeline("cnn", userTimelineParameters)); }); if (_isRateLimited) { Console.WriteLine("Request rate limit has been exceeded. Please try again later."); } else if (tweets != null) { foreach (var item in tweets) { Console.WriteLine(item.FullText); } } Console.ReadKey(); } catch (WebException wex) { var statusCode = -1; if (statusCode == TweetinviConsts.STATUS_CODE_TOO_MANY_REQUEST) { // The RateLimit is exhausted. Perform your code to manage it! } } }
public ExtractedTweet[] GetUserTweets(string twitterUserName, int nberTweets, bool returnReplies = true, long fromTweetId = -1) { if (nberTweets > 200) { throw new ArgumentException("More than 200 Tweets retrieval isn't supported"); } var devSettings = _twitterSettingsRepository.GetTwitterDevApiSettings(); var userSettings = _twitterSettingsRepository.GetTwitterUserApiSettings(); Auth.SetUserCredentials(devSettings.ConsumerKey, devSettings.ConsumerSecret, userSettings.AccessToken, userSettings.AccessTokenSecret); TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended; var user = User.GetUserFromScreenName(twitterUserName); var tweets = new List <ITweet>(); if (fromTweetId == -1) { tweets.AddRange(Timeline.GetUserTimeline(user.Id, nberTweets)); } else { var timelineRequestParameters = new UserTimelineParameters { MaxId = fromTweetId - 1, MaximumNumberOfTweetsToRetrieve = nberTweets }; tweets.AddRange(Timeline.GetUserTimeline(user.Id, timelineRequestParameters)); } return(tweets.Where(x => returnReplies || string.IsNullOrWhiteSpace(x.InReplyToScreenName)).Select(ExtractTweet).ToArray()); }
public IEnumerable <ITweet> getTweets() { var utp = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200 }; return(Timeline.GetUserTimeline(us, utp)); }
/// <summary> /// Returns the latest 40 tweets. Excluding replies and/or retweets will lower the number returned. /// </summary> /// <param name="username"></param> /// <param name="excludeReplies"></param> /// <param name="includeRetweets"></param> public List <ITweet> UserTimelineRecent(string username, bool excludeReplies, bool includeRetweets) { var param = new UserTimelineParameters { ExcludeReplies = excludeReplies, IncludeRTS = includeRetweets }; return(Timeline.GetUserTimeline(username, param).ToList()); }
public void SetTwitterDataByIds(List <string> twitterIds) { logger.Info("/-----Twitter Service Save Timelines In Ids Start-----/"); int savedCount = 0; UserTimelineParameters parameters = new UserTimelineParameters() { IncludeRTS = false, MaximumNumberOfTweetsToRetrieve = 100, ExcludeReplies = true }; try { if (twitterIds.Count > 0) { foreach (string twitterId in twitterIds) { var tweets = User.GetUserFromScreenName(twitterId).GetUserTimeline(parameters); foreach (var tweet in tweets) { /* * [Column("TWITTER_ID")] * [Column("ACCOUNT_NAME")] * [Column("TWEET_TEXT")] * [Column("HASHTAGS")] * [Column("RETWEET_CNT")] * [Column("URL")] */ TwitterT entity = new TwitterT() { TwitterId = tweet.IdStr, TweetText = tweet.FullText, hashTags = string.Join(" ", tweet.Hashtags), AccountName = tweet.CreatedBy.Name, RetweetCount = tweet.RetweetCount, Url = tweet.Url }; twitterRepository.InsertTwitterData(entity); savedCount++; } } } logger.Info("/-----Twitter Service Save Timelines In Ids End! Saved Count : " + savedCount + " -----/"); logger.Info("/-----Twitter Data Save End-----/"); } catch (Exception e) { logger.Error(e, "Twitter Service Error"); } }
private UserTimelineParameters CreateParaeater(long safeTweetId) { var p = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 100 }; if (safeTweetId != 0) { p.MaxId = safeTweetId; } return(p); }
public IEnumerable <ITweet> GetAllValidTweets(string twitterScreenName, Predicate <string> isValidTweet = null) { if (!isInit) { throw new Exception("Not logged in to Twitter. Please set the required Authentification Environment Variables: CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET"); } RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; RateLimit.QueryAwaitingForRateLimit += (sender, args) => { Console.WriteLine($"Query : {args.Query} is awaiting for rate limits!"); }; var lastTweets = Timeline.GetUserTimeline(twitterScreenName, 200).ToArray(); var allTweets = new List <ITweet>(lastTweets); var beforeLast = allTweets; while (lastTweets.Length > 0 && allTweets.Count <= 3200) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200; var timelineRequestParameters = new UserTimelineParameters { // MaxId ensures that we only get tweets that have been posted // BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(twitterScreenName, timelineRequestParameters).ToArray(); if (isValidTweet == null) { allTweets.AddRange(lastTweets); } else { foreach (var item in lastTweets) { if (isValidTweet(item.FullText)) { allTweets.Add(item); } } } } return(allTweets); }
public IEnumerable <ITweet> GetUserTimeline(long userId, int maxNumberOfTweetsRetrieve, long?maxId = null) { if (maxId.HasValue) { var parameter = new UserTimelineParameters { MaxId = maxId.Value, MaximumNumberOfTweetsToRetrieve = maxNumberOfTweetsRetrieve }; return(Timeline.GetUserTimeline(userId, parameter)); } else { return(Timeline.GetUserTimeline(userId, maxNumberOfTweetsRetrieve)); } }
public List <Tweet> GetTweetsForExistingUser(long userId, long latestTweetId) { UserTimelineParameters timelineParameters = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200, IncludeRTS = false, SinceId = latestTweetId }; Task <IEnumerable <ITweet> > userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() => { return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters)); }); RequestsRemaining--; var twitterResponse = userTimlineAsync.Result; var latestException = Tweetinvi.ExceptionHandler.GetLastException(); if (latestException != null) { throw new InvalidOperationException ($"{latestException.StatusCode} : {latestException.TwitterDescription}"); } else if (twitterResponse == null || !twitterResponse.Any()) { Console.WriteLine($"No new tweets were returned for {userId} since {latestTweetId}."); return(null); } else { List <Tweet> tweets = new List <Tweet>(); foreach (var tweet in twitterResponse) { var oembedHtml = OEmbedTweet.GetHtml(tweet.Url); RequestsRemaining--; tweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName, tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id, tweet.FullText, tweet.Url, oembedHtml)); } return(tweets); } }
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1) { TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended; var user = _twitterUserService.GetUser(username); if (user.Protected) { return(new ExtractedTweet[0]); } var tweets = new List <ITweet>(); try { if (fromTweetId == -1) { var timeline = Timeline.GetUserTimeline(user.Id, nberTweets); _statisticsHandler.CalledTimelineApi(); if (timeline != null) { tweets.AddRange(timeline); } } else { var timelineRequestParameters = new UserTimelineParameters { SinceId = fromTweetId, MaximumNumberOfTweetsToRetrieve = nberTweets }; var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters); _statisticsHandler.CalledTimelineApi(); if (timeline != null) { tweets.AddRange(timeline); } } } catch (Exception e) { _logger.LogError(e, "Error retrieving timeline from {Username}, from {TweetId}", username, fromTweetId); } return(tweets.Select(_tweetExtractor.Extract).ToArray()); }
public static void SaveAllTweetsFromProfileToJson(TwitterProfile profile) { RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; var lastTweets = Timeline.GetUserTimeline(profile.Name, smallTweetCount).ToArray(); var allTweets = new List <ITweet>(lastTweets); while (lastTweets.Length > 0 && allTweets.Count <= smallTweetCount) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : smallTweetCount; var timelineRequestParameters = new UserTimelineParameters { MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(profile.Name, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } var jsonOfTweets = allTweets.Distinct().ToJson(); string fileName = "test.json"; FileStream fs = null; try { fs = new FileStream(fileName, FileMode.CreateNew); using (StreamWriter writer = new StreamWriter(fs)) { writer.Write(jsonOfTweets); } } finally { if (fs != null) { fs.Dispose(); } } }
public async Task <int> GetRetweetsCountForUserAsync(string userId) { var userTimelineParam = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200, IncludeRTS = true, TrimUser = true, IncludeEntities = false, ExcludeReplies = true, }; var user = new UserIdentifier(long.Parse(userId)); var tweets = await Auth.ExecuteOperationWithCredentials( this.credentials, () => TimelineAsync.GetUserTimeline(user, userTimelineParam)); return(tweets.Count(x => x.IsRetweet)); }
public IEnumerable <ITweet> GetTweets(string userName, [FromQuery] int tweetsCount = 5) { var user = Tweetinvi.User.GetUserFromScreenName(userName); if (user != null) { var userTimelineParam = new UserTimelineParameters() { MaximumNumberOfTweetsToRetrieve = tweetsCount, IncludeRTS = true }; var tweets = Timeline.GetUserTimeline(user, userTimelineParam).ToList(); return(tweets); } throw new Exception("UserIsNotExist"); }
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1) { var tweets = new List <ITweet>(); _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); ExceptionHandler.SwallowWebExceptions = false; TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended; var user = _twitterUserService.GetUser(username); if (user == null || user.Protected) { return(new ExtractedTweet[0]); } if (fromTweetId == -1) { var timeline = Timeline.GetUserTimeline(user.Id, nberTweets); _statisticsHandler.CalledTimelineApi(); if (timeline != null) { tweets.AddRange(timeline); } } else { var timelineRequestParameters = new UserTimelineParameters { SinceId = fromTweetId, MaximumNumberOfTweetsToRetrieve = nberTweets }; var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters); _statisticsHandler.CalledTimelineApi(); if (timeline != null) { tweets.AddRange(timeline); } } return(tweets.Select(_tweetExtractor.Extract).ToArray()); }
private static void GetTimelineTest() { RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; RateLimit.QueryAwaitingForRateLimit += (sender, args) => { Console.WriteLine($"Query : {args.Query} is awaiting for rate limits to be available!"); }; //var userId = 159053980; var userId = "realDonaldTrump"; UserTimelineParameters userTimelineParameters = new UserTimelineParameters() { SinceId = 1262787562206367744 }; //var lastTweets = Timeline.GetUserTimeline(userId, 200).ToArray(); var lastTweets = Timeline.GetUserTimeline(userId, userTimelineParameters).ToArray(); var allTweets = new List <ITweet>(lastTweets); var beforeLast = allTweets; while (lastTweets.Length > 0 && allTweets.Count <= 3200) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var timelineRequestParameters = new UserTimelineParameters { // We ensure that we only get tweets that have been posted BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = allTweets.Count > 3000 ? (3200 - allTweets.Count) : 200 }; lastTweets = Timeline.GetUserTimeline(userId, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } }
private static List <ITweet> GetUserTimelineTweets(string userName, int maxNumberOfTweets) { RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; RateLimit.QueryAwaitingForRateLimit += (sender, args) => { Console.WriteLine($"Query : {args.Query} is awaiting for rate limits!"); }; Auth.SetUserCredentials("YZYc1Jyp31tKZ5zgJNWIVhLeT", "x8QRxQxtUa6JmatvMJhViVYyihm3EYpkfO89LE1jd1wAL4BGVp", "852507449630261248-hEPZwkIzKALj2sEzmumHozsl5GpcQPv", "7SOZWdC7qy7Y8zPIOj1HZ7r6CTUzB2dPUGaV89VejZiVE"); var user = Tweetinvi.User.GetUserFromScreenName(userName); var lastTweets = Timeline.GetUserTimeline(user.Id, 200).ToArray(); var allTweets = new List <ITweet>(lastTweets); var beforeLast = allTweets; while (lastTweets.Length > 0 && allTweets.Count <= 3200) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200; var timelineRequestParameters = new UserTimelineParameters { // MaxId ensures that we only get tweets that have been posted // BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(user.Id, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } return(allTweets); }
public List <ITweet> GetAllUserTweets(string userHandle) { // Set Auth Auth.SetUserCredentials(_consumerPublic, _consumerSecret, _accessToken, _accessSecret); RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; RateLimit.QueryAwaitingForRateLimit += (sender, args) => { Console.WriteLine($"Query : {args.Query} is awaiting for rate limits!"); }; var lastTweets = Timeline.GetUserTimeline(userHandle, 200).ToArray(); var allTweets = new List <ITweet>(lastTweets); while (lastTweets.Length > 0 && allTweets.Count <= 3200) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var numberOfTweetsToRetrieve = allTweets.Count > 3000 ? 3200 - allTweets.Count : 200; var timelineRequestParameters = new UserTimelineParameters { // MaxId ensures that we only get tweets that have been posted // BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = numberOfTweetsToRetrieve }; lastTweets = Timeline.GetUserTimeline(userHandle, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } return(allTweets); }
public virtual async Task <IEnumerable <Status> > GetUserTimelineAsync(StatusListParams statusListParams) { var userTimelineParam = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = statusListParams.Count ?? 100, IncludeRTS = true }; if (!string.IsNullOrEmpty(statusListParams.MaxId)) { userTimelineParam.MaxId = long.Parse(statusListParams.MaxId) - 1; } var userId = new UserIdentifier(long.Parse(statusListParams.CreatedByUserId)); var tweets = await Auth.ExecuteOperationWithCredentials( this.credentials, () => TimelineAsync.GetUserTimeline(userId, userTimelineParam)); tweets = tweets ?? Enumerable.Empty <ITweet>(); var mapper = new StatusMapper(); return(tweets.Select(x => mapper.Map(x, new Status()))); }
public async Task TweetRandom(string id) { Random rand = new Random(); var p = new UserTimelineParameters(); p.ExcludeReplies = true; p.MaximumNumberOfTweetsToRetrieve = 40; var latestTweets = Timeline.GetUserTimeline(id, p).ToArray(); int random = 0; int count = 0; do { random = rand.Next(latestTweets.Count()); count++; if (count >= 40) break; } while (latestTweets[random].IsRetweet); if(count >= 40) { await ReplyAsync("Failed to get random tweet."); return; } await ReplyAsync(Context.User.Mention + " " + latestTweets[random].Url); }
public IEnumerable <UserModel> GetTimelineForSpecificUser(string username) { var userTimelineParameters = new UserTimelineParameters() { MaximumNumberOfTweetsToRetrieve = 500, }; var tweets = Timeline.GetUserTimeline(username, userTimelineParameters); var listOfUsers = new List <UserModel>(); foreach (var item in tweets) { var user = new UserModel() { ScreenName = item.CreatedBy.ScreenName }; listOfUsers.Add(user); } //var user1 = User.GetUserFromScreenName(username); //user1.CreatedAt return(listOfUsers); }
/// <summary> /// This function takes a given user and downloads the tweets and media (filtered by conditions). /// Conditions: Limited to last 3200 tweets (twitter), does not include retweets, includes replies-to. /// </summary> /// <param name="TwitterUserScreenName"></param> /// <param name="BaseDirectory"></param> /// <returns> /// Status of function: /// 0=Success, /// -1=User not found, /// -99=Unexpected error /// </returns> public int ProcessTwitterUser(string TwitterUserScreenName, string BaseDirectory) { try { /* * Get target user */ // Get user from screen name (https://github.com/linvi/tweetinvi/wiki/Users) var user = User.GetUserFromScreenName(TwitterUserScreenName); if (user == null) { Console.BackgroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine("User not found: {0}", TwitterUserScreenName); Console.ResetColor(); return(-1); } else { Console.WriteLine("Found twitter user {0} (@{1}) having userId={2}", user.Name, user.ScreenName, user.Id); } /* * Get the user timeline (https://github.com/linvi/tweetinvi/wiki/Timelines , https://github.com/linvi/tweetinvi/wiki/Get-All-User-Tweets) */ Console.WriteLine("Scanning @{0} Timeline...", user.ScreenName); int maxAllowedTweets = 3200; // Limit set by Twitter API int pageSize = 200; // Max = 200 var userTimelineParameters = new UserTimelineParameters(); userTimelineParameters.ExcludeReplies = false; userTimelineParameters.MaximumNumberOfTweetsToRetrieve = pageSize; //userTimelineParameters.SinceId = 0; //userTimelineParameters.MaxId = latestTweetId; var tweetPage = Timeline.GetUserTimeline(user.Id, userTimelineParameters).ToArray(); List <ITweet> iTweetList = new List <ITweet>(tweetPage); while ((tweetPage.Length > 0) && (iTweetList.Count <= maxAllowedTweets)) { var oldTweetId = tweetPage.Select(x => x.Id).Min(); userTimelineParameters.MaxId = oldTweetId - 1; userTimelineParameters.MaximumNumberOfTweetsToRetrieve = iTweetList.Count > (maxAllowedTweets - pageSize) ? maxAllowedTweets - iTweetList.Count : pageSize; tweetPage = Timeline.GetUserTimeline(user.Id, userTimelineParameters).ToArray(); iTweetList.AddRange(tweetPage); } /* * Open/Create Twitter User directory for output */ string currentDirectory = BaseDirectory; Console.WriteLine("Creating/Validating output directory..."); FileIO.CheckAndCreateDir(currentDirectory + "/export"); FileIO.CheckAndCreateDir(currentDirectory + "/export/" + user.ScreenName); currentDirectory = currentDirectory + "/export/" + user.ScreenName + "/"; //change current directory FileIO.CheckAndCreateDir(currentDirectory + "tweets"); FileIO.CheckAndCreateDir(currentDirectory + "media"); /* * Output data from Timeline to JSON file */ Console.WriteLine("Write raw data to JSON file..."); File.WriteAllText(currentDirectory + user.ScreenName + "_" + DateTime.Now.ToString("yyMMddhhmmss") + ".RAW.json", iTweetList.ToJson().ToString()); /* * Convert ITweet to TweetItem for later processing */ Console.WriteLine("Exporting tweets..."); List <TweetItem> tweetList = new List <TweetItem>(); foreach (ITweet tweet in iTweetList) { if (tweet.IsRetweet == false) // Don't translate retweets, instead dump record { //Check if already downloaded if (!File.Exists(currentDirectory + "tweets/" + tweet.CreatedAt.ToString("yyMMddhhmmss") + "_" + tweet.Id + ".json")) { TweetItem tweetItem = BuildItem.BuildTweetItem(tweet); // Translate the tweet object File.WriteAllText(currentDirectory + "tweets/" + tweetItem.CreatedAt.ToString("yyMMddhhmmss") + "_" + tweetItem.TweetId + ".json", tweetItem.ToJson().ToString()); // Output to JSON file tweetList.Add(tweetItem); // Add to list } } } Console.WriteLine("Downloading media..."); using (var progress = new ProgressBar()) { int counter = 0; int denominator = tweetList.Count; foreach (TweetItem tweet in tweetList) // Print to console media items { // Console.WriteLine(item.ToString()); if (tweet.mediaItems.Count > 0) { foreach (MediaItem media in tweet.mediaItems) { FileIO.DownloadFile(currentDirectory + "media/", tweet.TweetId + "_" + media.MediaIndex + media.FileType, media.MediaURL); } } progress.Report((double)counter / denominator); counter++; } } Console.WriteLine("Finished downloading for user @{0}", user.ScreenName); return(0); } catch (Exception e) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("ERROR - An error occurred during ProcessTwitterUser({0}, {1})", TwitterUserScreenName, BaseDirectory); Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ResetColor(); return(-99); } }
public static IEnumerator UserTimeline(Client client, UserTimelineParameters parameters, Action <List <Tweet> > callback) { yield return(client.GET <List <Tweet> > (Helper.BuildRESTURL("statuses/user_timeline"), parameters, callback)); }
public List <Tweet> GetMoreTweets(long userId, long maxTweetId, int tweetsToRetrieve) { UserTimelineParameters timelineParameters = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200, IncludeRTS = false, MaxId = maxTweetId }; Task <IEnumerable <ITweet> > userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() => { return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters)); }); RequestsRemaining--; var lastTweets = userTimlineAsync.Result; var latestException = Tweetinvi.ExceptionHandler.GetLastException(); if (latestException != null) { throw new InvalidOperationException ($"{latestException.StatusCode} : {latestException.TwitterDescription}"); } else if (lastTweets == null || !lastTweets.Any()) { Console.WriteLine($"No tweets were returned for {userId}."); return(null); } else { List <Tweet> allTweets = new List <Tweet>(); foreach (var tweet in lastTweets) { var oembedHtml = OEmbedTweet.GetHtml(tweet.Url); RequestsRemaining--; allTweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName, tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id, tweet.FullText, tweet.Url, oembedHtml)); } while (lastTweets.ToArray().Length > 0 && allTweets.Count < tweetsToRetrieve) { if (RequestsRemaining < 10) { Console.WriteLine("Less than 10 requests remaining in window. 15 minute cooldown starts now."); Thread.Sleep(90 * 1000 * 10); // Wait 900,000 milliseconds, i.e. 15 minutes var newRateLimit = Tweetinvi.RateLimit.GetQueryRateLimit("https://api.twitter.com/1.1/statuses/user_timeline.json"); RequestsRemaining = newRateLimit.Remaining; ResetDateTime = newRateLimit.ResetDateTime; } // Max ID set to lowest tweet ID in our collection (i.e. the oldest tweet) minus 1. // This number serves as a point of reference for requesting tweets older than those // Twitter previously returned. long maxTweetID = allTweets.Min(x => x.TweetId) - 1; timelineParameters = new UserTimelineParameters { MaximumNumberOfTweetsToRetrieve = 200, IncludeRTS = false, MaxId = maxTweetID }; userTimlineAsync = Tweetinvi.Sync.ExecuteTaskAsync(() => { return(Tweetinvi.Timeline.GetUserTimeline(userId, timelineParameters)); }); RequestsRemaining--; lastTweets = userTimlineAsync.Result; foreach (var tweet in lastTweets) { var oembedHtml = OEmbedTweet.GetHtml(tweet.Url); RequestsRemaining--; allTweets.Add(new Tweet(tweet.CreatedBy.Name, tweet.CreatedBy.ScreenName, tweet.CreatedBy.Id, tweet.CreatedAt, tweet.Id, tweet.FullText, tweet.Url, oembedHtml)); } } return(allTweets); } }
public Object GetTwitterDataJSON(string userID) { Auth.SetUserCredentials(oAuthConsumerKey, oAuthConsumerSecret, oAuthAccessToken, oAuthAccessSecret); var sentimentInstance = Sentiment.Instance; var lastTweets = Timeline.GetUserTimeline(userID, 200).ToArray(); var allTweets = new List <ITweet>(lastTweets); var beforeLast = allTweets; while (lastTweets.Length > 0 && allTweets.Count <= 500) { var idOfOldestTweet = lastTweets.Select(x => x.Id).Min(); Console.WriteLine($"Oldest Tweet Id = {idOfOldestTweet}"); var timelineRequestParameters = new UserTimelineParameters { // We ensure that we only get tweets that have been posted BEFORE the oldest tweet we received MaxId = idOfOldestTweet - 1, MaximumNumberOfTweetsToRetrieve = allTweets.Count > 480 ? (500 - allTweets.Count) : 200 }; lastTweets = Timeline.GetUserTimeline(userID, timelineRequestParameters).ToArray(); allTweets.AddRange(lastTweets); } var jsonString = allTweets.ToJson(); //Using Json.NET to change json string to an array JArray parsedJson = JArray.Parse(jsonString); SingleTweet[] tweetArr = new SingleTweet[parsedJson.Count()]; string screenName = ""; string userName = ""; int followers = 0; int numTweets = 0; DateTime created = new DateTime(); //If an account was returned if (allTweets != null) { Trace.WriteLine(allTweets); //Loop for each tweet for (int i = 0; i < parsedJson.Count(); i++) { //Initialise tweet object //Making the json tweet an object JObject tweet = JObject.Parse(parsedJson[i].ToString()); //Setting the entity to an object to query against JObject entity = JObject.Parse(parsedJson[i]["entities"].ToString()); JObject retweet = new JObject(); JObject user = JObject.Parse(parsedJson[i]["user"].ToString()); //Setting the hashtags to an array to loop through JArray hashtags = JArray.Parse(entity["hashtags"].ToString()); tweetArr[i] = new SingleTweet { tweetID = i, Content = tweet["text"].ToString(), retweets = (int)tweet["retweet_count"], likes = (int)tweet["favorite_count"], sentiment = sentimentInstance.GetScore(tweet["text"].ToString()).AverageSentimentTokens, PostTime = DateTime.ParseExact(tweet["created_at"].ToString(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture), }; screenName = user["name"].ToString(); userName = user["screen_name"].ToString(); followers = (int)user["followers_count"]; numTweets = (int)user["statuses_count"]; created = DateTime.ParseExact(user["created_at"].ToString(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); //Setting the retweeted status if (parsedJson[i]["retweeted_status"].ToString().GetType() == typeof(string) && parsedJson[i]["retweeted_status"].ToString() != "") { retweet = JObject.Parse(parsedJson[i]["retweeted_status"].ToString()); tweetArr[i].RetweetTime = DateTime.ParseExact(retweet["created_at"].ToString(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); } //Loop through for all the hashtags and add them to the hashtag array for (int j = 0; j < hashtags.Count(); j++) { JObject hashtagContent = JObject.Parse(hashtags[j].ToString()); tweetArr[i].Hashtags.Add(hashtagContent["text"].ToString()); } } } User twitterUser = new User(userName, screenName, followers, numTweets, created, tweetArr); return(twitterUser); }