Class: Ayadn::NowPlaying
- Inherits:
-
Object
- Object
- Ayadn::NowPlaying
- Defined in:
- lib/ayadn/nowplaying.rb
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
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ayadn/nowplaying.rb', line 14 def initialize api, view, workers, = {} @api = api @view = view @workers = workers @status = Status.new unless [:hashtag] @hashtag = "#nowplaying" else @hashtag = "##{options[:hashtag].join()}" end unless [:text] @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 |
Instance Method Details
#deezer(options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ayadn/nowplaying.rb', line 33 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ayadn/nowplaying.rb', line 82 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.length == 0 @status.empty_fields exit end end post_itunes(, itunes) rescue => e @status.wtf Errors.global_error({error: e, caller: caller, data: [itunes, store, ]}) end end |
#lastfm(options) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ayadn/nowplaying.rb', line 60 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 |