forked from nolageek/Discogs-Collection-Page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-discogs.py.sample
152 lines (130 loc) · 5.01 KB
/
update-discogs.py.sample
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Notes for python 3.x:
#
# Change all occurances of urllib to urllib.request, including the following
# import statement as well as all instances of urllib.urlretrieve to
# urllib.request.urlretrieve
import getopt, sys, json, urllib, os.path, time
# You can either hardcode your Discogs token and username here
# or you can pass them via the --token and/or --username arguments
DISCOGS_TOKEN = ""
DISCOGS_USERNAME = ""
# As of this release set PER to a value larger than your total collection.
# At one point I may add the ability to create paginated files.
#PER = "500"
#
# Setting up default values for variables
# No need to change anything below.
TYPE = ""
DISCOGS_API_URL = "https://api.discogs.com"
DATA_DIR = "./jsondata/"
ALL_RELEASES_DATA_FILE = "collection.json"
FOLDERS_DATA_FILE = "folders.json"
IMG_DIR = "./img/"
# Remove 1st argument from the
# list of command line arguments
argumentList = sys.argv[1:]
# Options
options = "ht:"
# Long options
long_options = [
"help",
"type=",
"updateall",
"updatecollection",
"updateimages",
"token=",
"username=",
]
# Main loop that takes your option and decides what it's going to do next.
def sleep(t, step=1, msg='Pausing'): # in seconds
pad_str = ' ' * len('%d' % step)
for i in range(t, 0, -step):
sys.stdout.write( '%s for %d seconds %s due to API limits.\r' % (msg, i, pad_str),)
sys.stdout.flush()
time.sleep(step)
print ''
def main():
try:
# Parsing argument
arguments, values = getopt.getopt(argumentList, options, long_options)
# checking each argument
for currentArgument, currentValue in arguments:
if currentArgument in ("-h", "--help"):
print("Displaying Help")
elif currentArgument in ("--updateall"):
print("Task: Updating ALL")
print("Task: Updating Collection")
download_collection_data()
sleep(5)
print("Task: Updating IMAGES")
update_images()
elif currentArgument in ("--updatecollection"):
print("Task: Updating Folder DATA")
download_collection_data()
elif currentArgument in ("--updateimages"):
print("Task: Updating IMAGES")
update_images()
elif currentArgument in ("--token"):
DISCOGS_TOKEN = currentValue
print("Token: ", DISCOGS_TOKEN)
elif currentArgument in ("--username"):
DISCOGS_USERNAME = currentValue
print("Username: ", DISCOGS_USERNAME)
except getopt.error as err:
# output error, and return with an error code
print(str(err))
def download_collection_data():
if DISCOGS_TOKEN == "" or DISCOGS_USERNAME == "":
print("Missing Discogs Token or Username")
exit()
FOLDERURL = (
DISCOGS_API_URL
+ "/users/"
+ DISCOGS_USERNAME
+ "/collection/folders/0/releases?per_page=500&sort=added&token="
+ DISCOGS_TOKEN
)
print("Downloading Collection List To " + ALL_RELEASES_DATA_FILE)
urllib.urlretrieve(FOLDERURL, DATA_DIR + ALL_RELEASES_DATA_FILE)
# This helper function downloads an image when given the url to the image and the release ID (to be used as the file basename.
# The "name" that can be passed to the function is merely for display while the scripts runs.
# Image files are located in the ./img directory and are named after the release ID and use the .jpeg extension. ie: ./image/<ID>.jpeg
def download_image(url, id, name):
if name == "":
name = "No name given."
print("Downloading image for " + name)
urllib.urlretrieve(url, IMG_DIR + str(id) + ".jpeg")
# The following function parses the list of all items and if an image file is not found it will be downloaded using download_image().
def update_images():
loop = 1
sleep_time = 1
with open(DATA_DIR + ALL_RELEASES_DATA_FILE) as releasedata:
releases = json.load(releasedata)
total_releases = len(releases["releases"])
for images in releases["releases"]:
print(str(loop) + "/" + str(total_releases) + ": ")
if os.path.exists(IMG_DIR + str(images["basic_information"]["id"]) + ".jpeg"):
print(images["basic_information"]["title"] + " already exists.")
else:
print(
str(loop) + "/" + str(total_releases) + ": "
+ " Downloading "
+ images["basic_information"]["title"]
+ " ("
+ IMG_DIR
+ str(images["basic_information"]["id"])
+ ".jpeg)"
)
download_image(
images["basic_information"]["cover_image"],
str(images["basic_information"]["id"]),
images["basic_information"]["title"],
)
sleep_time = 3
if not loop % 10:
sleep(5)
else:
sleep(sleep_time)
loop = loop + 1
# Lets run the main() program!
main()