class Grooveshark::Client
Client
class
Attributes
comm_token[RW]
comm_token_ttl[R]
country[R]
session[RW]
user[R]
Public Class Methods
new(params = {})
click to toggle source
# File lib/grooveshark/client.rb, line 8 def initialize(params = {}) @ttl = params[:ttl] || 120 # 2 minutes @uuid = UUID.new.generate.upcase token_data end
Public Instance Methods
body(method, params)
click to toggle source
# File lib/grooveshark/client.rb, line 153 def body(method, params) body = { 'header' => { 'client' => 'mobileshark', 'clientRevision' => '20120830', 'country' => @country, 'privacy' => 0, 'session' => @session, 'uuid' => @uuid }, 'method' => method, 'parameters' => params } body['header']['token'] = create_token(method) if @comm_token body end
create_token(method)
click to toggle source
Sign method
# File lib/grooveshark/client.rb, line 140 def create_token(method) rnd = get_random_hex_chars(6) salt = 'gooeyFlubber' plain = [method, @comm_token, salt, rnd].join(':') hash = Digest::SHA1.hexdigest(plain) "#{rnd}#{hash}" end
get_random_hex_chars(length)
click to toggle source
# File lib/grooveshark/client.rb, line 148 def get_random_hex_chars(length) chars = ('a'..'f').to_a | (0..9).to_a (0...length).map { chars[rand(chars.length)] }.join end
get_song_url(song)
click to toggle source
Get song stream
# File lib/grooveshark/client.rb, line 115 def get_song_url(song) get_song_url_by_id(song.id) end
get_song_url_by_id(id)
click to toggle source
Get song stream url by ID
# File lib/grooveshark/client.rb, line 109 def get_song_url_by_id(id) resp = get_stream_auth_by_songid(id) "http://#{resp['ip']}/stream.php?streamKey=#{resp['stream_key']}" end
get_stream_auth(song)
click to toggle source
Get stream authentication for song object
# File lib/grooveshark/client.rb, line 104 def get_stream_auth(song) get_stream_auth_by_songid(song.id) end
get_stream_auth_by_songid(song_id)
click to toggle source
Get stream authentication by song ID
# File lib/grooveshark/client.rb, line 89 def get_stream_auth_by_songid(song_id) result = request('getStreamKeyFromSongIDEx', 'type' => 0, 'prefetch' => false, 'songID' => song_id, 'country' => @country, 'mobile' => false) if result == [] fail GeneralError, 'No data for this song. ' \ 'Maybe Grooveshark banned your IP.' end result end
get_user_by_id(id)
click to toggle source
Find user by ID
# File lib/grooveshark/client.rb, line 26 def get_user_by_id(id) resp = request('getUserByID', userID: id)['user'] resp['user_id'].nil? ? nil : User.new(self, resp) end
get_user_by_username(name)
click to toggle source
Find user by username
# File lib/grooveshark/client.rb, line 32 def get_user_by_username(name) resp = request('getUserByUsername', username: name)['user'] resp['user_id'].nil? ? nil : User.new(self, resp) end
login(user, password)
click to toggle source
Authenticate user
# File lib/grooveshark/client.rb, line 15 def login(user, password) data = request('authenticateUser', { username: user, password: password }, true) @user = User.new(self, data) fail InvalidAuthentication, 'Wrong username or password!' if @user.id == 0 @user end
popular_songs(type = 'daily')
click to toggle source
Get popular songs type => daily, monthly
# File lib/grooveshark/client.rb, line 47 def popular_songs(type = 'daily') fail ArgumentError, 'Invalid type' unless %w(daily monthly).include?(type) request('popularGetSongs', type: type)['songs'].map { |s| Song.new(s) } end
recent_users()
click to toggle source
Get recently active users
# File lib/grooveshark/client.rb, line 38 def recent_users request('getRecentlyActiveUsers', {})['users'] .map do |u| User.new(self, u) end end
refresh_token()
click to toggle source
Refresh communications token on ttl
# File lib/grooveshark/client.rb, line 194 def refresh_token token_data if Time.now.to_i - @comm_token_ttl > @ttl end
request(method, params = {}, secure = false)
click to toggle source
Perform API request
# File lib/grooveshark/client.rb, line 171 def request(method, params = {}, secure = false) refresh_token if @comm_token url = "#{secure ? 'https' : 'http'}://grooveshark.com/more.php?#{method}" begin data = RestClient.post(url, body(method, params).to_json, 'Content-Type' => 'application/json') rescue StandardError => ex raise GeneralError, ex.message end data = JSON.parse(data) data = data.normalize if data.is_a?(Hash) if data.key?('fault') fail ApiError, data['fault'] else data['result'] end end
search(type, query)
click to toggle source
Perform search request for query
# File lib/grooveshark/client.rb, line 67 def search(type, query) results = [] search = request('getResultsFromSearch', type: type, query: query) results = search['result'].map do |data| next Song.new data if type == 'Songs' next Playlist.new(self, data) if type == 'Playlists' data end if search.key?('result') results end
search_songs(query)
click to toggle source
Perform songs search request for query
# File lib/grooveshark/client.rb, line 79 def search_songs(query) search('Songs', query) end
search_songs_pure(query)
click to toggle source
Return raw response for songs search request
# File lib/grooveshark/client.rb, line 84 def search_songs_pure(query) request('getSearchResultsEx', type: 'Songs', query: query) end
token_data()
click to toggle source
# File lib/grooveshark/client.rb, line 119 def token_data response = RestClient.get('http://grooveshark.com') preload_regex = /gsPreloadAjax\(\{url: '\/preload.php\?(.*)&hash=' \+ clientPage\}\)/ # rubocop:disable Metrics/LineLength preload_id = response.to_s.scan(preload_regex).flatten.first preload_url = "http://grooveshark.com/preload.php?#{preload_id}" \ '&getCommunicationToken=1&hash=%2F' preload_response = RestClient.get(preload_url) token_data_json = preload_response.to_s .scan(/window.tokenData = (.*);/).flatten.first fail GeneralError, 'token data not found' unless token_data_json token_data = JSON.parse(token_data_json) @comm_token = token_data['getCommunicationToken'] @comm_token_ttl = Time.now.to_i config = token_data['getGSConfig'] @country = config['country'] @session = config['sessionID'] end
top_broadcasts(count = 10)
click to toggle source
Get top broadcasts count => specifies how many broadcasts to get
# File lib/grooveshark/client.rb, line 54 def top_broadcasts(count = 10) top_broadcasts = [] request('getTopBroadcastsCombined').each do |key, _val| broadcast_id = key.split(':')[1] top_broadcasts.push(Broadcast.new(self, broadcast_id)) count -= 1 break if count == 0 end top_broadcasts end