LinqToTwitter,无法正常工作

时间:2018-07-22 23:21:37

标签: c# twitter linq-to-twitter

我已经按照以下步骤执行了以下步骤: https://www.c-sharpcorner.com/uploadfile/b7325a/read-data-from-social-networking-feedssharp47tweets-and-save-t/

这是代码:

public static List<Status> GetTwitterFeeds()  
{  
    string screenname = "csharpcorner";  

    var auth = new SingleUserAuthorizer  
    {  

        CredentialStore = new InMemoryCredentialStore()  
        {  

            ConsumerKey = ConfigurationManager.AppSettings["consumerkey"],  
            ConsumerSecret = ConfigurationManager.AppSettings["consumersecret"],  
            OAuthToken = ConfigurationManager.AppSettings["accessToken"],  
            OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]  

        }  

    };  
    var twitterCtx = new TwitterContext(auth);  
    var ownTweets = new List<Status>();  

    ulong maxId = 0;  
    bool flag = true;  
    var statusResponse = new List<Status>();  
    statusResponse = (from tweet in twitterCtx.Status  
    where tweet.Type == StatusType.User  
    && tweet.ScreenName == screenname  
    && tweet.Count == 200  
    select tweet).ToList();  

    if (statusResponse.Count > 0)  
    {  
        maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;  
        ownTweets.AddRange(statusResponse);  

    }  
    do  
    {  
        int rateLimitStatus = twitterCtx.RateLimitRemaining;  
        if (rateLimitStatus != 0)  
        {  

            statusResponse = (from tweet in twitterCtx.Status  
            where tweet.Type == StatusType.User  
            && tweet.ScreenName == screenname  
            && tweet.MaxID == maxId  
            && tweet.Count == 200  
            select tweet).ToList();  

            if (statusResponse.Count != 0)  
            {  
                maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;  
                ownTweets.AddRange(statusResponse);  
            }  
            else  
            {  
                flag = false;  
            }  
        }  
        else  
        {  
            flag = false;  
        }  
        } while (flag);  

                return ownTweets;  
    }
}

这些推文确实会读取,但是只会保存日期和时间,而不是内容,有什么建议吗? 我的主要目的是每隔几分钟搜索一次发推文。

该如何更改搜索功能?

或者如果还有另一个更简单的库,因为我不认为我想做的那么难

1 个答案:

答案 0 :(得分:1)

您引用的文章适用于较旧的同步查询。 LINQ to Twitter现在为async。以下是执行用户时间轴查询的方法:

    static async Task RunUserTimelineQueryAsync(TwitterContext twitterCtx)
    {
        //List<Status> tweets =
        //    await
        //    (from tweet in twitterCtx.Status
        //     where tweet.Type == StatusType.User &&
        //           tweet.ScreenName == "JoeMayo"
        //     select tweet)
        //    .ToListAsync();

        const int MaxTweetsToReturn = 200;
        const int MaxTotalResults = 100;

        // 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> tweets =
            await
            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.User &&
                   tweet.ScreenName == "JoeMayo" &&
                   tweet.Count == MaxTweetsToReturn &&
                   tweet.SinceID == sinceID &&
                   tweet.TweetMode == TweetMode.Extended
             select tweet)
            .ToListAsync();

        if (tweets != null)
        {
            combinedSearchResults.AddRange(tweets);
            ulong previousMaxID = ulong.MaxValue;
            do
            {
                // one less than the newest id you've just queried
                maxID = tweets.Min(status => status.StatusID) - 1;

                Debug.Assert(maxID < previousMaxID);
                previousMaxID = maxID;

                tweets =
                    await
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.User &&
                           tweet.ScreenName == "JoeMayo" &&
                           tweet.Count == MaxTweetsToReturn &&
                           tweet.MaxID == maxID &&
                           tweet.SinceID == sinceID &&
                           tweet.TweetMode == TweetMode.Extended
                     select tweet)
                    .ToListAsync();

                combinedSearchResults.AddRange(tweets);

            } while (tweets.Any() && combinedSearchResults.Count < MaxTotalResults);

            PrintTweetsResults(tweets);
        }
        else
        {
            Console.WriteLine("No entries found.");
        }
    }

请注意,包含方法为async,而StatusType.User查询具有awaitLINQ to Twitter downloadable source code中有更多示例。