How to get all tweets in a Hashtag using LinqToTwitter

I am trying to get all the tweets (total number of tweets) owned by the hashtag. My function here is how to use maxID and sinceID to get all tweets. What is an account? I dont know.

if (maxid != null) { var searchResponse = await (from search in ctx.Search where search.Type == SearchType.Search && search.Query == "#karne" && search.Count == Convert.ToInt32(count) select search) .SingleOrDefaultAsync(); maxid = Convert.ToString(searchResponse.SearchMetaData.MaxID); foreach (var tweet in searchResponse.Statuses) { try { ResultSearch.Add(new KeyValuePair<String, String>(tweet.ID.ToString(), tweet.Text)); tweetcount++; } catch {} } while (maxid != null && tweetcount < Convert.ToInt32(count)) { maxid = Convert.ToString(searchResponse.SearchMetaData.MaxID); searchResponse = await (from search in ctx.Search where search.Type == SearchType.Search && search.Query == "#karne" && search.Count == Convert.ToInt32(count) && search.MaxID == Convert.ToUInt64(maxid) select search) .SingleOrDefaultAsync(); foreach (var tweet in searchResponse.Statuses) { try { ResultSearch.Add(new KeyValuePair<String, String>(tweet.ID.ToString(), tweet.Text)); tweetcount++; } catch { } } } } 
+5
source share
3 answers

Here is an example. Remember that MaxID is for the current session and prevents the re-reading of tweets that you have already processed in the current session. SinceID is the oldest tweet you have ever received for this search query, and helps to avoid re-reading tweets that you have already processed for this search term in previous sessions. Essentially, you create a window in which MaxID is the newest tweet to get, and SinceID is the oldest tweet that you don't want to read. In the first session for this search query, you must set SinceID to 1 because you do not yet have a major tweet. After the session, save SinceID so that you do not accidentally re-read the tweets.

  static async Task DoPagedSearchAsync(TwitterContext twitterCtx) { const int MaxSearchEntriesToReturn = 100; string searchTerm = "twitter"; // oldest id you already have for this search term ulong sinceID = 1; // used after the first query to track current session ulong maxID; var combinedSearchResults = new List<Status>(); List<Status> searchResponse = await (from search in twitterCtx.Search where search.Type == SearchType.Search && search.Query == searchTerm && search.Count == MaxSearchEntriesToReturn && search.SinceID == sinceID select search.Statuses) .SingleOrDefaultAsync(); combinedSearchResults.AddRange(searchResponse); ulong previousMaxID = ulong.MaxValue; do { // one less than the newest id you've just queried maxID = searchResponse.Min(status => status.StatusID) - 1; Debug.Assert(maxID < previousMaxID); previousMaxID = maxID; searchResponse = await (from search in twitterCtx.Search where search.Type == SearchType.Search && search.Query == searchTerm && search.Count == MaxSearchEntriesToReturn && search.MaxID == maxID && search.SinceID == sinceID select search.Statuses) .SingleOrDefaultAsync(); combinedSearchResults.AddRange(searchResponse); } while (searchResponse.Any()); combinedSearchResults.ForEach(tweet => Console.WriteLine( "\n User: {0} ({1})\n Tweet: {2}", tweet.User.ScreenNameResponse, tweet.User.UserIDResponse, tweet.Text)); } 

This approach looks like a lot of code, but actually gives you more control over the search. for example, you can examine tweets and determine how many times to request based on the contents of the tweet (e.g. CreatedAt ). You can wrap the request in a try/catch to see HTTP 429 when you have exceeded the speed limit, or twitter has a problem that allows you to remember where you were and what was. You can also track twitterContext RateLimit properties to see if you are approaching and avoid an exception for HTTP 429 ahead of time. Any other way to blindly read N tweets can make you give up speed limits and make the application less scalable.

  • Tip: Remember to save SinceID for this search query, if you save tweets so you donโ€™t re-read the same tweets the next time you search with this search query.

For more information on the mechanism behind this, read Working with Timeline in Twitter docs.

+9
source

Just wanted to say that with Tweetinvi it โ€‹โ€‹will be as simple as:

 // If you want to handle RateLimits RateLimit.RateLimitTrackerOption = RateLimitTrackerOptions.TrackAndAwait; var tweets = Search.SearchTweets(new TweetSearchParameters("#karne") { MaximumNumberOfResults = 10000 MaxId = 243982 // If you want to start at a specific point }); 
0
source

TweetInvi is even easier. All you have to do is:

 var matchingTweets = Search.SearchTweets("#AutismAwareness"); 
0
source

Source: https://habr.com/ru/post/1241208/


All Articles