Last active
December 25, 2015 13:49
-
-
Save L1fescape/6986679 to your computer and use it in GitHub Desktop.
Helper file for setting up Grooveshark API sessions and making requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gsapi import GSApi | |
api = GSApi() | |
username = "L1fescape" | |
response = api.call_remote('getUserIDFromUsername', username=username) | |
userID = response['result']['UserID'] | |
response = api.call_remote('getUserPlaylistsByUserID', userID=userID) | |
playlists = response['result']['playlists'] | |
print playlists |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOST = 'api.grooveshark.com' | |
ENDPOINT = 'ws3.php' | |
import settings | |
KEY = settings.KEY | |
SECRET = settings.SECRET | |
import json | |
import hmac | |
import urllib | |
import urllib2 | |
class GSApi: | |
def __init__(self): | |
self.session_id = None | |
def set_session_id(self, sessionID): | |
self.session_id = sessionID | |
def set_debug(self, debug): | |
self.debug = debug | |
def create_message_sig(self, message): | |
return hmac.new(SECRET, message).hexdigest() | |
def call_remote(self, method, **kargs): | |
api_params = {'parameters' : kargs} | |
api_params['method'] = method | |
api_params['header'] = { 'wsKey' : KEY } | |
if self.session_id: | |
api_params['header'].update({'sessionID': self.session_id}) | |
api_params = json.dumps(api_params) | |
signature = self.create_message_sig(api_params) | |
url = 'http://%s/%s?sig=%s' % (HOST, ENDPOINT, signature) | |
response = urllib2.urlopen(url, api_params).read() | |
response = json.loads(response) | |
if not self.session_id and method == 'startSession' and 'result' in response: | |
self.session_id = response['result']['sessionID'] | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to @sh0bin for sending this to me a few years back.