-
Notifications
You must be signed in to change notification settings - Fork 1
/
ams.py
153 lines (119 loc) · 4.65 KB
/
ams.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
#########################
# Imports
#########################
from bs4 import BeautifulSoup
from fuzzywuzzy import fuzz
import bs4, requests, json
from secrets import *
#########################
# Headers
#########################
headers = {
'Authorization': 'Bearer ' + ACCESS_TOKEN,
}
#########################
# Helper Functions
#########################
def get_tracks(url):
# Parse Webpage
html = requests.get(url).text
soup = bs4.BeautifulSoup(html, 'html.parser')
data = json.loads(soup.find(id="shoebox-ember-data-store").get_text())
name = data['data']['attributes']['name']
playlist = data['included']
# Get Track Names and Artists from Playlist
tracks = []
for track in playlist:
try:
tracks.append({
'name': track['attributes']['name'],
'artist': track['attributes']['artistName']
})
except:
continue
return name, tracks
def get_spotify_playlist(target_name):
# Get All Playlists
response = requests.get('https://api.spotify.com/v1/me/playlists', headers=headers)
playlists = json.loads(response.text)['items']
target_id = None
# Search for Playlist in Existing Playlists
for playlist in playlists:
if str(playlist['name']) == target_name:
target_id = str(playlist['id'])
# Create Playlist if it DNE
if target_id == None:
response = requests.post('https://api.spotify.com/v1/users/%s/playlists' % USER_ID, headers=headers, data='{"name":"%s","public":false}' % target_name)
target_id = str(json.loads(response.text)['id'])
return target_id
def get_spotify_playlist_tracks(target_id):
# Get All Teacks in Playlist
response = requests.get("https://api.spotify.com/v1/users/%s/playlists/%s/tracks" % (USER_ID, target_id), headers=headers)
playlist = json.loads(response.text)['items']
# Get Track Names, Artists, and URIs from Playlist
tracks = []
for track in playlist:
tracks.append({
'name': track['track']['name'],
'artist': track['track']['artists'][0]['name'],
'uri': track['track']['uri']
})
return tracks
def get_spotify_track_uri(target_name, target_artist):
# Parse Apple Music Song Name
if "(feat." in target_name:
index = target_name.find("(feat.")
target_artist += target_name[index + len("(feat."):-1]
target_name = target_name[:index]
# Get Search Results
params = (
('q', target_name),
('type', 'track'),
)
response = requests.get('https://api.spotify.com/v1/search', headers=headers, params=params)
results = json.loads(response.text)['tracks']['items']
# Return Best Fuzzy Match
scores = []
factor = 1
for track in results:
result = ""
for artist in track['artists']:
result += artist['name'] + " "
scores.append(fuzz.ratio(result.strip(), target_artist) * factor)
factor -= 0.02
return results[scores.index(max(scores))]['uri']
def delete_spotify_playlist_tracks(tracks, target_id):
# Generate Data String
uris = ""
for track in tracks:
uris += '{"uri":"' + str(track['uri']) + '"},'
data = '{"tracks":[' + uris[:-1] + "]}"
response = requests.delete('https://api.spotify.com/v1/users/%s/playlists/%s/tracks' % (USER_ID, target_id), headers=headers, data=data)
def add_spotify_playlist_tracks(tracks, target_id):
# Support 100 Track Limit
if len(tracks) > 100:
add_spotify_playlist_tracks(tracks[:100], target_id)
add_spotify_playlist_tracks(tracks[100:], target_id)
# Search for Tracks on Spotify
uris = ""
for track in tracks:
try:
uris += get_spotify_track_uri(track['name'], track['artist']) + ","
except:
print("Couldn't add " + track['name'] + " by " + track['artist'])
params = (
('uris', uris[:-1]),
)
response = requests.post('https://api.spotify.com/v1/users/%s/playlists/%s/tracks' % (USER_ID, target_id), headers=headers, params=params)
#########################
# Main Function
#########################
def ams(url):
name, cur_tracks = get_tracks(url)
target_id = get_spotify_playlist(name)
old_tracks = get_spotify_playlist_tracks(target_id)
add_tracks = [ track for track in cur_tracks if track not in old_tracks ]
del_tracks = [ track for track in old_tracks if track not in cur_tracks ]
print("Syncing " + name + "...")
delete_spotify_playlist_tracks(del_tracks, target_id)
add_spotify_playlist_tracks(add_tracks, target_id)