This commit is contained in:
IlyaShurupov 2024-07-07 15:32:50 +03:00
parent 28bf3a5187
commit 89c79a420c
2 changed files with 49 additions and 1 deletions

48
common.py Normal file
View file

@ -0,0 +1,48 @@
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()

2
token
View file

@ -1 +1 @@
BQCZr9A0eTYv5paloVLjSQGRQtkYatgH0tVWewIn-lMXh5dRUGK7xWqwHjgmLD9Kf3DDsAo2t5bmQ7g_W6EyFSMz2Tu4ZuMXPk3p0wrwvfnWyNi6W3uKELvDaMSS1hHDKe651RTUpApom6FUnoGnQ-SRYDZzoH9FwR0nOeMJgMqzpLtR0bXx3rkS3z_AeSmXIGjxBK7u7uWk3Iqabp2CGA
BQA-OcaRz7YObEOVptzs_uE9hg8o3wbZozKn1JBgduwxIyq_WebpieN5b0bsea7ocHCE2-aTJ9bQxR4Wg9eq9FuiwTog25QIMR52yIzV2FZ66H_pjqoDRn3vtObnhTpkc4LWauwoWE-8WFZVVWbXwOhJs4TH9242cnZ-hJcS_v_LepDJHHZnKn84jb3inxRTFP0lKbYU98nTC4rCJvGaYQ