-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_playlists.py
64 lines (53 loc) · 2.3 KB
/
create_playlists.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
from bs4 import BeautifulSoup
import spotipy
import spotipy.util as util
import urllib
import os
client_id = os.environ.get('CLIENT_ID')
client_secret = os.environ.get('CLIENT_SECRET')
redirect_uri = os.environ.get('REDIRECT_URI')
username = 'apuschin'
token = util.prompt_for_user_token(username,
'playlist-modify-public',
client_id, client_secret, redirect_uri)
spotify = spotipy.Spotify(auth=token)
page = 'https://www.bbc.co.uk/programmes/p0093v9l'
while page:
print('----------------------------------')
print('processing: ' + page)
html_source = urllib.request.urlopen(page)
soup = BeautifulSoup(html_source, 'html.parser')
title = soup.find('div', class_="island").find('h1', class_="no-margin").text
interviewee = title.split(',', 1)[0]
print('interviewee: ' + interviewee)
# find songs
songs = [] # a tuple of (song, artist)
for h3 in soup.find_all('h3'):
# find all span tags inside
span = h3.find('span', class_='artist')
if span:
artist = span.text
song = h3.parent.contents[3].find('span').text
songs.append((song, artist))
print("found " + str(len(songs)) + " songs")
## still broken - autocompleteion, elpy isnt automatic, flycheck doesn't work
if songs:
# get spotify_uris from songs
spotify_uris = []
for x in range(0, len(songs)):
query = songs[x][0] + ' '+songs[x][1]
results = spotify.search(q=query, type='track')
if results['tracks']['total'] != 0:
uri = results['tracks']['items'][0]['uri']
spotify_uris.append(uri)
else:
print("could not find track for query " + query)
print("found " + str(len(spotify_uris)) + " spotify_uris")
if spotify_uris:
# create a playlist of the spotify uris
playlist = spotify.user_playlist_create(username,
"Desert Island Discs: " + interviewee,
public=True)
spotify.user_playlist_add_tracks(username, playlist['id'], spotify_uris)
page = soup.find_all('h4', class_='programme__titles')[0] \
.find('a').get('href')