如何使用LinqToTwitter在Hashtag上获取所有推文 [英] How To Get All Tweets on Hashtag using LinqToTwitter

查看:87
本文介绍了如何使用LinqToTwitter在Hashtag上获取所有推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让所有tweet(计数总tweet数量)都属于主题标签.我的功能在这里,如何使用maxID和sinceID获取所有推文.什么是计数"而不是计数"?我不知道.

I'm trying to get all tweets(count total tweet number) belong to hashtag. My function is here, how to I use maxID and sinceID for get all tweets. What is the instead of "count"? 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 { }
                }
            }

        }

推荐答案

下面是一个示例.请记住,MaxID用于当前会话,并防止重新读取您在当前会话中已经处理过的推文. SinceID是您收到的有关该搜索字词的最古老的推文,可帮助您避免重新阅读在先前的会话中为该搜索字词处理过的推文.本质上,您正在创建一个窗口,其中MaxID是要删除的最新推文,而SinceID是您不想读过的最旧的推文.在给定搜索字词的第一次会话中,您将SinceID设置为1,因为您还没有最早的推文.会议结束后,保存SinceID,以免意外读取推文.

Here's an example. Remember that MaxID is for the current session and prevents re-reading tweets you've already processed in the current session. SinceID is the oldest tweet you've ever received for this search term and helps you avoid re-reading tweets that you've already processed for this search term during previous sessions. Essentially, you're creating a window where MaxID is the newest tweet to get next and SinceID is the oldest tweet that you don't want to read past. On the first session for a given search term, you would set SinceID to 1 because you don't have an oldest tweet yet. After the session, save SinceID so that you don't accidentally re-read 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));
    }

这种方法似乎需要很多代码,但实际上可以使您更好地控制搜索.例如您可以检查tweet,并根据tweet的内容(例如CreatedAt)确定要查询多少次.您可以将查询包装在try/catch块中,以在超出速率限制或Twitter出现问题时监视HTTP 429,从而可以记住自己所在的位置并恢复.您还可以监视twitterContext RateLimit属性,以查看是否接近,并提前避免HTTP 429异常.盲目阅读N条推文的任何其他技术都可能迫使您浪费速率限制,并使应用程序的可伸缩性降低.

This approach seems like a lot of code, but really gives you more control over the search. e.g. you can examine tweets and determine how many times to query based on the contents of a tweet (like CreatedAt). You can wrap the query in a try/catch block to watch for HTTP 429 when you've exceeded your rate limit or twitter has a problem, allowing you to remember where you were and resume. You could also monitor twitterContext RateLimit properties to see if you're getting close and avoid an exception for HTTP 429 ahead of time. Any other technique to blindly read N tweets could force you to waste rate-limit and make your application less scalable.

  • 提示:请记住,对于给定的搜索字词,请保存SinceID 保存推文,以免下次再次阅读相同的推文 您可以使用该搜索词进行搜索.
  • Tip: Remember to save SinceID for the given search term, if you're saving tweets, to keep from re-reading the same tweets the next time you do a search with that search term.

有关此机制的详细信息,请阅读《技术指南》中的使用时间线. Twitter文档.

For more info on the mechanics of this, read Working with Timelines in the Twitter docs.

这篇关于如何使用LinqToTwitter在Hashtag上获取所有推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