tmp
This commit is contained in:
parent
c690162983
commit
7df0c0a0ff
49 changed files with 1439861 additions and 169 deletions
160
spotifyFetch.py
160
spotifyFetch.py
|
|
@ -1,49 +1,147 @@
|
|||
import common as cm
|
||||
import os
|
||||
import requests
|
||||
from loadArtwork import load
|
||||
from spotifyAuth import authenticate
|
||||
from common import *
|
||||
|
||||
os.makedirs(cm.data_dir, exist_ok=True)
|
||||
FETCH_STEP = 50
|
||||
token = ''
|
||||
|
||||
fetch_step = 50
|
||||
|
||||
playlists = []
|
||||
playlist_items = {}
|
||||
def fetch(endpoint, method='GET', body=None):
|
||||
url = f'https://api.spotify.com/{endpoint}'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {token}',
|
||||
}
|
||||
|
||||
def loadPLaylists(pl):
|
||||
response = requests.request(method, url, headers=headers, json=body)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
|
||||
def fetch_playlists(user_id):
|
||||
print("Fetching playlists")
|
||||
endpoint = f"v1/me/playlists"
|
||||
pl_total = fetch(f'{endpoint}?limit=1')['total']
|
||||
pl_count = 0
|
||||
pl_total = 0
|
||||
|
||||
pl_total = cm.fetch('v1/me/playlists?limit=1')['total']
|
||||
playlists = []
|
||||
|
||||
while pl_count < pl_total:
|
||||
res = cm.fetch(f'v1/me/playlists?limit={fetch_step}&offset={pl_count}')
|
||||
pl += res['items']
|
||||
pl_count += fetch_step
|
||||
res = fetch(f'{endpoint}?limit={FETCH_STEP}&offset={pl_count}')
|
||||
playlists += res['items']
|
||||
pl_count += FETCH_STEP
|
||||
|
||||
return playlists
|
||||
|
||||
|
||||
cm.saveData(playlists, 'playlists')
|
||||
|
||||
|
||||
def loadPlaylistItems(playlistId, traks):
|
||||
items = cm.fetch(f'v1/playlists/{playlistId}/tracks?fields=total')['total']
|
||||
def fetch_playlist_items(playlist_id):
|
||||
endpoint = f"v1/playlists/{playlist_id}/tracks"
|
||||
items = fetch(f'{endpoint}?fields=total')['total']
|
||||
loaded = 0
|
||||
tracks = []
|
||||
|
||||
while loaded < items:
|
||||
res = cm.fetch(f'v1/playlists/{playlistId}/tracks?limit={fetch_step}&offset={items}')
|
||||
traks += res['items']
|
||||
loaded += fetch_step
|
||||
res = fetch(f'{endpoint}?limit={FETCH_STEP}&offset={loaded}')
|
||||
|
||||
tracks += res['items']
|
||||
loaded += FETCH_STEP
|
||||
|
||||
print(items)
|
||||
|
||||
loadPLaylists(playlists)
|
||||
print(len(playlists))
|
||||
return tracks
|
||||
|
||||
|
||||
for pl in playlists:
|
||||
id = pl['id']
|
||||
name = pl['name']
|
||||
playlist_items[name] = []
|
||||
def fetch_tracks(user_id):
|
||||
print("Fetching tracks")
|
||||
endpoint = f"v1/me/tracks"
|
||||
total = fetch(f"{endpoint}?limit=1&market=ES")['total']
|
||||
current = 0
|
||||
tracks = []
|
||||
|
||||
loadPlaylistItems(id, playlist_items[name])
|
||||
while total > current:
|
||||
res = fetch(f"{endpoint}?limit={FETCH_STEP}&offset={current}&market=ES")
|
||||
tracks += res['items']
|
||||
current += FETCH_STEP
|
||||
|
||||
cm.saveData(playlist_items, "playlistTraks")
|
||||
save_data(tracks, "tracks")
|
||||
return tracks
|
||||
|
||||
|
||||
def fetch_tracks_top():
|
||||
print("Fetching top tracks")
|
||||
total = fetch(f'v1/me/top/tracks?fields=total')['total']
|
||||
current = 0
|
||||
tracks = []
|
||||
|
||||
while current < total:
|
||||
res = fetch(f'v1/me/top/tracks?limit={FETCH_STEP}&offset={current}&time_range=long_term')
|
||||
tracks += res['items']
|
||||
current += FETCH_STEP
|
||||
|
||||
save_data(tracks, "top_tracks")
|
||||
return tracks
|
||||
|
||||
|
||||
def fetch_artists_top():
|
||||
print("Fetching top artists")
|
||||
|
||||
total = fetch(f'v1/me/top/artists?fields=total')['total']
|
||||
current = 0
|
||||
artists = []
|
||||
|
||||
while current < total:
|
||||
res = fetch(f'v1/me/top/artists?limit={FETCH_STEP}&offset={current}&time_range=long_term')
|
||||
artists += res['items']
|
||||
current += FETCH_STEP
|
||||
|
||||
save_data(artists, "top_artists")
|
||||
return artists
|
||||
|
||||
|
||||
def fetch_all_playlist_data(user_id):
|
||||
playlists = fetch_playlists(user_id)
|
||||
|
||||
save_data(playlists, 'playlists')
|
||||
|
||||
print(len(playlists))
|
||||
|
||||
playlist_items = {}
|
||||
|
||||
print("Fetching playlists items")
|
||||
|
||||
index = 0
|
||||
for pl in playlists:
|
||||
pl_id = pl['id']
|
||||
name = pl['name']
|
||||
playlist_items[name] = fetch_playlist_items(pl_id)
|
||||
print(f"{index} - {len(playlist_items[name])}")
|
||||
index += 1
|
||||
|
||||
save_data(playlist_items, "playlistTracks")
|
||||
|
||||
|
||||
def get_user_data():
|
||||
print("Fetching user data")
|
||||
|
||||
user_profile = fetch(f'v1/me')
|
||||
user_id = user_profile['id']
|
||||
user_data = fetch(f'v1/users/{user_id}')
|
||||
|
||||
save_data(user_profile, "user_profile")
|
||||
save_data(user_data, "user_data")
|
||||
return user_id
|
||||
|
||||
|
||||
def fetch_data():
|
||||
global token
|
||||
token = authenticate()
|
||||
|
||||
user_id = get_user_data()
|
||||
|
||||
fetch_tracks(user_id)
|
||||
fetch_all_playlist_data(user_id)
|
||||
fetch_tracks_top()
|
||||
fetch_artists_top()
|
||||
|
||||
load()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
fetch_data()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue