def test_track(): """ Test if the [Track][deethon.types.Track] class raises a [DeezerApiError][deethon.errors.DeezerApiError] when an invalid track ID is passed. """ assert deethon.Track(95813354).id == 95813354 with pytest.raises(deethon.errors.DeezerApiError): deethon.Track(1234567890)
async def track_handler(event: events.NewMessage.Event): track_id = event.pattern_match.group(1) track = deethon.Track(track_id) # track.add_more_tags(deezer) await event.respond(translate.TRACK_MSG.format( track.title, track.artist, track.album.title, track.release_date, f"https://www.deezer.com/track/{event.pattern_match.group(1)}", f"@{event.chat.username}" if event.chat.username else event.chat.first_name), file=track.album.cover_xl, link_preview=False) await dl_and_upload_track(event, track_id) await event.respond("Download Complete !")
async def track_link(event: NewMessage.Event): try: track = deethon.Track(event.pattern_match.group(1)) except deethon.errors.DeezerApiError: await event.reply("Track not found.") raise StopPropagation await event.respond( translate.TRACK_MSG.format( track.title, track.artist, track.album.title, track.release_date ), file=track.album.cover_xl) quality = users[event.chat_id]["quality"] download_status = DownloadStatus(event) await download_status.start() file = await bot.loop.run_in_executor(None, deezer.download_track, track, quality, download_status.progress) download_status.finished() file_ext = ".mp3" if quality.startswith("MP3") else ".flac" file_name = track.artist + " - " + track.title + file_ext upload_status = UploadStatus(event) await upload_status.start() async with bot.action(event.input_chat, 'audio'): uploaded_file = await upload_file( file_name=file_name, client=bot, file=open(file, 'rb'), progress_callback=upload_status.progress, ) upload_status.finished() await bot.send_file( event.chat_id, uploaded_file, thumb=track.album.cover_medium, attributes=[ DocumentAttributeAudio( voice=False, title=track.title, duration=track.duration, performer=track.artist, ) ], ) raise StopPropagation
async def dl_and_upload_track(event, track_id): track = deethon.Track(track_id) track.add_more_tags(deezer) quality = "FLAC" quality_constant = deethon.utils.get_quality(quality) download_url = deethon.utils.get_stream_url(track, quality_constant) print("Download Url : ", download_url, end='\n') file_object = await bot.loop.run_in_executor(None, dl._download_track, download_url) file_size = file_object.tell() print(file_size) file_object.seek(0) _file_name = track.title + '.flac' if file_size < 52428800: await bot.loop.run_in_executor(None, BOT.send_audio, event.chat_id, file_object, track.duration, track.artist, track.title, translate.FILE_CAPTION, False, None, None, 20, None, track.album.cover_medium) # BOT.send_audio(chat_id=event.chat_id, audio=file_object, # filename=_file_name, duration=track.duration, performer=track.artist, # title=track.title, thumb=track.album.cover_medium, # caption=translate.FILE_CAPTION) else: uploaded_file = await bot.upload_file(file_object) await bot.send_file( event.chat_id, uploaded_file, caption=translate.FILE_CAPTION, thumb=track.album.cover_medium, attributes=[ DocumentAttributeAudio( voice=False, title=track.title, duration=track.duration, performer=track.artist, ) ], )
async def track_link(event: Union[NewMessage.Event, Message]): if users[event.chat_id]["downloading"]: await event.reply(translate.USER_IS_DOWNLOADING) raise events.StopPropagation try: track = deethon.Track(event.pattern_match.group(1)) except deethon.errors.DeezerApiError: await event.reply("Traccia non trovata.") raise events.StopPropagation await event.respond(translate.TRACK_MSG.format( track.title, track.artist, track.album.title, get_italian_date_format(track.release_date)), file=track.album.cover_xl) quality = users[event.chat_id]["quality"] users[event.chat_id]["downloading"] = True download_status = DownloadStatus(event) await download_status.start() try: file = await bot.loop.run_in_executor(None, deezer.download_track, track, quality, download_status.progress) except deethon.errors.DeezerLoginError: await event.reply(translate.LOGIN_ERROR) users[event.chat_id]["downloading"] = False raise events.StopPropagation except Exception as e: await event.reply(translate.GENERIC_ERROR) users[event.chat_id]["downloading"] = False raise events.StopPropagation finally: await download_status.finished() file_ext = ".mp3" if quality.startswith("MP3") else ".flac" file_name = track.artist + " - " + track.title + file_ext upload_status = UploadStatus(event) await upload_status.start() async with bot.action(event.chat_id, 'audio'): uploaded_file = await upload_file( file_name=file_name, client=bot, file=open(file, 'rb'), progress_callback=upload_status.progress, ) await upload_status.finished() await bot.send_file( event.chat_id, uploaded_file, thumb=track.album.cover_medium, attributes=[ DocumentAttributeAudio( voice=False, title=track.title, duration=track.duration, performer=track.artist, ) ], ) await event.reply(translate.END_MSG) users[event.chat_id]["downloading"] = False remove(file) raise events.StopPropagation