Poudro's blog
CTO / Data Scientist / Problem Solver - Consultant

@gmasot, or a Twitter Last.fm Mashup

@gmasot, or a Twitter Last.fm Mashup - poudro.com développeur web freelance


I’ve been wanting to experiment with Twitter and Last.fm APIs for a while now. Well, why not put them both together ! That’s how I came up with the bot @gmasot (for Give Me A Suggestion Or Two).



How it works, well go to Twitter, post a tweet ‘@gmasot artist’. It will reply with an @mention to you with the name of a similar artist taken from Last.fm and a link to artist’s page on Last.fm. If you follow @gmasot you will see the reply in your own feed. For now, you can’t put anything else in the tweet you send to the robot. I hope to make the service evolve to connect to your Last.fm user account, allow more text in the tweets you send it, allow for multiple artists and/or multiple replies… (Your twitter feed needs to be public so it can read the @mention, I don’t like automatic follows from a bot, a little spammy I find…)






This is the first time I’ve been playing with these APIs. So to do this I needed to figure out how to “listen” to Twitter, fetch similar artists from Last.fm and then post an answer back to Twitter.


First things first, reading the Twitter feed. This is done by using their “Streaming API” (read more). I love it ! It enables you to open a single connection that stays alive for how ever long you want. It is actually strongly recommended to never close it. It gives you access to a few simple search features called filters (track, locations…). When a new tweet matches your filters it is piped to you via the stream. You can then play with it as you feel. For me, and for now, I apply minimal parsing and fetch a list of similar artists from Last.fm.


The Last.fm API (read more) is real nice. I love Last.fm, I think it’s the coolest social site on the web and that it should get more attention than it does (not that it doesn’t already get some). Anyway, with a simple API key, you can fetch a list of similar artists to the one you request. The returned JSON (or XML) contains loads of information on all the related artists. For gmasot, one parameter that is really useful is the “match”. This returns a number between 0 and 1 of the similarity between the initial artist and the one you are looking at in the reply. By transforming these matches into probabilities and applying a simple Binary Search on the constructed discrete cumulative distribution function, I extract a single result weighted by these matches. The response tweet (in the form of an RT retweet) is then constructed by appending the name of the suggested artist with a bitly’ed version of the last.fm profile url for the suggested artist.


Then, the final step, is to submit the result back to Twitter. This is done through twitteroauth.php that talks to a Twitter App that is connected to the @gmasot account. I really like TwitterOAuth, as far as I can tell, it takes all the pain away but none of the power.


Anyway, enjoy playing with it. Again, it is in very early stages, so there might very well be bugs and crashes and whatnots, it’s all for fun… :)


(Of course, I don’t keep any personal information whatsoever. Once the tweet is parsed it is forgotten by gmasot and me. The only history is in Twitter and I cannot guaranty anything about what Twitter does with the data. No information is sent to Last.fm other than the contents of the tweet, without names. For the time being, it is Last.fm that informs me if there is an artist’s name in there or not.)




Résumé en français





07 Aug 2011