A Twitter search client in 10 lines of code with F# and the JSON type provider
May 18, 2013Twitter’s basic search API is pretty simple to use, and returns JSON back to you with tweets matching your search parameters.
JSON, eh? “There’s an app a type provider for that.” I wonder if I could cook something up…
type T = FSharp.Data.JsonProvider<"http://search.twitter.com/search.json?q=%23fsharp&lang=en&rpp=1&page=1">
let tweets (tag : string) (since : System.DateTime) =
let enc = System.Web.HttpUtility.UrlEncode : string -> string
let rec page n =
let data = T.Load(sprintf "http://search.twitter.com/search.json?q=%s&rpp=100&page=%d&since=%4d-%02d-%02d" (enc tag) n since.Year since.Month since.Day)
seq{
yield! data.Results
if not (Seq.isEmpty data.Results) then yield! page (n + 1)
}
page 1
Whoa.