updated music fetching

This commit is contained in:
Alexander Karpov 2024-04-06 23:34:05 +03:00
parent 2b2c16db2d
commit 00668b6f18
2 changed files with 34 additions and 9 deletions

View File

@ -75,13 +75,38 @@ def load_file_meta(track: int, user_id: int) -> str:
return str(song)
def load_playlist(link: str, user_id: int):
def load_url(link: str, user_id: int):
client = login()
if "/playlists/" in link:
author = link.split("/")[4]
playlist_id = link.split("/")[-1]
client = login()
playlist = client.users_playlists(int(playlist_id), author) # type: Playlist
for track in playlist.fetch_tracks():
tasks.load_ym_file_meta.apply_async(
kwargs={"track": track.track.id, "user_id": user_id}
)
elif "/album/" in link:
album_id = link.split("/")[-1]
album = client.albums_with_tracks(int(album_id))
tracks = []
for volume in album.volumes:
for track in volume:
tracks.append(track)
for track in tracks:
tasks.load_ym_file_meta.apply_async(
kwargs={"track": track.track.id, "user_id": user_id}
)
elif "/artist/" in link:
author = link.split("/")[-1]
artist = client.artists([int(author)])[0]
for track in artist.popular_tracks:
tasks.load_ym_file_meta.apply_async(
kwargs={"track": track.id, "user_id": user_id}
)
else:
track_id = link.split("/")[-1]
tasks.load_ym_file_meta.apply_async(
kwargs={"track": track_id, "user_id": user_id}
)

View File

@ -29,7 +29,7 @@
logger = structlog.get_logger(__name__)
@shared_task(soft_time_limit=60 * 20, time_limit=60 * 30)
@shared_task(soft_time_limit=60 * 60, time_limit=60 * 120)
def list_tracks(url, user_id):
if "music.youtube.com" in url or "youtu.be" in url:
url = url.replace("music.youtube.com", "youtube.com")
@ -37,7 +37,7 @@ def list_tracks(url, user_id):
if "spotify.com" in url:
spotify.download_url(url, user_id)
elif "music.yandex.ru" in url:
yandex.load_playlist(url, user_id)
yandex.load_url(url, user_id)
if "youtube.com" in url:
if "channel" in url or "/c/" in url:
ytmusic = ytmusicapi.YTMusic()