1

I want to create C# Web API project to i.) get all the status tweets of userID (need help/guidance, as data could be large, so what are my options) ii.) store them to Azure DB (hoping, I can achieve this) iii.) get DB values and display JSON output (hoping, I can achieve this)

I am trying to achieve this by using Streaming API and LinqToTwitter but no luck.

a. StreamingType.Filter : It is not returning desired status object or returns null, if I use StreamingType.Filter , Track == userName and Follow == userID

b. StatusType.User : It returns object if I use StatusType. But I am not supposed to use StatusType.

Can I achieve this using Tweetinvi if not using LinqToTwitter. Please help me achieve this.

Thanks. Appreciate your time.

sravan
  • 93
  • 1
  • 8

2 Answers2

3

Web API is inherently stateless, so a stream won't work, regardless of LINQ to Twitter, any other tool, or writing it yourself. On every request, the stream starts and stops. This doesn't make sense because a stream is intended as a persistent and long running connection. Twitter has guidance on using their stream API and it seems like bad things could happen if you reconnect too many times.

For Web API, you're better off using a Search query:

        var searchResponse =
            await
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == "\"LINQ to Twitter\""
             select search)
            .SingleOrDefaultAsync();

I have a blog post on Working with Timelines with LINQ to Twitter that can help you manage the query via SinceID and MaxID parameters. It's based on the previous version of LINQ to Twitter, but the concepts remain the same. There are demos in the Downloadable Source Code for paging with other query types.

Other Notes:

  • On the TwitterContext instance, you can read the RawResult property if you need the JSON from the query. JSON.NET is very useful here too.
  • On Azure, check out DocumentDB,which has a nice set of APIs, including LINQ.
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
  • Thanks Joe, I will try Search query and let you know if it works. – sravan Jan 22 '15 at 18:08
  • I want to capture only status tweets posted by the user but not the retweets. Will the search query show all tweets or is there any limit? – sravan Jan 22 '15 at 18:33
  • Look at the search operators in the Twitter API docs: https://dev.twitter.com/rest/public/search. There's a from operator that you can use. You'll have to experiment to see how far back a search goes. There's also a StatusType.User query with LINQ to Twitter: http://linqtotwitter.codeplex.com/wikipage?title=Querying%20the%20User%20Timeline&referringTitle=Making%20Status%20Queries%20and%20Calls, which lets you set IncludeMyRetweets to false. For Search, you can do a LINQ to Objects query (after materializing the Search query) to exclude tweets where the Retweet property is null. – Joe Mayo Jan 23 '15 at 03:42
  • b. StatusType.User : It returns object if I use StatusType. But I am not supposed to use StatusType. Meanwhile I will try search – sravan Jan 27 '15 at 15:21
  • @sravan what I meant was a Status query like this: http://linqtotwitter.codeplex.com/wikipage?title=Querying%20the%20User%20Timeline&referringTitle=Making%20Status%20Queries%20and%20Calls – Joe Mayo Jan 27 '15 at 19:26
  • Hi Joe, I used search.Query == "from:@USERID". I see searchResponse.Statuses returns 15 tweets as count by default in Search query (which includes Retweets, Replies). I changed count by setting Count in query as && search.Count == 10), and each tweet returned has Retweeted and IncludeMyReweet has a value of 'false'. I tried to add few to the query: tweet.InReplyToScreenName == null && tweet.ExcludeReplies == true && tweet.IncludeMyRetweet == null. Not sure where I am doing wrong.Can you please post sample code by applying any of these filters in LINQ to objects query. Thanks for all your help. – sravan Feb 11 '15 at 15:24
  • I added code to exclude retweets: `search.Query == "from:@USERID -retweets" //exclude retweets` Then to exclude replies: `var tweetsOnlyList = from status in statusList //Exclude replies where status.InReplyToScreenName == null select new { Text = status.Text, ScreenNameResponse = status.User.ScreenNameResponse, StatusID = status.StatusID }; foreach (var tweet in tweetsOnlyList) { Console.WriteLine("User: {0},\n Tweet: {1},\n maxID : {2}", tweet.ScreenNameResponse,tweet.Text,tweet.StatusID); }` – sravan Feb 12 '15 at 17:07
0

With tweetinvi you can easily do this by just using the following code

var fs = Stream.CreateFilteredStream();
fs.AddFollow(userId);
fs.MatchingTweetReceived += (sender, args) => { Console.WriteLine(args.Tweet); };
fs.StartStreamMatchingAllConditions();
user2465083
  • 595
  • 3
  • 12