Nicholas Pike

Like nailing jelly to a wall…

Twitter + Last.fm: A quick ruby script

with 6 comments

There’s all sorts of automated twitter posting now adays - you see it a lot for “new blog posts”, but not once have I seen a Twitter/Last.FM post.

For some reason I wrote this last night (even though there is no free time in my schedule at all lately) - as a quick exercise in using Ruby.

Once the script is run, it will suck down my Last.FM recently loved feed every 5 minutes - if the last song loved has changed, then it posts the name of the song, and the Last.FM url to my Twitter account (so that folks can listen to the song too)

#!/usr/bin/env ruby
# @author:  Nicholas Pike - npike@npike.net
require 'net/http'
require 'rexml/document'

# Twitter account information
TW_USER = 'twitterusername'
TW_PASS = 'twitterpassowrd'
# Last.fm username
LF_USER = "lastfmuseraname"

# DO NOT CHANGE BELOW THIS
TW_URL  = 'http://twitter.com/statuses/update.xml'
LAST_FM_URL = "http://ws.audioscrobbler.com/1.0/user/#{LF_USER}/recentlovedtracks.xml"

# temp variables
last_url = ""
first_run = 1

def postToTwitter(message)
  begin
    url = URI.parse(TW_URL)
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth TW_USER, TW_PASS
    req.set_form_data({'status' => message})
    begin
      res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
      case res
        when Net::HTTPSuccess, Net::HTTPRedirection
          if res.body.empty?
            puts "Twitter is not responding properly"

          else
            puts 'Twitter update succeeded'

          end
        else
          puts 'Twitter update failed for an unknown reason'
          # res.error!

        end
    rescue
      puts $!
      #puts "Twitter update failed - check username/password"

    end
  rescue SocketError
    puts "Twitter is currently unavailable"

  end
end

while true

# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(LAST_FM_URL)).body
doc = REXML::Document.new(xml_data)

if ( doc.elements["recentlovedtracks/track[1]/url”].text != last_url)
   puts “No match”

   last_url = doc.elements["recentlovedtracks/track[1]/url”].text
   last_artist = doc.elements["recentlovedtracks/track[1]/artist”].text
   last_name = doc.elements["recentlovedtracks/track[1]/name”].text 

   message = “Last.FM: #{last_name} - #{last_artist}\n\n#{last_url}”

  # Dont send a twitter message on first run of script
   if (first_run != 1)
     postToTwitter(message)
   end
   first_run = 0
else
  puts “No change”
end
sleep 200
end

Written by npike

March 26th, 2008 at 7:30 am

Posted in Projects

6 Responses to 'Twitter + Last.fm: A quick ruby script'

Subscribe to comments with RSS or TrackBack to 'Twitter + Last.fm: A quick ruby script'.

  1. I think you’ve just tipped the scale and I need to turn off notifications. :-/

    Randy Aldrich

    26 Mar 08 at 9:33 am

  2. awwe - It’s not like it puts an update in the twitter feed every time I “listen” to a song - only when I tag it.

    And besides, this is something made for show/fun.

    npike

    26 Mar 08 at 9:35 am

  3. Your supposed to comment on how different ruby is ;-)

    npike

    26 Mar 08 at 9:36 am

  4. Handy script for Twitter and Lastfm crazy people :)

    Ankit

    1 Nov 08 at 2:50 am

  5. [...] sobie zmodyfikować skrypt w Ruby autorstwa Nicholasa Pike, wyświetlający nasze ulubione piosenki na Twiterze, dodając mu także obsługę naszego [...]

  6. Thanks for sharing!!! I hope You have nothing against it, I modified Your script to make it work on Twitter and blip (a polish twitter equivalent). You can find it here:
    http://www.harv.pl/2008/11/lastfm-na-twitterze-i-blipie/

    Harv

    10 Nov 08 at 10:14 am

Leave a Reply