Created
March 7, 2011 11:10
-
-
Save james/858388 to your computer and use it in GitHub Desktop.
10 minutes of filthy code to let me know if I'm allowed to like the tune I'm listening
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'net/http' | |
require 'cgi' | |
def track_info(title, artist) | |
JSON.parse(http.get("/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=#{CGI.escape(artist)}&track=#{CGI.escape(title)}&format=json").body) | |
end | |
def http | |
http = Net::HTTP.new("ws.audioscrobbler.com") | |
http | |
end | |
def the_loop | |
@played_tracks ||= [] | |
user_info = JSON.parse(http.get("/2.0/?method=user.getrecenttracks&user=abscond&api_key=b25b959554ed76058ac220b7b2e0a026&format=json").body) | |
current_track = user_info["recenttracks"]["track"][0] | |
if !@played_tracks.include? current_track | |
listeners = track_info(current_track["name"], current_track["artist"]["#text"])["track"]["listeners"] | |
p "#{format_int(listeners)} - #{current_track["artist"]["#text"]}: #{current_track["name"]}" | |
@played_tracks << current_track | |
end | |
sleep 30 | |
the_loop | |
end | |
def format_int(int) | |
int.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse | |
end | |
the_loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment