tmp
This commit is contained in:
parent
c690162983
commit
7df0c0a0ff
49 changed files with 1439861 additions and 169 deletions
35
common.py
35
common.py
|
|
@ -1,18 +1,10 @@
|
|||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
# Directory for storing JSON files
|
||||
data_dir = 'prefetched'
|
||||
token = ''
|
||||
|
||||
with open('token', 'r') as file:
|
||||
token = file.readline().strip()
|
||||
|
||||
print(f'Using auth token : {token}')
|
||||
|
||||
def getData(name):
|
||||
"""Opens a JSON file in the 'prefetched' folder and returns its content."""
|
||||
def get_data(name):
|
||||
file_path = os.path.join(data_dir, f"{name}.json")
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
|
|
@ -23,26 +15,11 @@ def getData(name):
|
|||
except json.JSONDecodeError:
|
||||
return f"Error decoding JSON from {name}.json."
|
||||
|
||||
def saveData(data, name):
|
||||
"""Saves JSON data in the 'prefetched' folder with the given name."""
|
||||
|
||||
def save_data(data, name):
|
||||
os.makedirs(data_dir, exist_ok=True)
|
||||
|
||||
file_path = os.path.join(data_dir, f"{name}.json")
|
||||
with open(file_path, 'w') as file:
|
||||
json.dump(data, file, indent=4)
|
||||
return f"Data saved to {name}.json."
|
||||
|
||||
def fetch(endpoint, method='GET', body=None):
|
||||
url = f'https://api.spotify.com/{endpoint}'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {token}',
|
||||
}
|
||||
response = requests.request(method, url, headers=headers, json=body)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def main():
|
||||
data = fetch('v1/artists/4Z8W4fKeB5YxbusRsdQVPb')
|
||||
data2 = fetch('v1/me/playlists?limit=1')['total']
|
||||
print(data2)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
return f"Data saved to {name}.json."
|
||||
Loading…
Add table
Add a link
Reference in a new issue