Module: Rdio

Extended by:
GLI::App
Defined in:
lib/rdio.rb,
lib/rdio/version.rb,
lib/rdio/desktop_bridge.rb

Defined Under Namespace

Classes: DesktopBridge

Constant Summary collapse

VERSION =
'1.2.2'

Class Method Summary collapse

Class Method Details

.add_friend(vanity_or_email) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rdio.rb', line 129

def self.add_friend(vanity_or_email)
  if vanity_or_email.include? "@"
    data = api.call 'findUser', { :email => vanity_or_email }
  else
    data = api.call 'findUser', { :vanityName => vanity_or_email }
  end

  unless data['result']
    return "Could not find user #{vanity_or_email}."
  end

  data = api.call 'addFriend', :user => data['result']['key']
  data['result'] ? "You are now friends with #{vanity_or_email}." :
    "Rdio said you were unable to become friends with #{vanity_or_email}."
end

.add_to_collection(tracks) ⇒ Object



123
124
125
126
127
# File 'lib/rdio.rb', line 123

def self.add_to_collection(tracks)
  tracks = Array(tracks)

  api.call 'addToCollection', { :keys => tracks.join(',') }
end

.apiObject



22
23
24
25
26
27
# File 'lib/rdio.rb', line 22

def self.api
  token = @access_token ? [@access_token, @access_secret] : nil
  @api ||= Api.new \
    [@consumer_key, @consumer_secret],
    token
end

.authorize_apiObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rdio.rb', line 29

def self.authorize_api
  url = api.begin_authentication('oob')
  ask "Copy the four digit code from your browser. [ENTER] to continue. "
  Launchy.open url
  code = ask 'Code: '
  @access_token, @access_secret = @api.complete_authentication(code)

  write_config

  say "You're all set. see `rdio help` for usage"
end

.bridgeObject



18
19
20
# File 'lib/rdio.rb', line 18

def self.bridge
  @bridge ||= Rdio::DesktopBridge.new
end

.current_album_track_keysObject



116
117
118
119
120
121
# File 'lib/rdio.rb', line 116

def self.current_album_track_keys
  current_album_url = current_user['lastSongPlayed']['albumUrl']
  data = api.call 'getObjectFromUrl', { :url => current_album_url }

  data['result']['trackKeys']
end

.current_track_keyObject



110
111
112
113
114
# File 'lib/rdio.rb', line 110

def self.current_track_key
  data = api.call 'getObjectFromUrl', { :url => bridge.current_url }

  data['result']['key']
end

.current_userObject



106
107
108
# File 'lib/rdio.rb', line 106

def self.current_user
  @current_user ||= api.call('currentUser', :extras => 'lastSongPlayed')['result']
end

.lyrics_for(artist, title) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rdio.rb', line 41

def self.lyrics_for(artist, title)
  uri = URI('http://makeitpersonal.co/lyrics')
  params = { :artist => artist.gsub(/[<>]/, ' '), :title => title.gsub(/[<>]/, ' ') }
  uri.query = URI.encode_www_form(params)

  res = Net::HTTP.get_response(uri)
  return res.body
end

.rdio_configObject



90
91
92
93
94
95
96
97
# File 'lib/rdio.rb', line 90

def self.rdio_config
  {
   :consumer_key    => @consumer_key,
   :consumer_secret => @consumer_secret,
   :access_token    => @access_token,
   :access_secret   => @access_secret
  }
end

.shows_for(artist, count) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rdio.rb', line 50

def self.shows_for(artist, count)
  uri = URI('http://ws.audioscrobbler.com/2.0')
  params = { :method => 'artist.getEvents', :artist => artist, :limit => count, :format => 'json', :autocorrect => 1, :api_key => '3c3e4b39c2aedcac5d745c70a898ee76' }
  uri.query = URI.encode_www_form(params)
  res = Net::HTTP.get_response(uri)

  json = JSON.parse(res.body)
  return "Sorry, I’ve never heard of #{artist}" if json['error'] == 6

  events = json['events']['event']
  events = [events] if events.is_a?(Hash)
  return "No upcoming events for #{artist}" if !events

  corrected_artist_name = json['events']['@attr']['artist']

  cities = []
  countries = []
  events.each do |event|
    cities << event['venue']['location']['city']
    countries << event['venue']['location']['country']
  end

  longest_city = cities.inject { |a, b| a.length > b.length ? a : b }
  longest_country = countries.inject { |a, b| a.length > b.length ? a : b }

  events.map! do |event|
    location = event['venue']['location']

    city = location['city']
    city_spaces = (0..longest_city.length - city.length).map{' '}.join('')

    country = location['country']
    country_spaces = (0..longest_country.length - country.length).map{' '}.join('')

    "#{city}#{city_spaces} #{country}#{country_spaces} #{event['startDate']} #{event['startTime']}"
  end

  "Here are #{count} upcoming events for #{corrected_artist_name}\n#{events.join("\n")}\n"
end

.write_configObject



99
100
101
102
103
104
# File 'lib/rdio.rb', line 99

def self.write_config
  p = File.join(File.expand_path(ENV['HOME']), '.rdio')
  File.open(p, 'w' ) do |out|
    YAML.dump rdio_config, out
  end
end