lib/ayadn/nowplaying.rb in ayadn-1.7.7 vs lib/ayadn/nowplaying.rb in ayadn-1.8.0

- old
+ new

@@ -63,14 +63,14 @@ unless options[:no_url] || store.nil? text_to_post += "\n \n[iTunes Store](#{store['link']})" end abort(Status.canceled) unless STDIN.getch == ("y" || "Y") puts "\n#{Status.yourpost}" - unless store.nil? || options[:no_url] - visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist'] - else + if store.nil? || options[:no_url] visible, track, artwork, artwork_thumb, link, artist = false + else + visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist'] end options = options.dup options[:nowplaying] = true if options[:lastfm] source = 'Last.fm' @@ -128,12 +128,38 @@ get_itunes_store(itunes_url, artist, track) end def get_itunes_store url, artist, track results = JSON.load(CNX.download(URI.escape(url)))['results'] + unless results.empty? || results.nil? - resp = results.select {|obj| obj['artistName'] == artist && obj['trackName'] == track} + results.keep_if do |obj| + unless obj['artistName'].nil? + obj['artistName'].downcase == artist.downcase + end + end + + if results.empty? + return { + 'code' => 404, + 'request' => url + } + end + + if results.length > 1 + resp = results.select {|obj| obj['trackName'].downcase == track.downcase} + else + resp = results + end + + if resp.empty? + return { + 'code' => 404, + 'request' => url + } + end + candidate = resp[0] || results[0] return { 'code' => 200, 'artist' => candidate['artistName'],