initial
This commit is contained in:
parent
b2c903a5d7
commit
ee40573d9c
4 changed files with 104 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
prefetched
|
||||
__pycache__
|
||||
47
spotifyAuth.py
Normal file
47
spotifyAuth.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import requests
|
||||
import base64
|
||||
# Method 2: Using regular expressions
|
||||
|
||||
import re
|
||||
|
||||
|
||||
# Your Spotify app credentials
|
||||
client_id = '***REDACTED***'
|
||||
client_secret = '***REDACTED***'
|
||||
|
||||
# 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
49
spotifyFetch.py
Normal 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
6
stat.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import common as cm
|
||||
|
||||
playlists = cm.getData('playlists')
|
||||
|
||||
for pl in playlists:
|
||||
print(pl['name'])
|
||||
Loading…
Add table
Add a link
Reference in a new issue