Class: Lily
- Inherits:
-
Object
- Object
- Lily
- Defined in:
- lib/lily.rb
Instance Method Summary collapse
- #auth ⇒ Object
- #format_params(params) ⇒ Object
- #get_album_info(album, artist) ⇒ Object
- #get_lily_json ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize(*args) ⇒ Lily
constructor
A new instance of Lily.
- #love ⇒ Object
- #run ⇒ Object
- #scrobble ⇒ Object
- #session_key ⇒ Object
- #token ⇒ Object
- #unlove ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Lily
Returns a new instance of Lily.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/lily.rb', line 10 def initialize(*args) @args = args @keys = { "public" => "238b1080c5a14212261f2c963baaf2db", "private" => "85fbaa6644c31d879c3e004fbddb82bf" } @lily_json = self.get_lily_json @api_url = "http://ws.audioscrobbler.com/2.0/" @token ||= self.token end |
Instance Method Details
#auth ⇒ Object
65 66 67 68 69 70 |
# File 'lib/lily.rb', line 65 def auth lastfmURL = "http://www.last.fm/api/auth/?api_key=#{@keys.public}&token=#{@token}" print "Press Enter once you've given access..." Launchy.open(lastfmURL) STDIN.gets end |
#format_params(params) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lily.rb', line 32 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)) sig_str += key + value.to_s if (key != "format") 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_album_info(album, artist) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lily.rb', line 47 def get_album_info(album, artist) url = "#{@api_url}?method=album.getinfo&api_key=#{@keys.public}&artist=#{URI.encode(artist)}&album=#{URI.encode(album)}&format=json" res = JSON.parse(HTTP.get(url)) begin res.album rescue puts "Album does not exist." end end |
#get_lily_json ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lily.rb', line 21 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
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/lily.rb', line 262 def help puts """ Lily, the Last.fm CLI. Usage: lily init lily help lily (scrobble | love | unlove) song (<song> <artist>) [<album>] lily (love | unlove) album (<album> <artist> | <eponymous album>) lily scrobble album (<album> <artist> | <eponymous album>) [--live] """ end |
#init ⇒ Object
275 276 277 278 279 |
# File 'lib/lily.rb', line 275 def init auth session_key puts "Initialized ~/.lily.json successfully" end |
#love ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/lily.rb', line 154 def love # last.fm/api/show/track.love command = @args.shift if command == "song" track, artist = @args[0], @args[1] if @args[2] then album = @args[2] end # album name is optional body = self.format_params({ "track" => track, "artist" => artist, "method" => "track.love", "sk" => @lily_json.session_key, "api_key" => @keys.public, "format" => "json", "album" => album }) res = HTTP.post(@api_url, :body => body) puts "#{track} by #{artist} has been loved ♥" elsif command == "album" # we've got you covered, eponymous albums if @args[1] == nil then album, artist = @args[0], @args[0] else album, artist = @args[0], @args[1] end album_info = get_album_info(album, artist) album_length = 0 album_info.tracks.track.each do |track| body = self.format_params({ "track" => track.name, "artist" => artist, "method" => "track.love", "sk" => @lily_json.session_key, "api_key" => @keys.public, "format" => "json", "album" => album }) res = HTTP.post(@api_url, :body => body) end puts "#{album} by #{artist} has been loved ♥" else puts "huh" end end |
#run ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/lily.rb', line 250 def run first = @args.shift case first when 'scrobble' then self.scrobble when 'init' then self.init when 'love' then self.love when 'unlove' then self.unlove else self.help end end |
#scrobble ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lily.rb', line 83 def scrobble # last.fm/api/show/track.scrobble command = @args.shift = Time.now.utc.to_i # UTC time since unix epoch in seconds if command == "song" # song track, artist = @args[0], @args[1] if @args[2] then album = @args[2] end # album name is optional body = self.format_params({ "artist" => artist, "method" => "track.scrobble", "sk" => @lily_json.session_key, "timestamp" => , "track" => track, "api_key" => @keys.public, "format" => "json", "album" => album }) res = HTTP.post(@api_url, :body => body) puts "#{track} by #{artist} has been scrobbled." elsif command == "album" # album # we got eponymous albums covered :) if @args[1] == nil then album, artist = @args[0], @args[0] else album, artist = @args[0], @args[1] end album_info = get_album_info(album, artist) album_length = 0 # get full length of album in seconds album_info.tracks.track.each do |track| album_length += track.duration.to_i end puts "Whole album length: #{album_length} seconds." # set time when album should have started (a.k.a. album length ago), or if the '--live' option is specified then just start it right now if @args[-1] == "--live" then time = else time = - album_length end puts "Scrobbling your album starting at " + Time.at(time).utc.strftime("%I:%M:%S %p") + " UTC" # begin scrobbling album_info.tracks.track.each do |track| time += track.duration.to_i # scrobble track body = self.format_params({ "artist" => track.artist.name, "method" => "track.scrobble", "sk" => @lily_json.session_key, "timestamp" => time, "track" => track.name, "api_key" => @keys.public, "format" => "json", "album" => album_info.name }) res = HTTP.post(@api_url, :body => body) end puts "#{album} by #{artist} has been scrobbled." else puts "huh" end end |
#session_key ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lily.rb', line 72 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
58 59 60 61 62 63 |
# File 'lib/lily.rb', line 58 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 |
#unlove ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/lily.rb', line 202 def unlove # last.fm/api/show/track.unlove command = @args.shift if command == "song" track, artist = @args[0], @args[1] if @args[2] then album = @args[2] end # album name is optional body = self.format_params({ "track" => track, "artist" => artist, "method" => "track.unlove", "sk" => @lily_json.session_key, "api_key" => @keys.public, "format" => "json", "album" => album }) res = HTTP.post(@api_url, :body => body) puts "#{track} by #{artist} has been unloved :(" elsif command == "album" # we've got you covered, eponymous albums if @args[1] == nil then album, artist = @args[0], @args[0] else album, artist = @args[0], @args[1] end album_info = get_album_info(album, artist) album_length = 0 album_info.tracks.track.each do |track| body = self.format_params({ "track" => track.name, "artist" => artist, "method" => "track.unlove", "sk" => @lily_json.session_key, "api_key" => @keys.public, "format" => "json", "album" => album }) res = HTTP.post(@api_url, :body => body) end puts "#{album} by #{artist} has been unloved :(" else puts "huh" end end |