rdio 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rdio/oauth.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'open-uri'
4
+
5
+ module Rdio
6
+
7
+ # ----------------------------------------------------------------------
8
+ # Performs OAuth authentication based on a key and secret
9
+ # ----------------------------------------------------------------------
10
+ class RdioOAuth
11
+
12
+ SITE = 'http://api.rdio.com'
13
+
14
+ def initialize(key,secret)
15
+ @key = key
16
+ @secret = secret
17
+ end
18
+
19
+ def access_token(requires_auth=false)
20
+ requires_auth ? access_token_auth : access_token_no_auth
21
+ end
22
+
23
+ private
24
+
25
+ def access_token_no_auth
26
+ consumer = OAuth::Consumer.new @key, @secret, {:site => SITE}
27
+ consumer.http.read_timeout = 600
28
+ OAuth::AccessToken.new consumer
29
+ end
30
+
31
+ def access_token_auth
32
+ consumer =
33
+ OAuth::Consumer.new(@key,@secret,
34
+ {:site => SITE,
35
+ :request_token_path => "/oauth/request_token",
36
+ :authorize_path => "/oauth/authorize",
37
+ :access_token_path => "/oauth/access_token",
38
+ :http_method => :post})
39
+
40
+ request_token = consumer.get_request_token({:oauth_callback => 'oob'})
41
+ url = 'https://www.rdio.com/oauth/authorize?oauth_token=' +
42
+ request_token.token.to_s
43
+ system 'open',url
44
+
45
+ oauth_verifier = nil
46
+ while not oauth_verifier or oauth_verifier == ''
47
+ print 'Enter the PIN> '
48
+ oauth_verifier = gets.strip
49
+ end
50
+ request_token.get_access_token({:oauth_verifier => oauth_verifier})
51
+ end
52
+ end
53
+
54
+ end
data/lib/rdio/types.rb ADDED
@@ -0,0 +1,527 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Rdio
3
+
4
+ # ----------------------------------------------------------------------
5
+ # Represents an artist on Rdio, either an individual performer or a
6
+ # group
7
+ # ----------------------------------------------------------------------
8
+ class Artist < ArtistData
9
+
10
+ def initialize(api)
11
+ super api
12
+ end
13
+
14
+ attr_accessor :albums
15
+ attr_accessor :play_count
16
+ attr_accessor :secondary_id
17
+ attr_accessor :count
18
+ attr_accessor :has_offline
19
+ attr_accessor :sortable_name
20
+ attr_accessor :id
21
+
22
+ attr_accessor :user_name
23
+ attr_accessor :frozen_track_list
24
+ attr_accessor :artist_url
25
+ attr_accessor :user_key
26
+ attr_accessor :artist_name
27
+
28
+ attr_accessor :tracks
29
+
30
+ # Get all of the tracks by this artist.
31
+ def tracks(appears_on=nil,start=nil,count=nil,extras=nil)
32
+ api.getTracksForArtist self,appears_on,start,count,extras
33
+ end
34
+
35
+ # Which tracks from the given artist are in the user's collection.
36
+ def tracks_in_collection(user=nil,extras=nil)
37
+ api.getTracksForArtistInCollection self,user,extras
38
+ end
39
+
40
+ # Fetch one or more objects from Rdio of type Artist.
41
+ def self.all(keys)
42
+ Rdio::api.get keys,Artist
43
+ end
44
+
45
+ # Fetch one object from Rdio of type Artist.
46
+ def self.get(key)
47
+ arr = all [key]
48
+ (arr and not arr.empty?) ? arr[0] : nil
49
+ end
50
+
51
+ # Get all of the artist in a user's collection.
52
+ def in_collection(user=nil,start=nil,count=nil,sort=nil,query=nil)
53
+ api.getArtistsInCollection user,start,count,sort,query
54
+ end
55
+
56
+ # Return the Artist that the supplied Rdio short-code is a
57
+ # representation of, or null if the short-code is invalid.
58
+ def self.from_short_code(short_code)
59
+ Rdio::api.getObjectFromShortCode short_code,Artist
60
+ end
61
+
62
+ # Return the Artist that the supplied Rdio url is a representation
63
+ # of, or null if the url doesn't represent an object.
64
+ def self.from_url(url)
65
+ Rdio::api.getObjectFromUrl url,Artist
66
+ end
67
+
68
+ # Return the site-wide most popular items for Artists
69
+ def self.top_charts
70
+ Rdio::api.getTopCharts Artist
71
+ end
72
+
73
+ end
74
+
75
+
76
+ # ----------------------------------------------------------------------
77
+ # Represents a recording on Rdio, usually an album but often a
78
+ # single, EP or compilation
79
+ # ----------------------------------------------------------------------
80
+ class Album < AlbumData
81
+
82
+ def initialize(api)
83
+ super api
84
+ end
85
+
86
+ # the track ids
87
+ attr_accessor :track_ids
88
+
89
+ # the secondary id
90
+ attr_accessor :secondary_id
91
+
92
+ # the tracks
93
+ attr_accessor :tracks
94
+
95
+ # Fetch one or more objects from Rdio of type Album.
96
+ def self.all(keys)
97
+ Rdio::api.get keys,Album
98
+ end
99
+
100
+ # Fetch one object from Rdio of type Album.
101
+ def self.get(key)
102
+ arr = all [key]
103
+ (arr and not arr.empty?) ? arr[0] : nil
104
+ end
105
+
106
+ # Return the albums by (or featuring) an artist.
107
+ def self.for_artist(artist,featuring=nil,extras=nil,start=nil,count=nil)
108
+ Rdio::api.getAlbumsForArtist artist,featuring,extras,start,count
109
+ end
110
+
111
+ # Get the albums in the user's collection by a particular artist.
112
+ def self.for_artist_in_collection(artist,user=nil)
113
+ Rdio::api.getAlbumsForArtistInCollection artist,user
114
+ end
115
+
116
+ # Get all of the albums in the user's collection.
117
+ def self.in_collection(user=nil,start=nil,count=nil,
118
+ sort=nil,query=nil)
119
+ Rdio::api.getAlbumsInCollection user,start,count,sort,query
120
+ end
121
+
122
+ # Return new albums released across a timeframe.
123
+ def self.new_releases(time)
124
+ Rdio::api.getNewReleases time
125
+ end
126
+
127
+ # Return the Albums that the supplied Rdio short-code is a
128
+ # representation of, or null if the short-code is invalid.
129
+ def self.from_short_code(short_code)
130
+ Rdio::api.getObjectFromShortCode short_code,Album
131
+ end
132
+
133
+ # Return the Album that the supplied Rdio url is a representation
134
+ # of, or null if the url doesn't represent an object.
135
+ def self.from_url(url)
136
+ Rdio::api.getObjectFromUrl url,Album
137
+ end
138
+
139
+ # Return the site-wide most popular items for Albums
140
+ def self.top_charts
141
+ Rdio::api.getTopCharts Album
142
+ end
143
+
144
+ # Which tracks on the given album are in the user's collection.
145
+ def tracks_in_collection(user=nil,extras=nil)
146
+ api.getTracksForAlbumInCollection self,user,extras
147
+ end
148
+
149
+ end
150
+
151
+ # ----------------------------------------------------------------------
152
+ # Represents a song on Rdio
153
+ # ----------------------------------------------------------------------
154
+ class Track < TrackData
155
+
156
+ def initialize(api)
157
+ super api
158
+ end
159
+
160
+ # the track number
161
+ attr_accessor :track_num
162
+
163
+ # the secondary id
164
+ attr_accessor :secondary_id
165
+
166
+ # Get all of the tracks in the user's collection.
167
+ def self.in_collection(user=nil,start=nil,count=nil,sort=nil,query=nil)
168
+ Rdio::api.getTracksInCollection user,start,count,sort,query
169
+ end
170
+
171
+ # Get all of the tracks by this artist.
172
+ def self.for_artist(artist,appears_on=nil,start=nil,
173
+ count=nil,extras=nil)
174
+ Rdio::api.getTracksForArtist artist,appears_on,start,count,extras
175
+ end
176
+
177
+ # Which tracks from the given artist are in the user's collection.
178
+ def self.for_artist_in_collection(artist,user=nil,extras=nil)
179
+ Rdio::api.getTracksForArtistInCollection artist,user,extras
180
+ end
181
+
182
+ # Fetch one or more objects from Rdio of type Track.
183
+ def self.all(keys)
184
+ Rdio::api.get keys,Track
185
+ end
186
+
187
+ # Fetch one object from Rdio of type Track.
188
+ def self.get(key)
189
+ arr = all [key]
190
+ return (arr and not arr.empty?) ? arr[0] : nil
191
+ end
192
+
193
+ # Return the Track that the supplied Rdio short-code is a
194
+ # representation of, or null if the short-code is invalid.
195
+ def self.from_short_code(short_code)
196
+ Rdio::api.getObjectFromShortCode short_code,Track
197
+ end
198
+
199
+ # Return the Track that the supplied Rdio url is a representation
200
+ # of, or null if the url doesn't represent an object.
201
+ def self.from_url(url)
202
+ Rdio::api.getObjectFromUrl url,Track
203
+ end
204
+
205
+ # Return the site-wide most popular items for Tracks
206
+ def self.top_charts
207
+ Rdio::api.getTopCharts Track
208
+ end
209
+
210
+ # Which tracks on the given album are in the user's collection.
211
+ def self.for_album_in_collection(album,user=nil,extras=nil)
212
+ Rdio::api.getTracksForAlbumInCollection album,user,extras
213
+ end
214
+
215
+ end
216
+
217
+ # ----------------------------------------------------------------------
218
+ # Represents a playlist on Rdio
219
+ # ----------------------------------------------------------------------
220
+ class Playlist < PlaylistData
221
+
222
+ def initialize(api)
223
+ super api
224
+ end
225
+
226
+ # Remove an item from a playlist by its position in the playlist.
227
+ def remove(index,count,tracks)
228
+ api.removeFromPlaylist self,index,count,tracks
229
+ end
230
+
231
+
232
+ # Add a track to a playlist.
233
+ def add_to_playlist(tracks)
234
+ api.addToPlaylist self,tracks
235
+ end
236
+
237
+ # Create a new playlist in the current user's collection. The new
238
+ # playlist will be returned if the creation is successful,
239
+ # otherwise null will be returned.
240
+ def self.create(name,description,tracks,extras=nil)
241
+ Rdio::api.createPlaylist name,description,tracks,extras
242
+ end
243
+
244
+ # Delete a playlist.
245
+ def delete
246
+ api.deletePlaylist self
247
+ end
248
+
249
+ # Fetch one or more objects from Rdio of type Playlist.
250
+ def self.all(keys)
251
+ Rdio::api.get keys,Playlist
252
+ end
253
+
254
+ # Fetch one object from Rdio of type Playlist.
255
+ def self.get(key)
256
+ arr = all [key]
257
+ return (arr and not arr.empty?) ? arr[0] : nil
258
+ end
259
+
260
+ # Return the Playlist that the supplied Rdio short-code is a
261
+ # representation of, or null if the short-code is invalid.
262
+ def self.from_short_code(short_code)
263
+ Rdio::api.getObjectFromShortCode short_code,Playlist
264
+ end
265
+
266
+ # Return the Playlist that the supplied Rdio url is a representation
267
+ # of, or null if the url doesn't represent an object.
268
+ def self.from_url(url)
269
+ Rdio::api.getObjectFromUrl url,Playlist
270
+ end
271
+
272
+ # Return the site-wide most popular items for Playlists
273
+ def self.top_charts
274
+ Rdio::api.getTopCharts Playlist
275
+ end
276
+
277
+ end
278
+
279
+ # ----------------------------------------------------------------------
280
+ # Represents an Rdio user
281
+ # ----------------------------------------------------------------------
282
+ class User < UserData
283
+
284
+ def initialize(api)
285
+ super api
286
+ end
287
+
288
+ # Get the activity events for a user, a user's friends, or
289
+ # everyone on Rdio.
290
+ def activity_stream(scope='user',last_id=nil)
291
+ api.getActivityStream self,scope,last_id
292
+ end
293
+
294
+ # Get information about the currently logged in user.
295
+ def self.current(extras=nil)
296
+ Rdio::api.currentUser extras
297
+ end
298
+
299
+ # Find a user either by email address.
300
+ def self.find_by_email(email)
301
+ Rdio::api.findUserByEmail email
302
+ end
303
+
304
+ # Find a user either by email address.
305
+ def self.find_by_vanity_name(name)
306
+ Rdio::api.findUserByVanityName name
307
+ end
308
+
309
+ # Fetch one or more objects from Rdio of type User.
310
+ def self.all(keys)
311
+ Rdio::api.get keys,User
312
+ end
313
+
314
+ # Fetch one object from Rdio of type User.
315
+ def self.get(key)
316
+ arr = all [key]
317
+ return (arr and not arr.empty?) ? arr[0] : nil
318
+ end
319
+
320
+ # Get all of the albums in the user's collection.
321
+ def albums_in_collection(start=nil,count=nil,
322
+ sort=nil,query=nil)
323
+ api.getAlbumsInCollection self,start,count,sort,query
324
+ end
325
+
326
+ # Get the albums in the user's collection by a particular artist.
327
+ def albums_for_artist_in_collection(artist)
328
+ api.getAlbumsForArtistInCollection artist,self
329
+ end
330
+
331
+ # Get all of the artist in a user's collection.
332
+ def artists_in_collection(start=nil,count=nil,sort=nil,query=nil)
333
+ api.getArtistsInCollection self,start,count,sort,query
334
+ end
335
+
336
+ # Which tracks from the given artist are in the user's collection.
337
+ def tracks_for_artist_in_collection(artist,extras=nil)
338
+ api.getTracksForArtistInCollection artist,self,extras
339
+ end
340
+
341
+ # Get all of the tracks in the user's collection.
342
+ def tracks_in_collection(start=nil,count=nil,sort=nil,query=nil)
343
+ api.getTracksInCollection self,start,count,sort,query
344
+ end
345
+
346
+ # Which tracks on the given album are in the user's collection.
347
+ def tracks_for_album_in_collection(album,extras=nil)
348
+ api.getTracksForAlbumInCollection album,self,extras
349
+ end
350
+
351
+ # Add a friend to the current user.
352
+ def self.add_friend(user)
353
+ Rdio::api.addFriend user
354
+ end
355
+
356
+ # Remove a friend from the current user.
357
+ def self.remove_friend(user)
358
+ Rdio::api.removeFriend user
359
+ end
360
+
361
+ # Remove tracks or playlists from the current user's collection.
362
+ def self.remove_from_collection(objs)
363
+ Rdio::api.removeFromCollection objs
364
+ end
365
+
366
+ # Remove tracks from the current user's collection.
367
+ def self.remove_tracks_from_collection(tracks)
368
+ remove_from_collection tracks
369
+ end
370
+
371
+ # Remove playlistsfrom the current user's collection.
372
+ def self.remove_playlists_from_collection(playlists)
373
+ remove_from_collection playlists
374
+ end
375
+
376
+ # Add tracks or playlists to the current user's collection.
377
+ def self.add_to_collection(objs)
378
+ Rdio::api.addToCollection objs
379
+ end
380
+
381
+ # Add tracks to the current user's collection.
382
+ def self.add_tracks_to_collection(tracks)
383
+ add_to_collection tracks
384
+ end
385
+
386
+ # Add playlists to the current user's collection.
387
+ def self.add_playlists_to_collection(playlists)
388
+ add_to_collection playlists
389
+ end
390
+
391
+ # Find the most popular artists or albums for a user, their
392
+ # friends or the whole site.
393
+ def heavy_rotation(friends=nil,limit=nil)
394
+ api.getHeavyRotation self,nil,friends,limit
395
+ end
396
+
397
+ # Find the most popular artists for a user, their friends or the
398
+ # whole site.
399
+ def artists_heavy_rotation(friends=nil,limit=nil)
400
+ api.getHeavyRotation self,Artist,friends,limit
401
+ end
402
+
403
+ # Find the most popular albums for a user, their
404
+ # friends or the whole site.
405
+ def albums_heavy_rotation(friends=nil,limit=nil)
406
+ api.getHeavyRotation self,Album,friends,limit
407
+ end
408
+
409
+ # Return the User that the supplied Rdio short-code is a
410
+ # representation of, or null if the short-code is invalid.
411
+ def self.from_short_code(short_code)
412
+ Rdio::api.getObjectFromShortCode short_code,User
413
+ end
414
+
415
+ # Return the User that the supplied Rdio url is a representation
416
+ # of, or null if the url doesn't represent an object.
417
+ def self.from_url(url)
418
+ Rdio::api.getObjectFromUrl url,User
419
+ end
420
+
421
+ # Get the current user's playlists.
422
+ def self.playlists(extras=nil)
423
+ Rdio::api.getPlaylists extras
424
+ end
425
+
426
+ # Return the site-wide most popular items for Users
427
+ def self.top_charts
428
+ Rdio::api.getTopCharts User
429
+ end
430
+
431
+
432
+ end
433
+
434
+ # ----------------------------------------------------------------------
435
+ # An activity stream.
436
+ # ----------------------------------------------------------------------
437
+ class ActivityStream < ApiObj
438
+
439
+ # used to walk through activity
440
+ attr_accessor :last_id
441
+
442
+ # the User object for the user that was passed in
443
+ attr_accessor :user
444
+
445
+ # the updates
446
+ attr_accessor :updates
447
+
448
+ def initialize(api)
449
+ super api
450
+ end
451
+
452
+ class Update < ApiObj
453
+
454
+ # One of the following
455
+ # 0 — track added to collection
456
+ # 1 — track added to playlist
457
+ # 3 — friend added
458
+ # 5 — user joined
459
+ # 6 — comment added to track
460
+ # 7 — comment added to album
461
+ # 8 — comment added to artist
462
+ # 9 — comment added to playlist
463
+ # 10 — track added via match collection
464
+ # 11 — user subscribed to Rdio
465
+ # 12 — track synced to mobile
466
+ attr_accessor :update_type
467
+
468
+ # string date
469
+ attr_accessor :date
470
+
471
+ # User owner
472
+ attr_accessor :owner
473
+
474
+ # Albums
475
+ attr_accessor :albums
476
+
477
+ # People
478
+ attr_accessor :people
479
+
480
+ def initialize(api)
481
+ super api
482
+ end
483
+
484
+ end
485
+
486
+ end
487
+
488
+ # Wrapper for search
489
+ class Search
490
+
491
+ def self.search(query,types=nil,never_or=nil,extras=nil,start=nil,count=nil)
492
+ Rdio::api.search query,types,never_or,extras,start,count
493
+ end
494
+
495
+ end
496
+
497
+ # Dummy class so we can create booleans from strings
498
+ class Boolean
499
+
500
+ end
501
+
502
+ # Dummy class so we can set the type variable in API methods to TODO
503
+ class TODO
504
+
505
+ end
506
+
507
+ # ----------------------------------------------------------------------
508
+ # Set up the mapping of names of attributes to the classes with
509
+ # which we will construct the JSON.
510
+ #
511
+ # TODO: May want to put these in individual classes?
512
+ # ----------------------------------------------------------------------
513
+ Rdio::symbols_to_types = {
514
+ :user => User,
515
+ :owner => User,
516
+ :friends => User,
517
+ :everyone => User,
518
+ :people => User,
519
+ :users => User,
520
+ :artists => Artist,
521
+ :albums => Album,
522
+ :tracks => Track,
523
+ :playlists => Playlist,
524
+ :updates => ActivityStream::Update,
525
+ }
526
+
527
+ end