added type hints all over the project, updated mypy to ignore missing types from libraries
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# Handles translating urls and search terms
|
||||
"""
|
||||
URL and search query handling for Groovy-Zilean
|
||||
Translates YouTube, Spotify, SoundCloud URLs and search queries into playable audio
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
import yt_dlp as ytdlp
|
||||
import spotipy
|
||||
|
||||
@@ -22,7 +26,7 @@ ydl_opts = {
|
||||
},
|
||||
}
|
||||
|
||||
async def main(url, sp):
|
||||
async def main(url: str, sp: spotipy.Spotify) -> list[dict[str, Any] | str]:
|
||||
|
||||
#url = url.lower()
|
||||
|
||||
@@ -60,7 +64,7 @@ async def main(url, sp):
|
||||
return []
|
||||
|
||||
|
||||
async def search_song(search):
|
||||
async def search_song(search: str) -> list[dict[str, Any]]:
|
||||
with ytdlp.YoutubeDL(ydl_opts) as ydl:
|
||||
try:
|
||||
info = ydl.extract_info(f"ytsearch1:{search}", download=False)
|
||||
@@ -89,7 +93,7 @@ async def search_song(search):
|
||||
return [data]
|
||||
|
||||
|
||||
async def spotify_song(url, sp):
|
||||
async def spotify_song(url: str, sp: spotipy.Spotify) -> list[dict[str, Any]]:
|
||||
track = sp.track(url.split("/")[-1].split("?")[0])
|
||||
search = ""
|
||||
|
||||
@@ -106,7 +110,11 @@ async def spotify_song(url, sp):
|
||||
return await search_song(query)
|
||||
|
||||
|
||||
async def spotify_playlist(url, sp):
|
||||
async def spotify_playlist(url: str, sp: spotipy.Spotify) -> list[str | dict[str, Any]]:
|
||||
"""
|
||||
Get songs from a Spotify playlist
|
||||
Returns a mixed list where first item is dict, rest are search strings
|
||||
"""
|
||||
# Get the playlist uri code
|
||||
code = url.split("/")[-1].split("?")[0]
|
||||
|
||||
@@ -115,42 +123,36 @@ async def spotify_playlist(url, sp):
|
||||
results = sp.playlist_tracks(code)['items']
|
||||
except spotipy.exceptions.SpotifyException:
|
||||
return []
|
||||
|
||||
# Go through the tracks
|
||||
songs = []
|
||||
|
||||
# Go through the tracks and build search queries
|
||||
songs: list[str | dict[str, Any]] = [] # Explicit type for mypy
|
||||
for track in results:
|
||||
search = ""
|
||||
|
||||
# Fetch all artists
|
||||
for artist in track['track']['artists']:
|
||||
|
||||
# Add all artists to search
|
||||
search += f"{artist['name']}, "
|
||||
|
||||
# Remove last column
|
||||
# Remove last comma
|
||||
search = search[:-2]
|
||||
search += f" - {track['track']['name']}"
|
||||
songs.append(search)
|
||||
|
||||
#searched_result = search_song(search)
|
||||
#if searched_result == []:
|
||||
#continue
|
||||
|
||||
#songs.append(searched_result[0])
|
||||
|
||||
# Fetch first song's full data
|
||||
while True:
|
||||
search_result = await search_song(songs[0])
|
||||
search_result = await search_song(songs[0]) # type: ignore
|
||||
if search_result == []:
|
||||
songs.pop(0)
|
||||
continue
|
||||
else:
|
||||
songs[0] = search_result[0]
|
||||
songs[0] = search_result[0] # Replace string with dict
|
||||
break
|
||||
|
||||
|
||||
return songs
|
||||
|
||||
|
||||
async def song_download(url):
|
||||
async def song_download(url: str) -> list[dict[str, Any]]:
|
||||
with ytdlp.YoutubeDL(ydl_opts) as ydl:
|
||||
try:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
@@ -180,7 +182,7 @@ async def song_download(url):
|
||||
return [data]
|
||||
|
||||
|
||||
async def playlist_download(url):
|
||||
async def playlist_download(url: str) -> list[dict[str, Any]]:
|
||||
with ytdlp.YoutubeDL(ydl_opts) as ydl:
|
||||
try:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
|
||||
Reference in New Issue
Block a user