-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetlist_to_spotify.py
71 lines (48 loc) · 1.69 KB
/
setlist_to_spotify.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
from bs4 import BeautifulSoup
import requests
import spotipy
import spotipy.util as util
# gather source info
source = requests.get(input("Enter setlist.fm link: ")).text
soup = BeautifulSoup(source, 'lxml')
# title from setlist.fm
setlist_title = soup.h1.text
# date of set for playlist title
day = soup.find('span', class_="day").text
month = soup.find('span', class_="month").text
year = soup.find('span', class_="year").text
date = f'{day} {month} {year}'
# title for Spotify playist
playlist_title = f'{setlist_title} - {date}'
playlist_title = playlist_title.replace('\n', ' ').strip()
# create list of artist and song titles
search_data = []
for search in soup.find_all('a', class_="songLabel"):
artist = search.get('title').split('by ')[1]
song = search.text
search_data.append(f'{artist} {song}')
# set up Spotipy
scope = 'playlist-modify-public'
username = input("Enter Spotify username: ")
token = util.prompt_for_user_token(
username, scope) # Follow Directions in Console
sp = spotipy.Spotify(auth=token)
# create playist
sp.user_playlist_create(user=username, name=playlist_title,
public=True, description='')
playlist_id = ''
playlists = sp.user_playlists(username)
playlists = playlists['items']
for playlist in playlists:
if playlist['name'] == playlist_title:
playlist_id += playlist['id']
track_ids = []
for song in search_data:
track_id = sp.search(q=song, limit=1, type='track')
if track_id['tracks']['items'] == []:
continue
track_id = track_id['tracks']['items'][0]['id']
track_ids.append(track_id)
sp.user_playlist_add_tracks(
user=username, playlist_id=playlist_id, tracks=track_ids)
print('Complete')