-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTracks.py
90 lines (73 loc) · 3.01 KB
/
getTracks.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
#!/bin/python
import json
import string
import requests
from bs4 import BeautifulSoup
import re
import wikipedia
import sys
''' Run getTracks.py to get playlist track data '''
def get_spotify_tracks():
''' Update weekly track list for Smookify. Returns JSON tracks string '''
# get token
r = requests.post('https://accounts.spotify.com/api/token',
data={'grant_type': 'client_credentials'},
headers={
'Authorization': 'Basic='}) # this will no longer work
token_dict = json.loads(r.content)
p = requests.get('https://api.spotify.com/v1/users/12171649066/playlists/160cCIcwQXfhKVxcD4mBiz/tracks',
headers={'Authorization': '{} {}'.format(token_dict['token_type'], token_dict['access_token'])},
params={'fields': 'items(track(name,album(artists)))'})
return json.loads(p.content)
def parse_spotify_tracks(tracks=None):
if not tracks:
tracks = get_spotify_tracks()
parsed = {}
for item in tracks['items']:
t = item['track']
try:
parsed[t['name']] = t['album']['artists'][0]['name']
except KeyError:
print 'error on {}'.format(t['name'])
return parsed
def get_track_details(track, artist):
# Get year
try:
r = requests.get('https://musicbrainz.org/search?query={}+{}&type=release&method=indexed'.format(track.encode('utf8', 'ignore'), artist.encode('utf8', 'ignore')))
soup = BeautifulSoup(r.content, 'lxml')
match = soup.find_all('tr')[1]
year = re.search("[0-9]+", str(match.find_all('td')[5])).group(0)
except AttributeError:
year = 'Year Not Found'
# Get genre
try:
w = requests.get('https://en.wikipedia.org/wiki/{}'.format(
wikipedia.search('{} {} {}'.format(track, artist, year))[0].replace(' ', '_')))
soupW = BeautifulSoup(w.content, 'lxml')
infobox = soupW.find_all('table', class_='infobox')[0]
for row in infobox.find_all('tr'):
if row.th:
if 'Music genre' in str(row.th):
genre = row.td.get_text().strip().split('\n')[0]
for ch in string.punctuation:
genre = genre.split(ch)[0]
return year, genre
except:
return year, 'Genre Not Found'
if __name__ == '__main__':
tracks = parse_spotify_tracks()
genre_test = []
try:
for track, artist in tracks.items():
year, genre = get_track_details(track, artist)
json_obj = {'track': track, 'artist': artist, 'year': year, 'genre': genre}
print str(json_obj).encode('utf8')
genre_test.append(json_obj)
with open('./src/genre-test.json', 'w') as f:
f.write(json.dumps(genre_test))
print 'success'
except Exception as e:
with open('./src/genre-test.json', 'w') as f:
f.write(json.dumps(genre_test))
print 'program exited with status 0', e.message
sys.exit(0)