예제 #1
0
async def download_track(track_id, quality='MP3_320'):
    track = await gettrack(track_id)
    album = await getalbum(track.album.id)
    private_info = await getprivateinfo(track.id)
    private_track = private_info['DATA']
    try:
        lyrics = private_info['LYRICS']['LYRICS_TEXT']
    except KeyError:
        lyrics = None
    if not private_track[f'FILESIZE_{quality}']:
        raise ValueError(
            f'quality {quality} is not availible for track {track_id}')
    quality_n = qualities[quality]
    if quality_n == '9':
        ext = 'flac'
    else:
        ext = 'mp3'

    print(
        f'[Deezer] Start downloading: {track.id} | {track.artist.name} - {track.title} '
    )
    track_url = decrypt.get_dl_url(private_track, quality_n)
    os.makedirs(f'downloads/{track.id}', exist_ok=True)
    filepath = f'downloads/{track.id}/{track.filename_safe}.{ext}'
    stream = await utils.get_file(track_url)
    await decrypt.decrypt_track(stream, private_track, filepath)
    cover = await track.get_max_size_cover(album)
    utils.add_tags(filepath, track, album, cover, lyrics)
    print(
        f'[Deezer] Finished downloading: {track.id} | {track.artist.name} - {track.title} '
    )
    file_download_logger.info(f'[downloaded track {track.id}] {track}')
    return filepath
예제 #2
0
async def finish_download(track, inline_message_id, user):
    file_id = await db_utils.get_track(track.id)
    if file_id:
        return await bot.edit_message_media(
            media=types.InputMediaAudio(media=file_id,
                                        title=track.title,
                                        performer=track.artist.name,
                                        duration=track.duration),
            inline_message_id=inline_message_id)
    try:
        track_file = await track.download()
        thumb = await track.get_thumb()
        if thumb:
            thumb = BytesIO(thumb)
        filename = f"{track.artist.name} - {track.title}.mp3".replace('/', '_')
    except ValueError:
        return await bot.edit_message_reply_markup(
            inline_message_id=inline_message_id,
            reply_markup=inline_keyboards.download_error_keyboard)
    msg = await bot.send_audio(chat_id=-1001246220493,
                               audio=InputFile(track_file, filename=filename),
                               thumb=InputFile(thumb, filename='thumb.jpg'),
                               performer=track.artist.name,
                               title=track.title,
                               duration=track.duration)
    file_id = msg.audio.file_id
    await db_utils.add_track(track.id, file_id)
    file_id = await db_utils.get_track(track.id)

    try:
        await bot.edit_message_media(media=types.InputMediaAudio(
            media=file_id,
            title=track.title,
            performer=track.artist.name,
            duration=track.duration),
                                     inline_message_id=inline_message_id)
    except exceptions.BadRequest:
        try:
            await bot.send_audio(user.id, file_id)
        except:
            pass

    file_download_logger.info(
        f'[downloaded track {track.id} (inline)] {track}')
    sent_message_logger.info(
        f'[send track {track.id} to {format_name(user)} (inline)] {track}')
예제 #3
0
async def finish_download(track, inline_message_id, user):
    file_id = await db_utils.get_track(track.id)
    if file_id:
        return await bot.edit_message_media(
            media=types.InputMediaAudio(media=file_id,
                                        title=track.title,
                                        performer=track.artist.name,
                                        duration=track.duration),
            inline_message_id=inline_message_id)
    path = await track.download()
    if (os.path.getsize(path) >> 20) > 50:
        await bot.edit_message_reply_markup(
            inline_message_id=inline_message_id,
            reply_markup=inline_keyboards.large_file_keyboard)
        await post_large_track(path, track)
        file_id = await db_utils.get_track(track.id)
    else:
        msg = await upload_track(track, path)
        await db_utils.add_track(track.id, msg.audio.file_id)
        file_id = msg.audio.file_id

    try:
        await bot.edit_message_media(media=types.InputMediaAudio(
            media=file_id,
            title=track.title,
            performer=track.artist.name,
            duration=track.duration),
                                     inline_message_id=inline_message_id)
        shutil.rmtree(path.rsplit('/', 1)[0])
    except exceptions.BadRequest:
        try:
            await bot.send_audio(user.id, file_id)
        except:
            pass

    file_download_logger.info(
        f'[downloaded track {track.id} (inline)] {track}')
    sent_message_logger.info(
        f'[send track {track.id} to {format_name(user)} (inline)] {track}')
예제 #4
0
async def download_track(track_id, quality='MP3_320'):
    track = await gettrack(track_id)
    album = await getalbum(track.album.id)
    private_info = await getprivateinfo(track.id)
    try:
        private_track = private_info['DATA']
    except KeyError:
        await getCSRFToken()
        try:
            private_track = private_info['DATA']
        except KeyError:
            raise ValueError('Cant download track')
    try:
        lyrics = private_info['LYRICS']['LYRICS_TEXT']
    except KeyError:
        lyrics = None
    if not private_track[f'FILESIZE_{quality}']:
        raise ValueError(
            f'quality {quality} is not availible for track {track_id}')
    quality_n = qualities[quality]
    if quality_n == '9':
        file_format = 'flac'
    else:
        file_format = 'mp3'

    print(f'[Deezer] Start downloading: {track.id} '
          f'| {track.artist.name} - {track.title} ')
    track_url = decrypt.get_dl_url(private_track, quality_n)
    stream = await utils.get_file(track_url)
    decrypted_file = decrypt.decrypt_track(stream, private_track)
    cover = await track.get_max_size_cover(album)
    lyrics = await getlyrics(track.id)
    add_tags(decrypted_file, file_format, track, album, cover, lyrics)
    print(f'[Deezer] Finished downloading: {track.id} '
          f'| {track.artist.name} - {track.title} ')
    file_download_logger.info(f'[downloaded track {track.id}] {track}')
    return decrypted_file