-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwantlist.py
executable file
·162 lines (129 loc) · 3.95 KB
/
wantlist.py
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
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
import pprint
import discogs_client
import argparse
import sys
# Todo:
# 1. Implement OAuth
# 2. Refactoring:
# 1. reading in main() ( will become handleUserInput())
# 2. findAlbum() called from addAlbum()
# 3. Add handleUserInput()
# 3. Add Progress bar
def readFile(filename, delimiter, revert, album_format):
with open(filename) as f:
for line in f:
album = findAlbum(line, delimiter, album_format)
if album:
if revert is True:
removeAlbum(album)
else:
addAlbum(album)
def readSTDIN(stdin, delimiter, revert, album_format):
for line in stdin:
album = findAlbum(line, delimiter, album_format)
if album:
if revert is True:
removeAlbum(album)
else:
addAlbum(album)
def findAlbum(line, delimiter, album_format):
artist = line[0:line.find(delimiter)]
album = line[line.find(delimiter)+len(delimiter):]
results = d.search(
artist=artist,
release_title=album,
format=album_format,
type="release"
)
if len(results) > 0:
return results
else:
print(album + ' by ' + artist + ' was not found as ' + album_format)
return None
def addAlbum(albums):
me = d.identity()
for album in albums:
pprint.pprint(album)
me.wantlist.add(album)
def removeAlbum(albums):
me = d.identity()
for album in albums:
pprint.pprint(album)
me.wantlist.remove(album)
def main():
delimiter = ' || '
parser = argparse.ArgumentParser()
parser.add_argument(
'STDIN',
nargs='?',
type=argparse.FileType('r'),
default=sys.stdin
)
parser.add_argument(
"-u",
"--user-token",
default=None,
type=str,
dest="USER_TOKEN",
help="User token to connect with the Discogs API. You can obtain a token from https://www.discogs.com/settings/developers"
)
parser.add_argument(
"-f",
"--file",
default=None,
type=str,
dest="FILE",
help="File with album-titles and artists delimited by '"+delimiter+"' (not including quotes) or a chosen delimiter"
)
parser.add_argument(
"-d",
"--delimiter",
default=None,
type=str,
dest="DELIMITER",
help="Delimiter used in the specfied file. Defaults to '"+delimiter+"' (not including quotes) when not specified"
)
parser.add_argument(
"-v",
'--version',
action='version',
version='%(prog)s 0.1'
)
parser.add_argument(
"-r",
"--revert",
action='store_true',
dest="REVERT",
help="Removes the specified files from your wantlist"
)
parser.add_argument(
"--format",
type=str,
dest="FORMAT",
help="Album format you want in your wantlist (eg. Vinyl, CD, 12\", Cassette...) For a list of all formats go to https://www.discogs.com/help/formatslist"
)
options = parser.parse_args()
input_count = sum([bool(options.FILE), not sys.stdin.isatty()])
if input_count is 0:
options.FILE = input("Please enter the location of your list file: ")
elif input_count > 1:
raise RuntimeError("Please specify only file or stdin")
if not options.USER_TOKEN:
options.USER_TOKEN = input("Please enter your Discogs Consumer Token: ")
global d
d = discogs_client.Client(
'Discogs-Wantlist-Adder/0.1',
user_token=options.USER_TOKEN
)
album_format = "Vinyl"
if options.FORMAT:
album_format = options.FORMAT
if options.DELIMITER:
delimiter = options.DELIMITER
if(options.FILE):
readFile(options.FILE, delimiter, options.REVERT, album_format)
else:
readSTDIN(options.STDIN, delimiter, options.REVERT, album_format)
if __name__ == "__main__":
main()