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