-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_requests.py
150 lines (132 loc) · 4.84 KB
/
api_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
from typing import List
import requests as req
import data.get_data as get_data
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
import os
from openai import OpenAI, types as ty
from dotenv import load_dotenv
load_dotenv()
chat_token = os.getenv('CHAT_KEY')
client = OpenAI(
api_key=chat_token,
)
# Define the retry strategy
retry_strategy = Retry(
total=4, # Maximum number of retries
# HTTP status codes to retry on
status_forcelist=[429, 500, 502, 503, 504],
)
# Create an HTTP adapter with the retry strategy and mount it to session
adapter = HTTPAdapter(max_retries=retry_strategy)
# Create a new session object
session = req.Session()
session.mount('http://', adapter)
session.mount('https://', adapter)
req_headers = {
'Authorization': ""
}
def set_token(token):
req_headers['Authorization'] = f'Bearer {token}'
def get_liked():
songs = {}
response = session.get(
'https://api.spotify.com/v1/me/tracks?limit=50', headers=req_headers)
response.raise_for_status()
if response.status_code == 200:
data = response.json()
[songs.update({song['track']['id']: song}) for song in data['items']]
i = 0
# while data['next']:
# print(f'getting liked songs {i * 50} - {(i + 1) * 50}')
# i += 1
# response = session.get(data['next'], headers=req_headers)
# if response.status_code == 200:
# data = response.json()
# [songs.update({song['track']['id']: song})
# for song in data['items']]
# else:
# print(response.content)
return songs
def get_songs_stats(ids: List[str]):
songs = {}
ids_len = len(ids)
max_id_per_request = 100
for i in range(0, ids_len, max_id_per_request):
end = i + \
max_id_per_request if (
i + max_id_per_request) < ids_len else ids_len - 1
print(f'getting songs stats {i} - {end}')
param = ",".join(ids[i:end])
response = session.get(
f'https://api.spotify.com/v1/audio-features?ids={param}', headers=req_headers)
if response.status_code == 200:
data = response.json()
for song in data['audio_features']:
songs[song['id']] = song
return songs
def get_artists(ids: set):
artists = {}
unique_ids = list(ids)
ids_len = len(unique_ids)
max_id_per_request = 50
for i in range(0, ids_len, max_id_per_request):
end = i + \
max_id_per_request if (
i + max_id_per_request) < ids_len else ids_len - 1
print(f'getting artists {i} - {end}')
param = ",".join(unique_ids[i:end])
response = session.get(
f'https://api.spotify.com/v1/artists?ids={param}', headers=req_headers)
if response.status_code == 200:
data = response.json()
for artist in data['artists']:
artists[artist['id']] = artist
return artists
def add_playlists_names(data):
playlist_quantity = len(data)
prompt = f"Create {playlist_quantity} unique playlist names based in the following spotify metadata, in JSON format, on the next lines, give only the playlists names separated by line breaks in your response, do not number the items\n"
for cluster in data:
prompt += json.dumps(cluster["center"])
messages = [{"content": prompt, "role": "user"}]
# get_data.get_fake_chat_completion(messages=messages, model="gpt-3.5-turbo-1106") #
response = client.chat.completions.create(
messages=messages, model="gpt-3.5-turbo-1106")
print(response.choices)
print(response.choices[0])
choices = response.choices[0].message.content.split("\n")
playlists = [
{
'name': choice,
'musics': data[index]['musics']
}
for index, choice in enumerate(choices)
]
print([name for name in playlists])
return playlists
def create_playlist(body):
song_ids = body.get('song_ids')
playlist_name = body.get('playlist_name')
user = session.get("https://api.spotify.com/v1/me", headers=req_headers)
user_id = user.json()['id']
response = session.post(
f'https://api.spotify.com/v1/users/{user_id}/playlists', headers=req_headers,
json={
"name": playlist_name,
"description": "My description",
"public": False
})
body = response.json()
print(body)
playlist_id = body['id']
# max 100
song_ids_to_uri = [f"spotify:track:{id}" for id in song_ids]
while len(song_ids_to_uri) > 0:
uris = song_ids_to_uri[:100]
test = session.post(
f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks',
json={"uris": uris},
headers=req_headers)
song_ids_to_uri = song_ids_to_uri[100:]
return playlist_id