48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
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."""
|
|
file_path = os.path.join(data_dir, f"{name}.json")
|
|
try:
|
|
with open(file_path, 'r') as file:
|
|
data = json.load(file)
|
|
return data
|
|
except FileNotFoundError:
|
|
return f"File {name}.json not found."
|
|
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."""
|
|
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()
|