Skip to content

Instantly share code, notes, and snippets.

@statianzo
Created May 17, 2011 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statianzo/977704 to your computer and use it in GitHub Desktop.
Save statianzo/977704 to your computer and use it in GitHub Desktop.
Scrobbles your latest last.fm song to twitter
require 'open-uri'
require 'xmlsimple'
require 'yaml'
config = YAML::load(File.open('musicmonday.yaml'))
lastfm_username = config['settings']['lastfm']['username']
lastfm_api = config['settings']['lastfm']['api']
recent_tracks_url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{lastfm_username}&api_key=#{lastfm_api}&limit=1"
twitter_url = "http://twitter.com/statuses/update.xml"
twitter_username = config['settings']['twitter']['username']
twitter_password = config['settings']['twitter']['password']
open recent_tracks_url do |body|
data = XmlSimple.xml_in body.read
recenttracks = data['recenttracks'][0]
track = recenttracks['track'][0]
@artist = track['artist'][0]['content']
@name = track['name'][0]
end
text = URI.escape "If today were Monday, I'd tag it. \"#{@name}\" by #{@artist} #lastfm (musicmonday.rb test)"
result = %x[curl -s -S -u #{twitter_username}:#{twitter_password} -d status="#{text}" #{twitter_url}]
settings:
lastfm:
username:
api:
twitter:
username:
password:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment