Class: Lily
- Inherits:
-
Object
- Object
- Lily
- Defined in:
- lib/lily.rb
Instance Method Summary collapse
- #auth ⇒ Object
- #format_params(params) ⇒ Object
- #get_lily_json ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize(*args) ⇒ Lily
constructor
A new instance of Lily.
- #run ⇒ Object
- #scrobble ⇒ Object
- #session_key ⇒ Object
- #token ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Lily
Returns a new instance of Lily.
10 11 12 13 14 15 16 17 18 |
# File 'lib/lily.rb', line 10 def initialize(*args) @args = args @keys = { "public": "238b1080c5a14212261f2c963baaf2db", "private": "85fbaa6644c31d879c3e004fbddb82bf" } @lily_json = self.get_lily_json @token ||= self.token end |
Instance Method Details
#auth ⇒ Object
55 56 57 58 59 |
# File 'lib/lily.rb', line 55 def auth lastfmURL = "http://www.last.fm/api/auth/?api_key=#{@keys.public}&token=#{@token}" puts "Press Enter once you've given access." Launchy.open(lastfmURL) end |
#format_params(params) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lily.rb', line 31 def format_params(params) path_arr = [] sig_str = "" params = params.sort_by { |key, value| key } params.each do |key, value| path_arr.push("#{key}=" + URI.encode(value.to_s)) if (key != "format") sig_str += key + value.to_s end end sig_str += @keys.private sig = Digest::MD5.hexdigest(sig_str) path_arr.push("api_sig=#{sig}") path = path_arr.join("&") return path end |
#get_lily_json ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lily.rb', line 20 def get_lily_json begin # Make this more concise later content = File.read(ENV['HOME'] + '/.lily.json') lily_json = JSON.parse(content) # returns {sk, signature} rescue puts "~/.lily.json appears empty" end end |
#help ⇒ Object
104 105 106 |
# File 'lib/lily.rb', line 104 def help puts "no" end |
#init ⇒ Object
108 109 110 111 112 113 |
# File 'lib/lily.rb', line 108 def init auth STDIN.gets # wow, hack session_key puts "Initialized successfully" end |
#run ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/lily.rb', line 94 def run first = @args.shift case first when 'scrobble' then self.scrobble when 'init' then self.init else self.help end end |
#scrobble ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lily.rb', line 72 def scrobble # http://www.last.fm/api/show/track.scrobble track, artist = @args[0], @args[1] url = "http://ws.audioscrobbler.com/2.0/" = Time.now.utc.to_i # UTC time since unix epoch in seconds body = self.format_params({ "artist" => artist, "method" => "track.scrobble", "sk" => @lily_json.session_key, "timestamp" => , "track" => track, "api_key" => @keys.public, "format" => "json" }) res = HTTP.post(url, :body => body) puts "#{track} by #{artist} has been scrobbled" end |
#session_key ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lily.rb', line 61 def session_key sig = Digest::MD5.hexdigest("api_key#{@keys.public}methodauth.getSessiontoken#{@token}#{@keys.private}") session = "http://ws.audioscrobbler.com/2.0/?method=auth.getSession&api_key=#{@keys.public}&api_sig=#{sig}&token=#{@token}&format=json" res = JSON.parse(HTTP.get(session)) sk = res.session['key'] File.open(ENV['HOME'] + '/.lily.json', 'w+') do |file| file.write(JSON.pretty_generate({"session_key" => sk})) end end |
#token ⇒ Object
48 49 50 51 52 53 |
# File 'lib/lily.rb', line 48 def token url = "http://ws.audioscrobbler.com/2.0/?method=auth.getToken&api_key=#{@keys.public}&api_sig=#{@keys.private}&format=json" res = JSON.parse(HTTP.get(url)) # return res.token res.token end |