-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
107 lines (94 loc) · 3.19 KB
/
generate.py
File metadata and controls
107 lines (94 loc) · 3.19 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
import pandas as pd
import re
FEATURE_PATTERNS = [
r"\s+feat\.?\s+",
r"\s+ft\.?\s+",
r"\s+featuring\s+"
]
def create_songlist(start, end, chart, amount, omit=None):
print("Reading Chart...")
df = pd.read_csv(chart)
filtered = df[
(df['Date'] >= start) &
(df['Date'] <= end)
]
i = 1
sl = set()
songs = []
print("Reading Chart Completed!")
print("Creating Songlist...0%")
while len(sl) < amount and i <= amount:
top = filtered[filtered["Rank"] == i].drop_duplicates()
for _, row in top.iterrows():
title = row['Song']
artist = row['Artist']
image = row['Image URL']
length = len(sl)
dup = (title, artist) in sl
if not dup:
if omit:
if [title, artist] not in omit:
sl.add((title, artist))
songs.append([length + 1, image, title, artist])
else:
print("User Deleted Song...Skipping")
else:
sl.add((title, artist))
songs.append([length + 1, image, title, artist])
p = float(length / amount) * 100
if p < 100:
print(f"Creating Songlist...{p:.2f}%")
i += 1
p = 100
print(f"Creating Songlist...{p}%")
print("Songlist Completed!")
return songs
def cut_playlist(urilist, amount):
while len(urilist) > amount:
urilist.pop()
print("Cutting Process Completed!")
def extract_main_artist(artist_str):
s = artist_str.strip()
# Lowercase copy for searching
lower_s = s.lower()
for pattern in FEATURE_PATTERNS:
match = re.search(pattern, lower_s)
if match:
# Slice original string to preserve capitalization
cut_index = match.start()
return s[:cut_index].strip()
# No feature pattern — return unchanged
return s
def generate_playlist(songs, sp, omit):
urilist = []
failed = []
length = len(songs)
print("Obtaining Track Ids...0%")
for i in range(length):
title = songs[i][2]
artist = songs[i][3]
if omit and [title, artist] in omit:
continue
query = f"track:{title} artist:{extract_main_artist(artist)}"
result = sp.search(q=query, type="track", limit=1)
track = result.get("tracks", {}).get("items", [])
try:
urilist.append(track[0]["uri"])
except IndexError:
failed.append([songs[i][1], title, artist])
print("Could not obtain song, moving on to next")
print(f'Title: {title}, Artist: {artist}')
p = float(len(urilist) / length) * 100
if p < 100:
print(f"Obtaining Track Ids...{p:.2f}%")
p = 100
print(f"Obtaining Track Ids...{p}%")
print("Obtained All Track Ids!")
return [urilist, failed]
def get_dates(chart):
df = pd.read_csv(chart, dtype={4: str})
return (str(df['Date'].min()), str(df['Date'].max()))
def set_default_art(songs):
for i in range(len(songs)):
if songs[i][1] == '#':
songs[i][1] = 'static/images/default_art.png'