sravan
sravan

Reputation: 93

Twitter streaming api with linq2twitter / tweetinvi

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.

Upvotes: 1

Views: 2036

Answers (2)

user2465083
user2465083

Reputation: 595

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();

Upvotes: 0

Joe Mayo
Joe Mayo

Reputation: 7513

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.

Upvotes: 3

Related Questions