forked from XDGFX/spotr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotr.py
91 lines (61 loc) · 2.41 KB
/
spotr.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
import spotipy
import spotipy.util as util
import cred
import requests
from os import path
username = cred.username
scope = 'playlist-read-private playlist-modify-private'
client_id = cred.client_id
client_secret = cred.client_secret
# Modified lines in util.py:
# try:
# import webbrowser
# webbrowser.open(auth_url)
# print("Opened %s in your browser" % auth_url)
# except:
# Removed to prevent Python trying to open a webbrowser
print('Attempting to fetch token...')
token = util.prompt_for_user_token(
username, scope, client_id, client_secret, redirect_uri="http://localhost")
if token:
print('Token fetch successful')
sp = spotipy.Spotify(auth=token)
print('Requesting playlists...')
playlists = sp.user_playlists(username)
for playlist in playlists['items']:
if playlist['name'] == cred.playlist:
print('Found playlist: ' + cred.playlist)
playlist_id = playlist['id']
print('ID: ' + playlist_id)
data = sp.user_playlist(username, playlist['id'],
fields="tracks")
tracks = data['tracks']['items']
links = []
remove = []
seen = []
for track in tracks:
isrc = track['track']['external_ids']['isrc']
url = "https://api.deezer.com/2.0/track/isrc:" + isrc
response = requests.get(url)
if response.status_code != 200:
print('Unable to find isrc: ' + isrc)
else:
links.append(response.json()['link'])
remove.append(
track['track']['uri'].lstrip('spotify:track:'))
if path.exists(cred.filename):
print(cred.filename + ' already exists! Appending to file...')
with open(cred.filename, 'r') as f:
seen = f.readlines()
seen = [line.strip('\n') for line in seen]
with open(cred.filename, 'a+') as f:
for link in links:
if link not in seen:
f.write("%s\n" % link)
print('Appended tracks to file')
print('Removing tracks from Spotify...')
sp.user_playlist_remove_all_occurrences_of_tracks(
username, playlist_id, remove)
print('Success!')
else:
print('No token supplied')