get user latest 3000 tweets

Apr 20, 2015 at 11:49 AM
Edited Apr 20, 2015 at 12:09 PM
any code example on how to get the latest 3000 or at least 2000 tweet for authorized user and save it on database ?


also assume after i get this tweets i only want the app to get the newest only , that's mean the app every hour should check if there is new tweets and store that as well , so the 2000 tweet should happen once only when user start using the app ,

any idea of how to achieve that ? code example will help a lot to learn about this

thanks for your help
Coordinator
Apr 21, 2015 at 10:15 AM
Hi,

Please use the following code:
private static ITweet[] GetUserTimelineTweets(string userName, int maxNumberOfTweets)
{
    var tweets = new List<ITweet>();

    var receivedTweets = Timeline.GetUserTimeline(userName, 200).ToArray();
    tweets.AddRange(receivedTweets);
            
    while (tweets.Count < maxNumberOfTweets && receivedTweets.Length == 200)
    {
        var oldestTweet = tweets.Min(x => x.Id);
        var userTimelineParameter = Timeline.CreateUserTimelineRequestParameter(userName);
        userTimelineParameter.MaxId = oldestTweet;
        userTimelineParameter.MaximumNumberOfTweetsToRetrieve = 200;

        receivedTweets = Timeline.GetUserTimeline(userTimelineParameter).ToArray();
        tweets.AddRange(receivedTweets);
    }

    return tweets.Distinct().ToArray();
}
Linvi
Marked as answer by linvi on 4/21/2015 at 4:43 AM
Coordinator
Apr 21, 2015 at 10:19 AM
Also concerning getting latest tweets, I would personally use the UserStream if you have the credentials tokens of the user you want to analyze.
The user stream listens to the event of the currently logged user.

Cheers,
Linvi
Apr 21, 2015 at 12:37 PM
thanks but we can't use userstram because this a web app and will be used by many people so userstream not an option , only REST API will work
Coordinator
Apr 21, 2015 at 12:43 PM
Then the solution you had given is the right one.
You can do the same logic as above but you specify the SinceId parameter that will allow you to not get more tweets than the one you already received.

Linvi
Sep 21, 2015 at 1:11 AM
Dear linvi,

first of all, I really appreciate your work thank for this excellent api.

secondly, I copied the code to apply it in my project ,but I get 2 issues continuously
  1. it's shows an error the following errors
Error 1 No overload for method 'CreateUserTimelineRequestParameter' takes 1 arguments

Error 2 The best overloaded method match for 'Tweetinvi.Timeline.GetUserTimeline(Tweetinvi.Core.Interfaces.Models.IUserIdentifier, int)' has some invalid arguments

Error 3 Argument 1: cannot convert from 'Tweetinvi.Core.Interfaces.Parameters.IUserTimelineParameters' to 'Tweetinvi.Core.Interfaces.Models.IUserIdentifier'

Image
__
2.when I tray to get tweets it gives me this :
__
Image


your assistance is highly appreciated

regards
Coordinator
Sep 21, 2015 at 2:33 AM
Hi,

Thank you, I really appreciate your feedback.

First of all, please note that with the new version of tweetinvi, the code will now be the following:
private static ITweet[] GetUserTimelineTweets(string userName, int maxNumberOfTweets)
{
    var tweets = new List<ITweet>();

    var receivedTweets = Timeline.GetUserTimeline(userName, 200).ToArray();
    tweets.AddRange(receivedTweets);

    while (tweets.Count < maxNumberOfTweets && receivedTweets.Length == 200)
    {
        var oldestTweet = tweets.Min(x => x.Id);
        var userTimelineParameter = Timeline.CreateUserTimelineRequestParameter();
        // OR
        userTimelineParameter = new UserTimelineParameters();

        userTimelineParameter.MaxId = oldestTweet;
        userTimelineParameter.MaximumNumberOfTweetsToRetrieve = 200;

        receivedTweets = Timeline.GetUserTimeline(userName, userTimelineParameter).ToArray();
        tweets.AddRange(receivedTweets);
    }

    return tweets.Distinct().ToArray();
}
Concerning the second issue, would you please let me know what is the InnerException of your AggregateException.
You can find it by selecting 'ViewDetails' and expanding the InnerException.

Cheers,
Linvi
Sep 21, 2015 at 2:04 PM
Dear Linvi,

thank you brother for your quick response.

the fisrt issue thanks to you solved.

and regarding to the second error please see the innerException as requested:

{"Object reference not set to an instance of an object."}

pic :

Image


thanks bro
Coordinator
Sep 21, 2015 at 2:55 PM
Hi,

Thanks this is quite useful, would you please also share the StackTrace of the InnerException please.

Cheers,
Linvi
Sep 21, 2015 at 3:42 PM
hey,

sure ,here you are

at Tweetinvi.WebLogic.TwitterClientHandler.SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
at Tweetinvi.WebLogic.TwitterClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request)
at Tweetinvi.WebLogic.HttpClientWebHelper.<GetHttpResponse>d__2.MoveNext()
Coordinator
Sep 22, 2015 at 12:46 AM
Edited Sep 22, 2015 at 12:46 AM
Hi,

Looking at your code, I can see that you are not setting the credentials after receiving them from the CredentialsCreator.
var userCredentials = CredentialsCreator.GetCredentialsFromVerifierCode("my_code", creds);
Auth.SetCredentials(userCredentials);
I have created a work item to make the error easier to read for the version 0.9.10.0.

https://github.com/linvi/tweetinvi/issues/34

Cheers,
Linvi
Sep 22, 2015 at 1:00 AM
Edited Sep 22, 2015 at 4:13 AM
hhhh, man you rock i love the idea you documented the error so any one else facing the same issue will see it , good job bro,

thanks for your help brother ,worked fine now :)

best wishes.