grooveshark 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/grooveshark.rb CHANGED
@@ -2,6 +2,7 @@ require 'digest'
2
2
  require 'json'
3
3
  require 'rest-client'
4
4
 
5
+ require 'grooveshark/utils'
5
6
  require 'grooveshark/errors'
6
7
  require 'grooveshark/request'
7
8
  require 'grooveshark/client'
@@ -18,7 +18,6 @@ module Grooveshark
18
18
 
19
19
  # Get communication token
20
20
  def get_comm_token
21
- puts "Grooveshark::Client.get_comm_token"
22
21
  @comm_token = nil # so that it doesn't send a token
23
22
  @comm_token = request('getCommunicationToken', {:secretKey => Digest::MD5.hexdigest(@session)}, true)
24
23
  end
@@ -38,7 +37,24 @@ module Grooveshark
38
37
  raise InvalidAuthentication, 'Wrong username or password!' if @user.id == 0
39
38
  return @user
40
39
  end
41
-
40
+
41
+ # Find user by ID
42
+ def get_user_by_id(id)
43
+ resp = request('getUserByID', {:userID => id})['user']
44
+ resp['username'].empty? ? nil : User.new(self, resp)
45
+ end
46
+
47
+ # Find user by username
48
+ def get_user_by_username(name)
49
+ resp = request('getUserByUsername', {:username => name})['user']
50
+ resp['username'].empty? ? nil : User.new(self, resp)
51
+ end
52
+
53
+ # Get recently active users
54
+ def recent_users
55
+ request('getRecentlyActiveUsers', {})['users'].map { |u| User.new(self, u) }
56
+ end
57
+
42
58
  # Perform search request for query
43
59
  def search(type, query)
44
60
  results = request('getSearchResults', {:type => type, :query => query})
@@ -73,7 +89,7 @@ module Grooveshark
73
89
  # Get song stream url by ID
74
90
  def get_song_url_by_id(id)
75
91
  resp = get_stream_auth_by_songid(id)
76
- "http://#{resp['ip']}/stream.php?streamKey=#{resp['streamKey']}"
92
+ "http://#{resp['ip']}/stream.php?streamKey=#{resp['stream_key']}"
77
93
  end
78
94
 
79
95
  # Get song stream
@@ -1,4 +1,5 @@
1
1
  module Grooveshark
2
2
  class InvalidAuthentication < Exception ; end
3
+ class ReadOnlyAccess < Exception ; end
3
4
  class GeneralError < Exception ; end
4
5
  end
@@ -9,18 +9,18 @@ module Grooveshark
9
9
  @songs = []
10
10
 
11
11
  if data
12
- @id = data['PlaylistID']
13
- @name = data['Name']
14
- @about = data['About']
15
- @picture = data['Picture']
16
- @user_id = data['UserID'] || user_id
17
- @username = data['Username']
12
+ @id = data['playlist_id']
13
+ @name = data['name']
14
+ @about = data['about']
15
+ @picture = data['picture']
16
+ @user_id = data['user_id'] || user_id
17
+ @username = data['user_name']
18
18
  end
19
19
  end
20
20
 
21
21
  # Fetch playlist songs
22
22
  def load_songs
23
- @songs = @client.request('playlistGetSongs', :playlistID => @id)['Songs']
23
+ @songs = @client.request('playlistGetSongs', :playlistID => @id)['songs']
24
24
  @songs.map! { |s| Song.new(s) }
25
25
  end
26
26
 
@@ -12,7 +12,7 @@ module Grooveshark
12
12
  }
13
13
 
14
14
  # Perform API request
15
- def request(method, params, secure=false)
15
+ def request(method, params={}, secure=false)
16
16
  agent = METHOD_CLIENTS.key?(method) ? METHOD_CLIENTS[method] : CLIENT
17
17
  url = "#{secure ? 'https' : 'http'}://#{API_BASE}/more.php?#{method}"
