Class: Ayadn::NowPlaying

Inherits:
Object show all
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

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, options = {}
  @api = api
  @view = view
  @workers = workers
  @status = Status.new
  unless options[:hashtag]
    @hashtag = "#nowplaying"
  else
    @hashtag = "##{options[:hashtag].join()}"
  end
  unless options[:text]
    @custom_text = nil
  else
    @custom_text = "\n \n#{options[: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 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

#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 options
  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(options, itunes)
  rescue => e
    @status.wtf
    Errors.global_error({error: e, caller: caller, data: [itunes, options]})
  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 options
  begin
    user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user()
    @status.fetching_from('Last.fm')
    artist, track = get_lastfm_track_infos(user)
    @status.itunes_store
    store = []
    unless options['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}"
    post_nowplaying(text_to_post, store, options)
  rescue => e
    @status.wtf
    Errors.global_error({error: e, caller: caller, data: [store, options]})
  end
end