I'VE GOT THE BYTE ON MY SIDE

57005 or alive

A Twitter search client in 10 lines of code with F# and the JSON type provider

May 18, 2013 F# neat

Twitter’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…

http://fssnip.net/iu

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

twitter search results screenshot

Whoa.