lib/ayadn/nowplaying.rb in ayadn-2.0.4 vs lib/ayadn/nowplaying.rb in ayadn-2.0.5

- old
+ new

@@ -1,11 +1,17 @@ # encoding: utf-8 module Ayadn class NowPlaying - require 'rss' + begin + require 'rss' + rescue LoadError => e + puts "\nAYADN: Error while loading an external resource\n\n" + puts "RUBY: #{e}\n\n" + exit + end def initialize api, view, workers, options = {} @api = api @view = view @workers = workers @@ -19,12 +25,40 @@ @custom_text = nil else @custom_text = "\n \n#{options[:text].join(' ')}" end @affiliate_suffix = "&at=1l3vtb8&ct=ayadn" + @deezer_auth_url = "https://connect.deezer.com/oauth/auth.php?app_id=150971&redirect_uri=http://aya.io/ayadn/deezer.html&response_type=token&perms=basic_access,listening_history,offline_access" end + def deezer options + begin + @deezer_code = if Settings.options[:nowplaying][:deezer].nil? + create_deezer_user() + else + if Settings.options[:nowplaying][:deezer][:code].nil? + create_deezer_user() + else + Settings.options[:nowplaying][:deezer][:code] + end + end + @deezer_user_url = "http://api.deezer.com/user/me" + @deezer_token_suffix = "?access_token=#{@deezer_code}" + @status.fetching_from('Deezer') + req = "#{@deezer_user_url}/history#{@deezer_token_suffix}" + res = JSON.parse(CNX.download(req)) + res["data"].sort_by! { |obj| obj["timestamp"] } + candidate = res["data"].last + maker = Struct.new(:artist, :album, :track) + itunes = maker.new(candidate["artist"]["name"], candidate["album"]["title"], candidate["title"]) + post_itunes(options, itunes) + rescue => e + @status.wtf + Errors.global_error({error: e, caller: caller, data: [store, options]}) + end + end + def lastfm options begin user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user() @status.fetching_from('Last.fm') artist, track = get_lastfm_track_infos(user) @@ -57,29 +91,33 @@ if el.length == 0 @status.empty_fields exit end end - @status.itunes_store - store = [] - unless options['no_url'] - store = itunes_istore_request(itunes) - if store['code'] == 404 && itunes.artist =~ /(and)/ - itunes.artist.gsub!('and', '&') - store = itunes_istore_request(itunes) - end - end - text_to_post = "#{@hashtag}\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’#{@custom_text}" - post_nowplaying(text_to_post, store, options) + post_itunes(options, itunes) rescue => e @status.wtf Errors.global_error({error: e, caller: caller, data: [itunes, store, options]}) end end private + def post_itunes options, itunes + @status.itunes_store + store = [] + unless options['no_url'] + store = itunes_istore_request(itunes) + if store['code'] == 404 && itunes.artist =~ /(and)/ + itunes.artist.gsub!('and', '&') + store = itunes_istore_request(itunes) + end + end + text_to_post = "#{@hashtag}\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’#{@custom_text}" + post_nowplaying(text_to_post, store, options) + end + def get_lastfm_track_infos user begin url = "http://ws.audioscrobbler.com/2.0/user/#{user}/recenttracks.rss" feed = RSS::Parser.parse(CNX.download(url)) lfm = feed.items[0].title.split(' – ') @@ -159,13 +197,32 @@ @status.canceled exit end end + def ask_deezer_user + @status.info("please", "open this link to authorize Deezer", "yellow") + @status.say { @workers.thor.say_status :link, @deezer_auth_url, :green } + @status.info("next", "paste the authorization code here", "cyan") + print "> " + begin + STDIN.gets.chomp! + rescue Interrupt + @status.canceled + exit + end + end + def create_lastfm_user Settings.options[:nowplaying][:lastfm] = ask_lastfm_user() Settings.save_config return Settings.options[:nowplaying][:lastfm] + end + + def create_deezer_user + Settings.options[:nowplaying][:deezer] = {code: ask_deezer_user()} + Settings.save_config + return Settings.options[:nowplaying][:deezer][:code] end def itunes_istore_request itunes itunes_url = "https://itunes.apple.com/search?term=#{itunes.artist}&term=#{itunes.track}&term=#{itunes.album}&media=music&entity=musicTrack" get_itunes_store(itunes_url, itunes.artist, itunes.track)