From c690162983c48554b3253fce98ebda65de77dfbb Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Sun, 7 Jul 2024 15:32:50 +0300 Subject: [PATCH] tm --- common.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ token | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 common.py diff --git a/common.py b/common.py new file mode 100644 index 0000000..36f0352 --- /dev/null +++ b/common.py @@ -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() diff --git a/token b/token index 355f6ff..1dea914 100644 --- a/token +++ b/token @@ -1 +1 @@ -BQCZr9A0eTYv5paloVLjSQGRQtkYatgH0tVWewIn-lMXh5dRUGK7xWqwHjgmLD9Kf3DDsAo2t5bmQ7g_W6EyFSMz2Tu4ZuMXPk3p0wrwvfnWyNi6W3uKELvDaMSS1hHDKe651RTUpApom6FUnoGnQ-SRYDZzoH9FwR0nOeMJgMqzpLtR0bXx3rkS3z_AeSmXIGjxBK7u7uWk3Iqabp2CGA \ No newline at end of file +BQA-OcaRz7YObEOVptzs_uE9hg8o3wbZozKn1JBgduwxIyq_WebpieN5b0bsea7ocHCE2-aTJ9bQxR4Wg9eq9FuiwTog25QIMR52yIzV2FZ66H_pjqoDRn3vtObnhTpkc4LWauwoWE-8WFZVVWbXwOhJs4TH9242cnZ-hJcS_v_LepDJHHZnKn84jb3inxRTFP0lKbYU98nTC4rCJvGaYQ \ No newline at end of file