Repository navigation
Expand file tree
/
Copy pathcalls.py
More file actions
68 lines (57 loc) · 2.63 KB
/
Copy pathcalls.py
File metadata and controls
68 lines (57 loc) · 2.63 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Nimbus ~ UserBot
# Copyright (C) 2023 NimbusTheCloud, ufoptg, @TrueSaiyan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# This file is a part of < https://github.com/ufoptg/Nimbus/ > and < https://github.com/ToxygenX/Megatron/ >
# This file is licensed explicitly for and to the use of Both @TrueSaiyan(636002102) and @Hackintush(1601105531)
# PLease read the GNU Affero General Public License in
# <https://www.github.com/ufoptg/Nimbus/blob/main/LICENSE/>.
# If not, see <https://www.gnu.org/licenses/>.
from requests import get, post
from . import urlbuilder
def call_acess_token(credentials):
"""
Calls Spotify API to obtain an access token using client credentials.
:param credentials: Encoded client credentials.
:return: Response object containing the access token.
"""
endpoint = "https://accounts.spotify.com/api/token"
data = {"grant_type": "client_credentials"}
headers = {"Authorization": f"Basic {credentials}"}
return post(url=endpoint, data=data, headers=headers)
def call_search(acess_token, args):
"""
Calls Spotify API to search for entities using the provided access token and arguments.
:param access_token: Access token for authentication.
:param args: Tuple of arguments for the search endpoint.
:return: Response object containing the search results.
"""
endpoint = urlbuilder.search_endpoint(*args)
headers = {"Authorization": f"Bearer {acess_token}"}
return get(url=endpoint, headers=headers)
def call_track_preview(track_id, access_token):
"""
Calls Spotify API to retrieve the preview URL for a given track.
:param track_id: Spotify track ID.
:param access_token: Access token for authentication.
:return: Preview URL if available, None otherwise.
"""
endpoint = f"https://api.spotify.com/v1/tracks/{track_id}"
headers = {"Authorization": f"Bearer {access_token}"}
response = get(url=endpoint, headers=headers)
if response.status_code == 200:
track_data = response.json()
if "preview_url" in track_data:
return track_data["preview_url"]
return None