Class: Ayadn::NowPlaying
- Defined in:
- lib/ayadn/ids.rb,
lib/ayadn/nowplaying.rb
Constant Summary collapse
- AFFILIATE_SUFFIX =
"&at=1l3vtb8&ct=ayadn"
- DEEZER_APP_ID =
"150971"
- DEEZER_AUTH_URL =
"http://aya.io/ayadn/deezer.html"
Instance Method Summary collapse
- #deezer(options) ⇒ Object
-
#initialize(api, view, workers, options = {}) ⇒ NowPlaying
constructor
A new instance of NowPlaying.
- #itunes(options) ⇒ Object
- #lastfm(options) ⇒ Object
Constructor Details
#initialize(api, view, workers, options = {}) ⇒ NowPlaying
Returns a new instance of NowPlaying.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ayadn/nowplaying.rb', line 22 def initialize api, view, workers, = {} @api = api @view = view @workers = workers @status = Status.new unless [:hashtag] @hashtag = "#nowplaying" else @hashtag = "##{[:hashtag].join()}" end unless [:text] @custom_text = nil else @custom_text = "\n \n#{[:text].join(' ')}" end @auth_url = "https://connect.deezer.com/oauth/auth.php?app_id=#{DEEZER_APP_ID}&redirect_uri=#{DEEZER_AUTH_URL}&response_type=token&perms=basic_access,listening_history,offline_access" end |
Instance Method Details
#deezer(options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ayadn/nowplaying.rb', line 40 def deezer begin @deezer_code = if Settings.[:nowplaying][:deezer].nil? create_deezer_user() else if Settings.[:nowplaying][:deezer][:code].nil? create_deezer_user() else Settings.[: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(, itunes) rescue => e @status.wtf Errors.global_error({error: e, caller: caller, data: [store, ]}) end end |
#itunes(options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ayadn/nowplaying.rb', line 89 def itunes begin unless Settings.config[:platform] =~ /darwin/ @status.error_only_osx exit end @status.fetching_from('iTunes') itunes = get_itunes_track_infos() itunes.each do |el| if el.nil? || el.length == 0 @status.empty_fields exit end end post_itunes(, itunes) rescue => e @status.wtf Errors.global_error({error: e, caller: caller, data: [itunes, ]}) end end |
#lastfm(options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ayadn/nowplaying.rb', line 67 def lastfm begin user = Settings.[:nowplaying][:lastfm] || create_lastfm_user() @status.fetching_from('Last.fm') artist, track = get_lastfm_track_infos(user) @status.itunes_store store = [] unless ['no_url'] store = lastfm_istore_request(artist, track) if store['code'] == 404 && artist =~ /(and)/ artist.gsub!('and', '&') store = lastfm_istore_request(artist, track) end end text_to_post = "#{@hashtag}\n \nTitle: ‘#{track}’\nArtist: #{artist}#{@custom_text}" (text_to_post, store, ) rescue => e @status.wtf Errors.global_error({error: e, caller: caller, data: [store, ]}) end end |