-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
213 lines (158 loc) · 7.65 KB
/
core.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
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 interactions
import spotipy_oath
import requests
from bs4 import BeautifulSoup
from unidecode import unidecode
def searchSpotify(sp, name, rType = 'artist', limit=10):
names = []
ids = []
results = sp.search(q=name,type=rType, limit=limit)
for result in results[rType + 's']['items']:
if rType == 'track':
names.append([result['name'], result['artists'][0]['name']])
else:
names.append([result['name']])
ids.append(result['uri'])
return [names, ids]
def artistTopTracksSpotify(sp, id):
names = []
ids = []
results = sp.artist_top_tracks(id)
for track in results['tracks']:
names.append([track['name'], track['artists'][0]['name']])
ids.append(track['uri'])
return [names, ids]
def urlToID(sp):
print('Enter a spotify track URL or track ID (leave blank to cancel):')
while True:
inp = input('> ')
if not inp:
return None
try:
return sp.track(inp)['uri']
except sp.SpotifyException:
print('Wrong URL or ID, please try again')
def selectTrack(sp, trackList, track, artist):
if not trackList[0]:
print('No results found')
return findSpotifyID(sp, track, artist)
interactions.enumerateMenu([f"'{x[0]}' - '{x[1]}'" for x in trackList[0]] + ['Not in the listed tracks'])
selection = interactions.getInt(1, 'track', len(trackList[0]) + 1)
if selection == len(trackList[0]) + 1:
return findSpotifyID(sp, track, artist)
else:
return trackList[1][selection-1]
def findSpotifyID(sp, track, artist, firstLookup=False, ignoreNF=False):
if firstLookup:
initialSearch = searchSpotify(sp, track, 'track')
for idx, cTr in enumerate(initialSearch[0]):
if unidecode(cTr[0].lower()) == unidecode(track.lower()) and unidecode(cTr[1].lower()) == unidecode(artist.lower()):
print(f"\tSame matching track found on Spotify")
return initialSearch[1][idx]
if ignoreNF:
return None
interactions.clear()
choices = ['Ignore track', 'Spotify search for track name', 'Spotify search for artist name', 'Enter spotify track URL/ID']
print (f"\nThe spotify URL for '{track}' of '{artist}' has not be found, what do you want to do ?", )
interactions.enumerateMenu(choices)
selection = interactions.getInt(1, 'choice', len(choices))
if selection == 1:
return None
elif selection == 2:
interactions.clear()
print(f"List of corresponding tracks for '{track}' of '{artist}':")
return selectTrack(sp, searchSpotify(sp, track, 'track'), track, artist)
elif selection == 3:
results = searchSpotify(sp, artist)
if not results[0]:
print('No results found')
return findSpotifyID(sp, track, artist)
interactions.clear()
print(f"List of corresponding artists for '{artist}':")
interactions.enumerateMenu([f"'{x[0]}'" for x in results[0]] + ['Not in the listed artists'])
selection = interactions.getInt(1, 'artist', len(results[0]) + 1)
if selection == len(results[0]) + 1:
return findSpotifyID(sp, track, artist, True)
else:
artistID = results[1][selection-1]
interactions.clear()
print(f"Most popular tracks for '{artist}':")
return selectTrack(sp, artistTopTracksSpotify(sp, artistID), track, artist)
elif selection == 4:
spotId = urlToID(sp)
if not spotId:
return findSpotifyID(sp, track, artist)
return spotId
def scUrlToSpotID(sp, url):
print(f"\tGetting spotify URI from 'https://www.senscritique.com{url}'")
soup = BeautifulSoup(requests.get(f'https://www.senscritique.com{url}').text, features="html.parser")
spotID = soup.find('div', class_='d-media-music')
if spotID:
# Data from SensCritique can be expired so we need to check that the URI still exist
try:
sp.track(spotID['data-sc-play-value'])
return spotID['data-sc-play-value']
except sp.SpotifyException:
return None
else:
return None
def scGetType(soup):
# Expected: 'Sondages', 'Listes'
return soup.find_all('a', class_='lahe-breadcrumb-anchor')[-1].text
def scGetTracks(sp, pid, lType, soup, URIfromSC=False, ignoreNF=False):
tracks = []
if lType == 'Sondages':
itemPerPage = len(soup.find_all('li', class_='elpo-item'))
itemType = soup.find('a', class_='elco-anchor')['href'].split('/')[1]
pageNumber = 0
if itemType == 'morceau':
while itemPerPage != 0:
pageNumber += 1
if pageNumber != 1:
soup = BeautifulSoup(requests.get(f'https://www.senscritique.com/sc2/top/resultats/{pid}/page-{pageNumber}.ajax?limit=1000', headers= {"x-requested-with": "XMLHttpRequest"}).text, features="html.parser")
itemPerPage = len(soup.find_all('li', class_='elpo-item'))
for el in soup.find_all('li', class_='elpo-item'):
title = el.find('a', class_='elco-anchor').text
artist = el.find('a', class_='elco-baseline-a').text
print(f"Track: '{title}' of '{artist}':")
spotUrl = None
if URIfromSC:
spotUrl = scUrlToSpotID(sp, el.find('a', class_='elco-anchor')['href'])
if not spotUrl:
print("\tFailed to get URI for track from SensCritique")
if not spotUrl:
spotUrl = findSpotifyID(sp, title, artist, True, ignoreNF)
if not spotUrl:
print(f"\tTrack is ignored and will not be added to the playlist")
continue
print(f"\tTrack added (uri: {spotUrl})")
tracks.append(spotUrl)
else:
print('This is not a list of tracks')
elif lType == 'Listes':
itemPerPage = 30
itemInfos = soup.find('div', class_ = "elme-listAuthor")
itemNumbers = int(itemInfos.find('span').text)
itemType = itemInfos.find('h2').text.split()[-1]
if itemType == 'morceaux':
for i in range(1, itemNumbers//itemPerPage + (itemNumbers % itemPerPage > 0) + 1):
if i != 1:
soup = BeautifulSoup(requests.get(f'https://www.senscritique.com/sc2/liste/{pid}/page-{i}.ajax').text, features="html.parser")
for el in soup.find_all('li', class_='elli-item'):
title = el.find('a', class_='elco-anchor').text.replace("’", "'").replace("Ç","C")
artist = el.find('a', class_='elco-baseline-a').text
spotUrl = el.find('div', class_='d-media-music')
print(f"Track: '{title}' of '{artist}':")
if not spotUrl:
print("\tFailed to get URI for track from SensCritique")
spotUrl = findSpotifyID(sp, title, artist, True, ignoreNF)
if not spotUrl:
print(f"\tTrack '{title}' is ignored and will not be added to the playlist")
continue
else:
spotUrl = spotUrl['data-sc-play-value']
print(f'\tTrack added ({spotUrl})')
tracks.append(spotUrl)
else:
print('This is not a list of tracks')
return tracks