-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscogs.py
More file actions
133 lines (110 loc) · 4.74 KB
/
discogs.py
File metadata and controls
133 lines (110 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
__version_info__ = (0,0,1)
__version__ = '0.0.1'
import urllib2
import json
import time
import re
import webbrowser
import os
import requests
import glob
from rauth import OAuth1Service
class DiscogsClient:
# Init. Preparing the info for oAuth
def __init__(self):
self._discogsOauth = OAuth1Service(
consumer_key='putYourOwnConsumerKey',
consumer_secret='putYourOwnConsumeSecret',
name='discogs',
access_token_url='http://api.discogs.com/oauth/access_token',
authorize_url='http://www.discogs.com/oauth/authorize',
request_token_url='http://api.discogs.com/oauth/request_token',
base_url='http://api.discogs.com/')
# Get requested URL
def fetchRequest(self,request,url,params):
r = request.get(url,params=params)
return r
# Get User catalog
def fetchCatalog(self,type,username=None,authRequest=None):
catalogList = []
# If private the auth is required
if type == "private":
sender = self._session
username = authRequest['username']
url = authRequest['resource_url']+"/collection/folders/0/releases"
elif type == "public":
sender = requests
url = "http://api.discogs.com/users/"+username+"/collection/folders/0/releases"
releases = self.fetchRequest(sender,url,{'User-agent' : 'gettingCollections Python2.7', 'per_page' : '100'}).json()
# Prepare path to save the catalogs/info
if not os.path.exists('./catalogs/'):
os.makedirs('./catalogs/')
with open ('./catalogs/catalog'+username+'.json', 'w') as outfile:
json.dump(releases,outfile) # Save to file
catalogList.append(releases)
# Fetch all the pages in the catalog requested
try:
for i in range(1,int(re.compile('.*\&page=(.*)').match(releases['pagination']['urls']['last']).group(1))):
time.sleep(1)
releases = self.fetchRequest(sender,url,{'User-agent' : 'GettingCollections Python2.7', 'per_page' : '100', 'page' : i+1}).json()
with open ('catalogs/catalog'+username+str(i)+'.json', 'w') as outfile:
json.dump(releases,outfile)
catalogList.append(releases)
except KeyError: # There is no more than 1 page
pass
return catalogList
def getCatalog(self,username,force):
exists = False
# Checks if there is a catalog downloaded
if (os.path.exists('./catalogs/catalog'+username+'.json') and force == False):
catalogList = glob.glob('./catalogs/catalog'+username+'*.json')
exists = True
return catalogList,exists
# If not, connect to discogs
else:
request = self.fetchRequest(requests,"http://api.discogs.com/users/"+username+"/collection/folders/0/releases", "{'User-agent' : 'gettingCollections Python2.7', 'per_page' : '1'}") # Test if the collection can be downloaded
if request.status_code == 401: #User auth is required
authRequest = self.getAuthLogin()
if authRequest['username'] != username:
print "This collection is private and you dont have access, search for another collection"
exit()
else:
print "Private collection. Fetching..."
catalogList = self.fetchCatalog("private",None,authRequest)
return catalogList,exists
elif request.status_code == 404: #Collection not found
print "This resource is not avaible"
exit()
else: # No auth required
print "Public collection. Fetching..."
catalogList = self.fetchCatalog("public",username,None)
return catalogList,exists
# Login method
def getAuthLogin(self):
self._session = self.oauthLogin()
r = self._session.get(self._discogsOauth.base_url+"oauth/identity", params={'User-agent' : 'GettingCollections Python2.7'}).json()
return r
def oauthLogin(self):
session = None
try:
with open('./sec/oauth.sec'):
credentialsFile = open('./sec/oauth.sec', 'r')
session = credentialsFile.read().split(";")
access_token = session[0]
access_token_secret = session[1]
session = self._discogsOauth.get_session((access_token, access_token_secret))
except IOError:
request_token, request_token_secret = self._discogsOauth.get_request_token()
authorize_url = self._discogsOauth.get_authorize_url(request_token)
print 'Visit this URL in your browser: ' + authorize_url
webbrowser.open(authorize_url)
pin = raw_input('Enter PIN from browser: ')
session = self._discogsOauth.get_auth_session(request_token, request_token_secret,method='GET', data={'oauth_verifier': pin})
if not os.path.exists('./sec/'):
os.makedirs('./sec/')
credentialsFile = open('./sec/oauth.sec', 'w')
credentialsFile.write(session.access_token+";"+session.access_token_secret)
credentialsFile.close()
access_token = session.access_token
access_token_secret = session.access_token_secret
return session