This commit is contained in:
IlyaShurupov 2024-07-07 14:23:05 +03:00
parent e7848b70fe
commit 14b853ae34
4 changed files with 104 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
prefetched
__pycache__

47
spotifyAuth.py Normal file
View file

@ -0,0 +1,47 @@
import requests
import base64
# Method 2: Using regular expressions
import re
# Your Spotify app credentials
client_id = '3b4db45bb66b45e9a2532989c8034332'
client_secret = '4ced4e4575094b749834a60996680c6c'
# Encode client_id and client_secret in Base64
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
# Spotify token endpoint
token_url = 'https://accounts.spotify.com/api/token'
# Parameters for requesting access token
token_data = {
"grant_type": "client_credentials",
"client_secret": client_secret,
"client_id": client_id,
"scope": "user-read-private, playlist-read-private, playlist-read-collaborative",
}
# HTTP headers including encoded client_id and client_secret
token_headers = {
# "Authorization": f"Basic {client_creds_b64.decode()}"
"Content-Type" : "application/x-www-form-urlencoded"
}
# Requesting access token
token_response = requests.post(token_url, data=token_data, headers=token_headers)
# Handling response
if token_response.status_code not in range(200, 299):
print(f"Failed to retrieve access token: {token_response.status_code}")
print(token_response.json())
token = token_response.json()['access_token']
token_type = token_response.json()['token_type']
token_time = token_response.json()['expires_in']
if __name__ == "__main__":
print(token_response.json())

49
spotifyFetch.py Normal file
View file

@ -0,0 +1,49 @@
import common as cm
import os
os.makedirs(cm.data_dir, exist_ok=True)
fetch_step = 50
playlists = []
playlist_items = {}
def loadPLaylists(pl):
pl_count = 0
pl_total = 0
pl_total = cm.fetch('v1/me/playlists?limit=1')['total']
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
cm.saveData(playlists, 'playlists')
def loadPlaylistItems(playlistId, traks):
items = cm.fetch(f'v1/playlists/{playlistId}/tracks?fields=total')['total']
loaded = 0
while loaded < items:
res = cm.fetch(f'v1/playlists/{playlistId}/tracks?limit={fetch_step}&offset={items}')
traks += res['items']
loaded += fetch_step
print(items)
loadPLaylists(playlists)
print(len(playlists))
for pl in playlists:
id = pl['id']
name = pl['name']
playlist_items[name] = []
loadPlaylistItems(id, playlist_items[name])
cm.saveData(playlist_items, "playlistTraks")

6
stat.py Normal file
View file

@ -0,0 +1,6 @@
import common as cm
playlists = cm.getData('playlists')
for pl in playlists:
print(pl['name'])