18
18
  body = {
@@ -40,6 +40,7 @@ module Grooveshark
40
40
  end
41
41
 
42
42
  data = JSON.parse(data)
43
+ data = data.normalize if data.kind_of?(Hash)
43
44
  return data['result'] unless data['fault']
44
45
  end
45
46
  end
@@ -8,23 +8,25 @@ module Grooveshark
8
8
  def initialize(data=nil)
9
9
  unless data.nil?
10
10
  @data = data
11
- @id = data['SongID']
12
- @name = data['SongName'] || data['Name']
13
- @artist = data['ArtistName']
14
- @artist_id = data['ArtistID']
15
- @album = data['AlbumName']
16
- @album_id = data['AlbumID']
17
- @track = data['TrackNum']
18
- @duration = data['EstimateDuration']
19
- @artwork = data['CoverArtFilename']
20
- @playcount = data['SongPlays']
11
+ @id = data['song_id']
12
+ @name = data['song_name'] || data['name']
13
+ @artist = data['artist_name']
14
+ @artist_id = data['artist_id']
15
+ @album = data['album_name']
16
+ @album_id = data['album_id']
17
+ @track = data['track_num']
18
+ @duration = data['estimate_duration']
19
+ @artwork = data['cover_art_filename']
20
+ @playcount = data['song_plays']
21
21
  end
22
22
  end
23
23
 
24
+ # Presentable format
24
25
  def to_s
25
26
  [@id, @name, @artist].join(' - ')
26
27
  end
27
28
 
29
+ # Hash export for API usage
28
30
  def to_hash
29
31
  {
30
32
  'songID' => @id,
@@ -1,17 +1,33 @@
1
1
  module Grooveshark
2
2
  class User
3
- attr_reader :id, :username, :premium, :data
3
+ attr_reader :id, :username, :email, :premium, :data
4
+ attr_reader :city, :country, :sex
4
5
  attr_reader :playlists, :favorites
5
6
 
6
7
  # Init user account object
7
8
  def initialize(client, data=nil)
8
9
  if data
9
- @data = data
10
- @id = data['userID']
10
+ @data = data
11
+ @id = data['user_id']
11
12
  @username = data['username']
12
- @premium = data['isPremium']
13
+ @premium = data['is_premium']
14
+ @email = data['email']
15
+ @city = data['city']
16
+ @country = data['country']
17
+ @sex = data['sex']
13
18
  end
14
- @client = client
19
+ @client = client
20
+ end
21
+
22
+ # Get user avatar URL
23
+ def avatar
24
+ "http://beta.grooveshark.com/static/userimages/#{@id}.jpg"
25
+ end
26
+
27
+ # Get user activity for the date (COMES AS RAW RESPONSE)
28
+ def feed(date=nil)
29
+ date = Time.now if date.nil?
30
+ @client.request('getProcessedUserFeedData', {:userID => @id, :day => date.strftime("%Y%m%d")})
15
31
  end
16
32
 
17
33
  # --------------------------------------------------------------------------
@@ -20,19 +36,20 @@ module Grooveshark
20
36
 
21
37
  # Fetch songs from library
22
38
  def library(page=0)
23
- resp = @client.request('userGetSongsInLibrary', {:userID => @id, :page => page.to_s})['Songs']
39
+ resp = @client.request('userGetSongsInLibrary', {:userID => @id, :page => page.to_s})['songs']
24
40
  resp.map { |s| Song.new(s) }
25
41
  end
26
42
 
27
- # Add songs to user library (DOES NOT WORK FOR SOME REASON)
43
+ # Add songs to user's library
28
44
  def library_add(songs=[])
29
45
  @client.request('userAddSongsToLibrary', {:songs => songs.map { |s| s.to_hash }})
30
46
  end
31
47
 
32
48
  # Remove song from user library
33
49
  def library_remove(song)
34
- song_id = song.kind_of?(Song) ? song.id : song.to_s
35
- @client.request('userRemoveSongFromLibrary', {:userID => @id, :songID => song_id})
50
+ raise ArgumentError, 'Song object required' unless song.kind_of?(Song)
51
+ req = {:userID => @id, :songID => song.id, :albumID => song.album_id, :artistID => song.artist_id}
52
+ @client.request('userRemoveSongFromLibrary', req)
36
53
  end
37
54
 
38
55
  # --------------------------------------------------------------------------
@@ -43,7 +60,7 @@ module Grooveshark
43
60
  def playlists
44
61
  return @playlists if @playlists
45
62
  results = @client.request('userGetPlaylists', :userID => @id)
46
- @playlists = results['Playlists'].map { |list| Playlist.new(@client, list, @id) }
63
+ @playlists = results['playlists'].map { |list| Playlist.new(@client, list, @id) }
47
64
  end
48
65
 
49
66
  # Get playlist by ID
@@ -0,0 +1,26 @@
1
+ class String
2
+ def normalize_attribute
3
+ self.gsub(/^.*::/, "").
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ downcase
7
+ end
8
+ end
9
+
10
+ class Hash
11
+ def normalize
12
+ h = {}
13
+ self.each_pair do |k,v|
14
+ attr = k.to_s.normalize_attribute
15
+ case v
16
+ when Hash
17
+ h[attr] = v.normalize
18
+ when Array
19
+ h[attr] = v.map { |o| o.kind_of?(Hash) ? o.normalize : o }
20
+ else
21
+ h[attr] = v
22
+ end
23
+ end
24
+ h
25
+ end
26
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grooveshark
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dan Sosedoff
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-08 00:00:00 -06:00
18
+ date: 2011-01-18 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,7 @@ extra_rdoc_files: []
60
60
 
61
61
  files:
62
62
  - lib/grooveshark.rb
63
+ - lib/grooveshark/utils.rb
63
64
  - lib/grooveshark/client.rb
64
65
  - lib/grooveshark/errors.rb
65
66
  - lib/grooveshark/playlist.rb