Consuming Twitter With F#
April 8, 2014 7 Comments
I set up a meetup for TRINUG’s F#/data analytics SIG to center around consuming and analyzing Tweets. Since Twitter is just JSON, I assumed it would be easy enough to search Tweets for a given subjects in a given time period. How wrong I was. I spent several hours research different ways to consume Twitter to varying degrees of success. My 1st stop was to investigate some of the more common libraries that C# developers use to consume Twitter. Here is my survey of some of the more popular ones:
Twitterizer: No longer maintained
- // Install-Package twitterizer -Version 2.4.2
- // Update-Package Newtonsoft.Json -Reinstall
- open Twitterizer
- type public TwitterProvider() =
- member this.GetTweetsForDateRange(ticker:string, startDate: DateTime, endDate: DateTime) =
- let consumerKey = ConfigurationManager.AppSettings.["consumerKey"]
- let consumerSecret = ConfigurationManager.AppSettings.["consumerSecret"]
- let accessToken = ConfigurationManager.AppSettings.["accessToken"]
- let accessTokenSecret = ConfigurationManager.AppSettings.["accessTokenSecret"]
- let tokens = new OAuthTokens()
- tokens.set_ConsumerKey(consumerKey)
- tokens.set_ConsumerSecret(consumerSecret)
- tokens.set_AccessToken(accessToken)
- tokens.set_AccessTokenSecret(accessTokenSecret)
- let searchOptions = new SearchOptions()
- searchOptions.SinceDate <- startDate
- searchOptions.UntilDate <- endDate
- let results = TwitterSearch.Search(tokens, ticker,searchOptions)
- results.ResponseObject
- |> Seq.map(fun r -> r.CreatedDate, r.Text)
TweetSharp: No longer maintained
- open TweetSharp
- type public TwitterProvider() =
- member this.GetTweetsForDateRange(ticker:string, startDate: DateTime, endDate: DateTime) =
- let consumerKey = ConfigurationManager.AppSettings.["consumerKey"]
- let consumerSecret = ConfigurationManager.AppSettings.["consumerSecret"]
- let accessToken = ConfigurationManager.AppSettings.["accessToken"]
- let accessTokenSecret = ConfigurationManager.AppSettings.["accessTokenSecret"]
- let service = new TwitterService(consumerKey, consumerSecret)
- service.AuthenticateWith(accessToken, accessTokenSecret)
- let searchOptions = new SearchOptions()
- searchOptions.Q <- "IBM%20since%3A2014-03-01&src=typd"
- service.Search(searchOptions).Statuses
- |> Seq.map(fun s -> s.CreatedDate, s.Text)
Note that I did try and add a date range the way the Twitter API instructs, but it still came back with only 20 tweets.
LinqToTwitter: Active but nave to use Linq syntax. Ugh!
Twitterinvi: Active but does not have date range functionality
- open System
- open System.Configuration
- open Tweetinvi
- type public TwitterProvider() =
- member this.GetTodaysTweets(ticker: string) =
- let consumerKey = ConfigurationManager.AppSettings.["consumerKey"]
- let consumerSecret = ConfigurationManager.AppSettings.["consumerSecret"]
- let accessToken = ConfigurationManager.AppSettings.["accessToken"]
- let accessTokenSecret = ConfigurationManager.AppSettings.["accessTokenSecret"]
- TwitterCredentials.SetCredentials(accessToken, accessTokenSecret, consumerKey, consumerSecret)
- let tweets = Search.SearchTweets(ticker);
- tweets |> Seq.map(fun t -> t.CreatedAt, t.RetweetCount)
- member this.GetTweetsForDateRange(ticker: string, startDate: DateTime)=
- let consumerKey = ConfigurationManager.AppSettings.["consumerKey"]
- let consumerSecret = ConfigurationManager.AppSettings.["consumerSecret"]
- let accessToken = ConfigurationManager.AppSettings.["accessToken"]
- let accessTokenSecret = ConfigurationManager.AppSettings.["accessTokenSecret"]
- TwitterCredentials.SetCredentials(accessToken, accessTokenSecret, consumerKey, consumerSecret)
- let searchParameter = Search.GenerateSearchTweetParameter(ticker)
- searchParameter.Until <- startDate;
- let tweets = Search.SearchTweets(searchParameter);
- tweets |> Seq.map(fun t -> t.CreatedAt, t.RetweetCount)
So without an out of the box API to use, I thought about using a Json Type Provider the way Lincoln Atkinson did. The problem is that is example is for V1 of Twitter and V 1.1 uses Oauth. If you run his code, you get
I then thought about a 3rd party API that captures Tweets. I ran across gnip ($500!) and Topsy (no longer accepting new licenses b/c Apple bought them) so I am back to square one.
So finally I thought about rolling my own (with OAuth being the hard part) but I am quickly running out of time to get ready for the SIG and I don’t want to spend the time on only this part.
Why isn’t there a Twitter type provider? I’ll add it to the list….
Could do with a variable type provider so you could construct one at run time with a custom expression, although that would make anything other than desktops troublesome. Could you not use query expressions with Linq2Twitter?
Stuff like this would be interesting too:
type FSharpFeed = TwitterProvider
let fsFeed = FSharpFeed()
fsFeed.Listen |> Observable.filter myRealtimeFilter
I am going to take a look at this: http://taumuon-jabuka.blogspot.co.uk/2013/07/sipping-from-twitter-stream.html
Have a look at Tomas’ effort: https://github.com/tpetricek/Documents/tree/master/Samples/Twitter.API
Pingback: F# Weekly #15, 2014 | Sergey Tihon's Blog
https://github.com/chrismckelt/Lacjam/blob/master/src/Core/Jobs.fs — SendTweetJobHandler
Pingback: 4/19/2014 Article Links | The Puntastic Programmer