Skip to content

Instantly share code, notes, and snippets.

@pelf
Created November 18, 2011 10:57
Show Gist options
  • Save pelf/1376166 to your computer and use it in GitHub Desktop.
Save pelf/1376166 to your computer and use it in GitHub Desktop.
Last.fm loved tracks -> html with youtube links
require 'rubygems'
require 'util'
module Lastfm
class Artist
attr_accessor :name
def initialize(name)
@name = name
end
def to_s
@name
end
end
class Track
attr_accessor :title, :artist
def initialize(artist, title)
@artist = artist
@title = title
end
def to_s
"#{@artist} - #{@title}"
end
end
# represents a loved track
# only has a title (artist - track) and a date
class LovedTrack
attr_accessor :title, :date
def initialize(title, date)
@date = date
@title = title
end
def to_s
"#{@title}: #{@date}"
end
end
class User
def initialize(username='dryginlogic')
@username = username
end
# http://ws.audioscrobbler.com/2.0/user/username/lovedtracks.rss
def get_loved_tracks
url = "http://ws.audioscrobbler.com/2.0/user/#{@username}/lovedtracks.rss"
xml = Util.xml(Util.fetch(url))
begin
return xml['channel'][0]['item'].map {|item| LovedTrack.new(item['title'][0], item['pubDate'][0]) }
rescue # something went wrong, try to return only the xml of the items
return xml['channel'][0]['item']
end
end
end
# stores loved tracks in a text file
# useful for avoiding double posts on the blog
class Database
attr_accessor :db_name, :database
def initialize(db='lovedtracks.txt')
@db_name = db
@database = Hash.new
File.open(db, 'r') do |dbf|
while line = dbf.gets
@database[line.strip] = true
end
end
return self
end
# returns false if the track was already in the db
# return true if it is inserted
def put(track)
# check if it's already in the db
if check(track)
return false
else # store it if it's not
File.open(@db_name, 'a') do |dbf|
dbf.puts "#{track}\n"
end
@database[track] = true # also put in the hash
return true
end
end
def check(track)
return !@database[track].nil?
end
end
end
require 'lastfm'
require 'youtube'
# fetches my loved tracks from last.fm
# and outputs a list of the recent ones in html
# with links to youtube videos
# removing duplicates from previous posts
# and storing the new ones for the future
me = Lastfm::User.new('dryginlogic')
db = Lastfm::Database.new
me.get_loved_tracks.each do |track|
if db.put(track.title) # returns true when track wasn't already in the db
video = Youtube::Video.search(track.title).first
puts "<li><a href=\"#{video}\">#{track.title}</a></li>"
end
end
require 'net/http'
require 'xmlsimple'
require 'uri'
class Util
# fetch url body content
def self.fetch(url)
Net::HTTP.get_response(URI.parse(url)).body
end
# parse xml from string
def self.xml(xml)
XmlSimple.xml_in(xml)
end
# parse rss from xml simple object
def self.rss(xml)
end
end
require 'rubygems'
require 'util'
module Youtube
class Video
attr_accessor :url
def initialize(url)
@url = url
end
def self.search(q)
xml = Util.xml(Util.fetch("http://gdata.youtube.com/feeds/api/videos?q=#{URI.escape(q)}"))
begin
return xml['entry'].map{|e| e['link'][0]['href']}
rescue
return xml
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment