annotate Yading/7digital-python/lib/oauth7digital.py @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 8c29444cb5fd
children
rev   line source
yading@7 1 import httplib
yading@7 2 import oauth
yading@7 3 from lockerEndpoint import Locker
yading@7 4
yading@7 5 class Oauth7digital(object):
yading@7 6 key = None
yading@7 7
yading@7 8 SERVER = 'api.7digital.com'
yading@7 9 REQUEST_TOKEN_URL = 'https://%s/1.2/oauth/requesttoken' % SERVER
yading@7 10 ACCESS_TOKEN_URL = 'https://%s/1.2/oauth/accesstoken' % SERVER
yading@7 11 LOCKER_ENDPOINT_URL = 'http://%s/1.2/user/locker' %SERVER
yading@7 12
yading@7 13 def __init__(self, key, secret, access_token = None):
yading@7 14 self.key = key
yading@7 15 self.secret = secret
yading@7 16 self.access_token = access_token
yading@7 17
yading@7 18 def request_token(self):
yading@7 19 print '\nOAUTH STEP 1'
yading@7 20 oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.__consumer(), http_url = self.REQUEST_TOKEN_URL, parameters={})
yading@7 21 print '\nMESSAGE:: %s' %oauth_request
yading@7 22 oauth_request.sign_request(self.__signature_method(), self.__consumer(), None)
yading@7 23 resp = self.__fetch_response(oauth_request, self.__secure_connection())
yading@7 24
yading@7 25 token = oauth.OAuthToken.from_string(resp)
yading@7 26 return token
yading@7 27
yading@7 28 def authorize_request_token(self, token):
yading@7 29 AUTHORIZATION_URL = 'https://account.7digital.com/%s/oauth/authorise' % self.key
yading@7 30 print '\nOAUTH STEP 2'
yading@7 31 auth_url="%s?oauth_token=%s" % (AUTHORIZATION_URL, token.key)
yading@7 32
yading@7 33 # auth url to go to
yading@7 34 print 'Authorization URL:\n%s' % auth_url
yading@7 35 oauth_verifier = raw_input('Please go to the above URL and authorize the app. Hit return when you have been authorized: ')
yading@7 36 return True
yading@7 37
yading@7 38 def request_access_token(self, token):
yading@7 39 print '\nOAUTH STEP 3'
yading@7 40 oauth_request = self.__sign_oauth_request(token, self.ACCESS_TOKEN_URL)
yading@7 41 resp = self.__fetch_response(oauth_request, self.__secure_connection())
yading@7 42
yading@7 43 token = oauth.OAuthToken.from_string(resp)
yading@7 44 return token
yading@7 45
yading@7 46 def get_user_locker(self):
yading@7 47 resp = self.__get_locker()
yading@7 48 return Locker(resp).get_content()
yading@7 49
yading@7 50 def get_artist_from_user_locker(self):
yading@7 51 resp = self.__get_locker()
yading@7 52 return Locker(resp).get_artists()
yading@7 53
yading@7 54 def get_releases_from_user_locker(self):
yading@7 55 resp = self.__get_locker()
yading@7 56 return Locker(resp).get_releases()
yading@7 57
yading@7 58 def get_tracks_from_user_locker(self):
yading@7 59 resp = self.__get_locker()
yading@7 60 return Locker(resp).get_tracks()
yading@7 61
yading@7 62 def get_locker(self):
yading@7 63 resp = self.__get_locker()
yading@7 64 return Locker(resp).get_contents()
yading@7 65
yading@7 66 def __get_locker(self):
yading@7 67 oauth_request = self.__sign_oauth_request(self.access_token, self.LOCKER_ENDPOINT_URL)
yading@7 68 resp = self.__fetch_response(oauth_request, self.__connection())
yading@7 69 return resp
yading@7 70
yading@7 71 def __sign_oauth_request(self, token, url_end_point):
yading@7 72 oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.__consumer(), token=token, http_url = url_end_point, parameters={})
yading@7 73
yading@7 74 oauth_request.sign_request(self.__signature_method(), self.__consumer(), token)
yading@7 75 return oauth_request
yading@7 76
yading@7 77 def __consumer(self):
yading@7 78 return oauth.OAuthConsumer(self.key, self.secret)
yading@7 79
yading@7 80 def __signature_method(self):
yading@7 81 return oauth.OAuthSignatureMethod_HMAC_SHA1()
yading@7 82
yading@7 83 def __secure_connection(self):
yading@7 84 return httplib.HTTPSConnection(self.SERVER)
yading@7 85
yading@7 86 def __connection(self):
yading@7 87 return httplib.HTTPConnection(self.SERVER)
yading@7 88
yading@7 89 def __fetch_response(self, oauth_request, connection):
yading@7 90 url = oauth_request.to_url()
yading@7 91 connection.request(oauth_request.http_method, url)
yading@7 92 response = connection.getresponse()
yading@7 93 result = response.read()
yading@7 94
yading@7 95 return result
yading@7 96