LinqToTwitter, Can't Get It To Work

892 Views Asked by At

I have been do this steps as listed here: https://www.c-sharpcorner.com/uploadfile/b7325a/read-data-from-social-networking-feedssharp47tweets-and-save-t/

and here's the code:

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;  
    }
}

The tweets does read however the it would only save the date and time and not the content, any advice? My main aim is to search every couple of minutes if a group has tweeted.

How would I change the search function for this?

Or if there is another easier library as I don't think what I want to do is should be this hard

1

There are 1 best solutions below

0
Joe Mayo On

The article you're referencing is for the older synchronous queries. LINQ to Twitter is now async. Here's how to do a User Timeline query:

    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.");
        }
    }

Notice that the containing method is async and the StatusType.User query has an await. There are more examples in the LINQ to Twitter downloadable source code.