forked from sultansq/kiu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spotify.py
100 lines (89 loc) · 3.21 KB
/
Spotify.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
import re
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from youtubesearchpython.__future__ import VideosSearch
import config
class SpotifyAPI:
def __init__(self):
self.regex = r"^(https:\/\/open.spotify.com\/)(.*)$"
self.client_id = config.SPOTIFY_CLIENT_ID
self.client_secret = config.SPOTIFY_CLIENT_SECRET
if config.SPOTIFY_CLIENT_ID and config.SPOTIFY_CLIENT_SECRET:
self.client_credentials_manager = (
SpotifyClientCredentials(
self.client_id, self.client_secret
)
)
self.spotify = spotipy.Spotify(
client_credentials_manager=self.client_credentials_manager
)
else:
self.spotify = None
async def valid(self, link: str):
if re.search(self.regex, link):
return True
else:
return False
async def track(self, link: str):
track = self.spotify.track(link)
info = track["name"]
for artist in track["artists"]:
fetched = f' {artist["name"]}'
if "Various Artists" not in fetched:
info += fetched
results = VideosSearch(info, limit=1)
for result in (await results.next())["result"]:
ytlink = result["link"]
title = result["title"]
vidid = result["id"]
duration_min = result["duration"]
thumbnail = result["thumbnails"][0]["url"].split("?")[0]
track_details = {
"title": title,
"link": ytlink,
"vidid": vidid,
"duration_min": duration_min,
"thumb": thumbnail,
}
return track_details, vidid
async def playlist(self, url):
playlist = self.spotify.playlist(url)
playlist_id = playlist["id"]
results = []
for item in playlist["tracks"]["items"]:
music_track = item["track"]
info = music_track["name"]
for artist in music_track["artists"]:
fetched = f' {artist["name"]}'
if "Various Artists" not in fetched:
info += fetched
results.append(info)
return results, playlist_id
async def album(self, url):
album = self.spotify.album(url)
album_id = album["id"]
results = []
for item in album["tracks"]["items"]:
info = item["name"]
for artist in item["artists"]:
fetched = f' {artist["name"]}'
if "Various Artists" not in fetched:
info += fetched
results.append(info)
return (
results,
album_id,
)
async def artist(self, url):
artistinfo = self.spotify.artist(url)
artist_id = artistinfo["id"]
results = []
artisttoptracks = self.spotify.artist_top_tracks(url)
for item in artisttoptracks["tracks"]:
info = item["name"]
for artist in item["artists"]:
fetched = f' {artist["name"]}'
if "Various Artists" not in fetched:
info += fetched
results.append(info)
return results, artist_id