Twitter and F#

by Lord Cod3n 15. June 2009 16:27

I am attempting to write an application that uses F# on the server and Silverlight on the client to visualize the Twitter API.  I am currently thinking of tracking trends in data.  I may also put something together to view friends of friends navigation.  The first step was to get approval from Twitter to write the application.  Having this approval raises your Twitter API request rate from 100 per hour to 20,000 per hour.

The first part of the application was exploring the Twitter API with F#.  Here is the external tool I am utilizing to parse the JSON data returned from Twitter

The link below is a free open source JSON parser for .Net.
http://james.newtonking.com/pages/json-net.aspx

I am actually considering consuming the data in xml as I am having some parsing issues with some of the JSON returned.

Lets discuss the file piece by piece.

#light
#r @"g:\Assets\TweetSharp\Newtonsoft.JSON.dll";;

// 0.  #light makes F# case sensitive.  #r brings in the Newtonsoft.JSON.dll so it can be used by the F# interactive compiler.

open System.IO
open System.Net
open Newtonsoft.Json
open Newtonsoft.Json.Linq

//1.  Brings in the various namespace we need to work with our program.
//Declares a function that take a string parameter
   let twitterhttp(url: string) =

//2.  Generates a web request object from the provided url
    let myWebRequest = HttpWebRequest.Create(url)

//2a.  To assign to a mutable variable you must use <- syntax I find this much clearer

//3. Set the method to post this is important because some twitter api methods require you to use

//3a.  GET
    myWebRequest.Method <- "POST"

//4.  Assign your network credentials so you can authenticate, I believe this method may change when they switch over to using only OAuth type Authentication
    myWebRequest.Credentials <- new NetworkCredential("username","password")

//5.  Get the webresponse
    let myWebResponse = myWebRequest.GetResponse()

//6.  Get the stream and apply read its contents
    let myStreamReader = new StreamReader(myWebResponse.GetResponseStream())
    let sResponse = myStreamReader.ReadToEnd()
    myStreamReader.Close()

//7.  A nice feature of F# is last line of the function automatically becomes the return  value
    sResponse

Here is the method without the comments

let twitterhttp(url: string) =
    let myWebRequest = HttpWebRequest.Create(url)
    myWebRequest.Method <- "POST"
    myWebRequest.Credentials <- new NetworkCredential("codenenterp","l3tm3in")
    let myWebResponse = myWebRequest.GetResponse()
    let myStreamReader = new StreamReader(myWebResponse.GetResponseStream())
    let sResponse = myStreamReader.ReadToEnd()
    myStreamReader.Close()
    sResponse

Here is the method using GET for the web request.  Note:  I could just pass this in as a parameter, but i wanted to keep them separate. 

let twitterhttpGet(url: string) =
    let myWebRequest = HttpWebRequest.Create(url)
    myWebRequest.Method <- "GET"
    myWebRequest.Credentials <- new NetworkCredential("username","password")
    let myWebResponse = myWebRequest.GetResponse()
    let myStreamReader = new StreamReader(myWebResponse.GetResponseStream())
    let sResponse = myStreamReader.ReadToEnd()
    myStreamReader.Close()
    sResponse

Here is the modified method.  The variable name : type is not needed in all cases.   I am using it here to make the code more readable.

let twitterhttp2(url: string method:string) =
    let myWebRequest = HttpWebRequest.Create(url)
    myWebRequest.Method <- method
    myWebRequest.Credentials <- new NetworkCredential("username","password")
    let myWebResponse = myWebRequest.GetResponse()
    let myStreamReader = new StreamReader(myWebResponse.GetResponseStream())
    let sResponse = myStreamReader.ReadToEnd()
    myStreamReader.Close()
    sResponse

Here is an example of calling to the Twitter F# functions. 

let twitter2 = twitterhttp(http://search.twitter.com/trends/current.json?q=codenenterp&rpp=100)

This will get you xml returned.  Notice where it is bolded.

let twitter2 = twitterhttp(http://search.twitter.com/trends/current.xml?q=codenenterp&rpp=100)

let twitter2 = twitterhttp(http://search.twitter.com/trends/current.rss?q=codenenterp&rpp=100)

let twitter2 = twitterhttp(http://search.twitter.com/trends/current.atom?q=codenenterp&rpp=100)

I will be providing a library for this in the near future.  You can also do this:

File.WriteAllText(@"C:\codenenterp.json",t8)

which will write out the file.  This is pretty cool when you are using F# interactive compiler to get a feel for what you are working with.

I hope this has been informative and helpful. 

Happy Coding!

Silverlight Samurai

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

William Moore is the lead Software Architect and Technologist for Coden Enterprises. He has more than a decade of software development experience primarily Microsoft Platforms.  William enjoys the full gamit of coding everything from the UI down to the database.

Most comments

Page List