-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
discogs-single-song.py
executable file
·77 lines (62 loc) · 2.13 KB
/
discogs-single-song.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import requests
DISCOGS_URL = "https://www.discogs.com"
DISCOGS_API = "https://api.discogs.com/database/search?"
def discogs_api(song=None, artist=None):
"""retrieves the top search result from discogs for a given song &
artist combination.
"""
header = {"Authorization": "Discogs token={}".format(DISCOGS_TOKEN.strip())}
payload = {"per_page": 1, "page": 1}
if artist is not None:
payload.update({"artist": artist})
if song is not None:
payload.update({"release_title": song})
req = requests.get(DISCOGS_API, headers=header, params=payload)
return req.json()["results"]
def get_song_info(song, artist):
"""Searches the discogs database using the data for the given index in the
data frame.
"""
if type(song) == float:
return
for u in ["unknown", "unidentified", "untitled", "medley"]:
if u in song.lower():
return
if artist == "Unidentified":
artist = None
data = discogs_api(song, artist)
if len(data) == 0:
return
else:
data = data[0]
results = []
for key in ["genre", "style", "year", "master_url"]:
if key not in data:
continue
elif type(data[key]) != list:
if data[key] is not None:
results.append((key, data[key]))
elif len(data[key]) == 0:
continue
else:
results.append((key, data[key][0]))
if "uri" in data:
if data["uri"] != "None":
results.append((key, "{}{}".format(DISCOGS_URL, data["uri"])))
return results, data
with open(
".token"
) as token_file: # Get an API token from discogs and save as token.txt
DISCOGS_TOKEN = token_file.read()
song = input("Please enter song name: ")
artist = input("Please enter artist name: ")
# For example: results, data = get_song_info(song="planet caravan", artist="black sabbath")
results, data = get_song_info(song=song, artist=artist)
print(json.dumps(data, indent=2, sort_keys=True))
print("")
print("Summary:")
for res in results:
print("{}: {}".format(res[0], res[1]))