Twitter Local

Check out the local buzz

Most things that happen in the world gets broadcasted on Twitter. Usually you follow people to receive their tweets. There’s also a public twitter feed that any registered client can listen to. This feed can be filtered by location. So in this post I’m going to present an F# script that shows tweets from this feed.

The code is fairly easy to understand if you're familiar with C#. I wrote this some time ago while I was learning F#. And I hope some one begining in F# would find the code useful.

If you are interested in learning more about how F# can be used, check out the F# Advent Calendar for a wide variety of posts this festive time.

Before going any further let me show you what the results look like:

First thing to do is to register a twitter app so that I can consume the API. https://apps.twitter.com/ is the place to register an app. This is very quick and easy process and I can immediately use the API. Registration gives me a few key pieces of information that I require for the rest of the code.

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret

In order to access the twitter API, I need to authenticate with Twitter, using OAuth. This is a convoluted process that is tedious and error prone, but once you painstakingly figure this out, the rest is easy.

The following code shows how to create a streaming web resource. The OAuth is tied to the endpoint URL that would be used, so in order for it to be reusable I pass in the endpoint and the associated parameters, in the GetStreamingWebResource(...) function.

Once the web resource is aquired, I can start listening to the tweets using ReadStreamingResponse(...) function. This is nothing more than looping around , reading in the size of the response, allocating necessary buffers and reading the payload. When I have the text payload, it has to be HTML decoded and then parsed into a JSON object. For JSON object parsing, I use FSharp.Data lib which contains necessary JSON type provider.

Most of the tweets these days contain a link to an image. On a webpage these are automatically downloaded and shown, but no such luck in the console. I check if a tweet contains a URL and if there's an image, then download it.

So I have an image but if I would be so brave, I could convert the image to grayscale, and map the intensities to a character set which roughly represents those intensities. And characters I could handle! oh the glory days of tty.

Image2Ascii(...) takes in an image and does a resize so that I can deal with the text mapping. Here I take into account that text on the terminal is not square. So with some fudging about, I can make a picture look proportional.

For grayscale conversion we use a formula from trusty Wikipedia and the charset for mapping intensities was shamelessly pinched from here

When all this is put together, lo and behold I get a fancy and frenzy Twitter feed reader like this…

tty ascii art

This is what tweeters in Tokyo are talking about.

All the code is on github as an F# script with JSON fragments.