Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
213 lines (183 loc) · 8.03 KB
/
Copy pathmain.py
File metadata and controls
213 lines (183 loc) · 8.03 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pylast
import spotipy
from spotipy.oauth2 import SpotifyOAuth, SpotifyClientCredentials
from torchvision.transforms.functional import adjust_hue
from difflib import SequenceMatcher
from PIL import Image
from io import BytesIO
import credentials as c
import random
import base64
import time
import re
# constants
LASTFM_API_KEY: str = c.LASTFM_API_KEY
LASTFM_API_SECRET: str = c.LASTFM_API_SECRET
LASTFM_MY_ACCOUNT: str = c.LASTFM_MY_ACCOUNT
LASTFM_MY_PASSWORD: str = pylast.md5(c.LASTFM_MY_PASSWORD)
LASTFM_C_ACCOUNT: str = c.LASTFM_C_ACCOUNT
LASTFM_M_ACCOUNT: str = c.LASTFM_M_ACCOUNT
SPOTIFY_CLIENT_ID: str = c.SPOTIFY_CLIENT_ID
SPOTIFY_CLIENT_SECRET: str = c.SPOTIFY_CLIENT_SECRET
SPOTIFY_USERNAME: str = c.SPOTIFY_USERNAME
REDIRECT_URI: str = "http://localhost:8080/"
SCOPES: list = ["playlist-modify-public", "user-library-read", "ugc-image-upload"]
PLAYLIST_COVER_PATH: str = "playlist_cover.jpg"
COUNTER_PATH = "counter.txt"
LAST_WEEK_TRACKS_PATH = "last_week.txt"
class Track:
def __init__(self,
title: str = None,
artist: str = None,
uri: str = None):
if uri is not None:
search = my_spotify.search(q=uri, type="track", limit=1, market="US")
self.title = search["tracks"]["items"][0]["name"]
self.artist = search["tracks"]["items"][0]
return
self.title = clean(title, filter="track")
self.artist = clean(artist, filter="artist")
self.spotify_info = self.get_spotify_info()
def __str__(self):
return f"{self.title}[][][]{self.artist}"
def get_spotify_info(self):
return my_spotify.search(q=self.title + " " + self.artist,
type="track", limit=1, market="US")
def get_uri(self):
return self.spotify_info["tracks"]["items"][0]["uri"]
def spotify_search_success(self):
return SequenceMatcher(None,
self.artist.lower(),
self.spotify_info["tracks"]["items"][0]["artists"][0]["name"].lower()).ratio() * 0.5 \
+ SequenceMatcher(None,
self.title.lower(),
self.spotify_info["tracks"]["items"][0]["name"].lower()).ratio() * 0.5 >= 0.8
# authorization for last.fm and Spotify
def auth():
global my_spotify
network = pylast.LastFMNetwork(api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET,
username=LASTFM_MY_ACCOUNT, password_hash=LASTFM_MY_PASSWORD)
my_user: pylast.User = pylast.User(user_name=LASTFM_MY_ACCOUNT, network=network)
c_user: pylast.User = pylast.User(user_name=LASTFM_C_ACCOUNT, network=network)
m_user: pylast.User = pylast.User(user_name=LASTFM_M_ACCOUNT, network=network)
users: list = [my_user, c_user, m_user]
my_spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=c.SPOTIFY_CLIENT_ID,
client_secret=c.SPOTIFY_CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope=SCOPES))
return users
def add_to_playlist(counter: int, tracks: list):
my_spotify.user_playlist_create(user=c.SPOTIFY_USERNAME,
name=f"boolinmigster {int(counter / 2)}")
time.sleep(2)
playlist_uri = my_spotify.user_playlists(user=c.SPOTIFY_USERNAME, limit=1)["items"][0]["uri"]
my_spotify.playlist_add_items(
playlist_id=playlist_uri,
items=tracks)
my_spotify.playlist_upload_cover_image(playlist_id=playlist_uri, image_b64=get_image(PLAYLIST_COVER_PATH))
my_spotify.playlist_replace_items(playlist_id=c.SPOTIFY_PLAYLIST_URI, items=tracks)
def clean(item: str, filter: str):
if filter.lower() == "artist":
if "and" in item:
item = item[:item.index("and")]
if "&" in item:
item = item[:item.index("&")]
return item.rstrip()
if filter.lower() == "track":
if re.search("[(].+[)]$", item) and not ("mix" in item.lower() or "edit" in item.lower() or "dub" in item.lower()):
item = re.sub("[(].+[)]$", "", item)
item = item.replace("[", "").replace("]", "")
return item.rstrip()
raise Exception("Filter must be track or artist")
def contains_duplicates(data: list, filter: str):
seen = set()
for track in data:
if track.item.artist not in seen:
if filter == "artist":
seen.add(track.item.artist)
elif filter == "track":
seen.add(track.item.title)
return len(seen) != len(data)
def get_image(file_path: str):
img = Image.open(file_path)
img = adjust_hue(img=img, hue_factor=random.random() - 0.5)
buffer = BytesIO()
img.save(buffer, format="JPEG")
return base64.b64encode(buffer.getvalue())
def get_top_tracks(
user: pylast.User,
period,
limit,
read_last_week=False,
last_week_tracks=None):
c = limit
data_out = []
ignores = set()
lastfm_data: list = remove_duplicates(user.get_top_tracks(period=period, limit=c), filter="artist")
for data in lastfm_data:
temp = Track(title=str(data.item.title), artist=str(data.item.artist))
if not temp.spotify_search_success(): ignores.add(data)
while len(set(lastfm_data).difference(ignores)) < limit and c != 100:
for data in lastfm_data:
temp = Track(title=str(data.item.title), artist=str(data.item.artist))
if (read_last_week and clean(data.item.title, filter="track") in get_tracks(last_week_tracks)) \
or not temp.spotify_search_success():
ignores.add(data)
del temp
c += 1
lastfm_data = remove_duplicates(user.get_top_tracks(period=period, limit=c), filter="artist")
lastfm_data = list(set(lastfm_data).difference(ignores))
for track in lastfm_data:
data_out.append(Track(title=str(track.item.title), artist=str(track.item.artist)))
return data_out
def get_tracks(data: list):
return_data = [str] * len(data)
for i, j in enumerate(data):
return_data[i] = j[:j.index("[][][]")]
return return_data
def remove_duplicates(data: list, filter: str):
seen = set()
seen_out = set()
for track in data:
if filter == "artist" and track.item.artist not in seen:
seen.add(track.item.artist)
seen_out.add(track)
elif filter == "track" and track.item.title not in seen:
seen.add(track.item.title)
seen_out.add(track)
return list(seen_out)
def run(read_last_week=False, last_weeks_tracks=None):
global last_file
users = auth()
if not read_last_week:
last_file = open(LAST_WEEK_TRACKS_PATH, "w")
top_tracks = []
else:
top_tracks = [Track(
title=track[:track.index("[][][]")],
artist=track[(track.index("[][][]") + 6):]).get_uri() for track in last_weeks_tracks]
for user in users:
for track in get_top_tracks(user=user, period=pylast.PERIOD_7DAYS, limit=3,
read_last_week=read_last_week, last_week_tracks=last_weeks_tracks):
if len(track.spotify_info["tracks"]["items"]) == 0:
raise Exception(f"{track} not found by Spotify search.")
top_tracks.append(track.get_uri())
if not read_last_week: last_file.write(str(track) + "\n")
random.shuffle(top_tracks)
if not read_last_week: last_file.close()
return top_tracks
def main():
with open(COUNTER_PATH) as f: counter = int(f.read())
counter += 1
if counter % 2 == 0:
with open(LAST_WEEK_TRACKS_PATH) as f: lines = f.readlines()
lines = [line.rstrip() for line in lines]
del lines[-1]
top_tracks = run(read_last_week=True, last_weeks_tracks=lines)
add_to_playlist(counter=counter, tracks=top_tracks)
else:
run()
with open(COUNTER_PATH, 'w') as f:
f.write(str(counter))
if __name__ == "__main__":
main()