Exemple #1
0
async def download(gdrive, service, uri=None):
    global is_cancelled
    reply = ""
    """ - Download files to local then upload - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if uri:
        full_path = os.getcwd() + TEMP_DOWNLOAD_DIRECTORY.strip(".")
        if isfile(uri) and uri.endswith(".torrent"):
            downloads = aria2.add_torrent(
                uri, uris=None, options={"dir": full_path}, position=None
            )
        else:
            uri = [uri]
            downloads = aria2.add_uris(uri, options={"dir": full_path}, position=None)
        gid = downloads.gid
        await check_progress_for_dl(gdrive, gid, previous=None)
        file = aria2.get_download(gid)
        filename = file.name
        if file.followed_by_ids:
            new_gid = await check_metadata(gid)
            await check_progress_for_dl(gdrive, new_gid, previous=None)
        try:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filenames
        except Exception:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filename
    else:
        try:
            current_time = time.time()
            is_cancelled = False
            downloaded_file_name = await gdrive.client.download_media(
                await gdrive.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                    progress(
                        d,
                        t,
                        gdrive,
                        current_time,
                        "[ARQUIVO - DOWNLOAD]",
                        is_cancelled=is_cancelled,
                    )
                ),
            )
        except CancelProcess:
            reply += (
                "`[ARQUIVO - CANCELADO]`\n\n"
                "`Status` : **OK** - sinal recebido: cancelado."
            )
            return reply
        else:
            required_file_name = downloaded_file_name
    try:
        file_name = await get_raw_name(required_file_name)
    except AttributeError:
        reply += "`[ENTRY - ERRO]`\n\n" "`Status` : **FALHA**\n"
        return reply
    mimeType = await get_mimeType(required_file_name)
    try:
        status = "[ARQUIVO - UPLOAD]"
        if isfile(required_file_name):
            try:
                result = await upload(
                    gdrive, service, required_file_name, file_name, mimeType
                )
            except CancelProcess:
                reply += (
                    "`[ARQUIVO - CANCELADO]`\n\n"
                    "`Status` : **OK** - sinal recebido: cancelado."
                )
                return reply
            else:
                reply += (
                    f"`{status}`\n\n"
                    f"`Nome   :` `{file_name}`\n"
                    f"`Tamanho   :` `{humanbytes(result[0])}`\n"
                    f"`Link   :` [{file_name}]({result[1]})\n"
                    "`Status :` **OK** - Upload concluído.\n\n"
                )
                return reply
        else:
            status = status.replace("[ARQUIVO", "[PASTA")
            global parent_Id
            folder = await create_dir(service, file_name)
            parent_Id = folder.get("id")
            webViewURL = "https://drive.google.com/drive/folders/" + parent_Id
            try:
                await task_directory(gdrive, service, required_file_name)
            except CancelProcess:
                reply += (
                    "`[PASTA - CANCELADO]`\n\n"
                    "`Status` : **OK** - sinal recebido: cancelado."
                )
                await reset_parentId()
                return reply
            except Exception:
                await reset_parentId()
            else:
                reply += (
                    f"`{status}`\n\n"
                    f"[{file_name}]({webViewURL})\n"
                    "`Status` : **OK** - Upload concluído.\n\n"
                )
                await reset_parentId()
                return reply
    except Exception as e:
        status = status.replace("DOWNLOAD]", "ERRO]")
        reply += f"`{status}`\n\n" "`Status` : **FALHA**\n" f"`Motivo` : `{str(e)}`\n\n"
        return reply
    return
async def download(gdrive, service, uri=None):
    global is_cancelled
    reply = ''
    """ - Download files to local then upload - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if uri:
        full_path = os.getcwd() + TEMP_DOWNLOAD_DIRECTORY.strip('.')
        if isfile(uri) and uri.endswith(".torrent"):
            downloads = aria2.add_torrent(uri,
                                          uris=None,
                                          options={'dir': full_path},
                                          position=None)
        else:
            uri = [uri]
            downloads = aria2.add_uris(uri,
                                       options={'dir': full_path},
                                       position=None)
        gid = downloads.gid
        await check_progress_for_dl(gdrive, gid, previous=None)
        file = aria2.get_download(gid)
        filename = file.name
        if file.followed_by_ids:
            new_gid = await check_metadata(gid)
            await check_progress_for_dl(gdrive, new_gid, previous=None)
        try:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filenames
        except Exception:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filename
    else:
        try:
            current_time = time.time()
            is_cancelled = False
            downloaded_file_name = await gdrive.client.download_media(
                await gdrive.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d,
                             t,
                             gdrive,
                             current_time,
                             "[FILE - DOWNLOAD]",
                             is_cancelled=is_cancelled)))
        except CancelProcess:
            names = []
            for name in os.listdir(TEMP_DOWNLOAD_DIRECTORY):
                names.append(join(TEMP_DOWNLOAD_DIRECTORY, name))
            """ asumming newest files are the cancelled one """
            newest = max(names, key=getctime)
            os.remove(newest)
            reply += ("`[FILE - CANCELLED]`\n\n"
                      "`Status` : **OK** - received signal cancelled.")
            return reply
        else:
            required_file_name = downloaded_file_name
    try:
        file_name = await get_raw_name(required_file_name)
    except AttributeError:
        reply += ("`[ENTRY - ERROR]`\n\n" "`Status` : **BAD**\n")
        return reply
    mimeType = await get_mimeType(required_file_name)
    try:
        status = "[FILE - UPLOAD]"
        if isfile(required_file_name):
            try:
                result = await upload(gdrive, service, required_file_name,
                                      file_name, mimeType)
            except CancelProcess:
                reply += ("`[FILE - CANCELLED]`\n\n"
                          "`Status` : **OK** - received signal cancelled.")
                return reply
            else:
                reply += (f"`{status}`\n\n"
                          f"`Name   :` `{file_name}`\n"
                          f"`Size   :` `{humanbytes(result[0])}`\n"
                          f"`Link   :` [{file_name}]({result[1]})\n"
                          "`Status :` **OK** - Successfully uploaded.\n\n")
                return reply
        else:
            status = status.replace("[FILE", "[FOLDER")
            global parent_Id
            folder = await create_dir(service, file_name)
            parent_Id = folder.get('id')
            webViewURL = ("https://drive.google.com/drive/folders/" +
                          parent_Id)
            try:
                await task_directory(gdrive, service, required_file_name)
            except CancelProcess:
                reply += ("`[FOLDER - CANCELLED]`\n\n"
                          "`Status` : **OK** - received signal cancelled.")
                await reset_parentId()
                return reply
            except Exception:
                await reset_parentId()
            else:
                reply += (f"`{status}`\n\n"
                          f"[{file_name}]({webViewURL})\n"
                          "`Status` : **OK** - Successfully uploaded.\n\n")
                await reset_parentId()
                return reply
    except Exception as e:
        status = status.replace("DOWNLOAD]", "ERROR]")
        reply += (f"`{status}`\n\n"
                  "`Status` : **failed**\n"
                  f"`Reason` : `{str(e)}`\n\n")
        return reply
    return
Exemple #3
0
async def download(gdrive, service, uri=None):
    """ - Download files to local then upload - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if uri:
        try:
            if uri.endswith(".torrent"):
                pass
        except AttributeError:
            torrent = False
        else:
            torrent = True
        try:
            if torrent is True:
                downloads = aria2.add_torrent(uri,
                                              uris=None,
                                              options=None,
                                              position=None)
            else:
                downloads = aria2.add_uris(uri, options=None, position=None)
        except Exception as e:
            return await gdrive.edit(
                "`[FILE - ERROR]`\n\n"
                " • `Status :` **FAILED**\n"
                " • `Reason :` Download failed.\n"
                f"    `{str(e)}`"
            )
        gid = downloads.gid
        await check_progress_for_dl(gid=gid, event=gdrive, previous=None)
        File = aria2.get_download(gid)
        if File.followed_by_ids:
            new_gid = await check_metadata(gid)
            await check_progress_for_dl(gid=new_gid,
                                        event=gdrive, previous=None)
        for root, dirs, files in os.walk('.'):
            for entry in files:
                if File.name == entry:
                    required_file_name = join(root, entry)
    else:
        try:
            current_time = time.time()
            downloaded_file_name = await gdrive.client.download_media(
                await gdrive.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(progress(d, t, gdrive, current_time,
                                       "`[FILE - DOWNLOAD]`")))
        except Exception as e:
            await gdrive.edit(str(e))
        else:
            required_file_name = downloaded_file_name
    try:
        file_name = await get_raw_name(required_file_name)
    except AttributeError:
        return await gdrive.edit(
            "`[ENTRY - ERROR]`\n\n"
            " • `Status :` **BAD**\n"
            " • `Reason :` Replied entry is not media/file it's a messages."
        )
    mimeType = await get_mimeType(required_file_name)
    try:
        result = await upload(gdrive, service, required_file_name,
                              file_name, mimeType)
        return await gdrive.edit(
            "`[FILE - DOWNLOAD]`\n\n"
            f" • `Name     :`\n    `{file_name}`\n"
            " • `Status   :` **OK**\n"
            f" • `URL      :`\n    [{file_name}]({result[0]})\n"
            f" • `Download :`\n    [{file_name}]({result[1]})"
        )
    except Exception as e:
        return await gdrive.edit(
            "`[FILE - ERROR]`\n\n"
            f" • `Name   :`\n    `{file_name}`\n"
            " • `Status :` **FAILED**\n"
            " • `Reason :` failed to upload.\n"
            f"    `{str(e)}`"
        )
    return
Exemple #4
0
async def download(gdrive, service, uri=None):
    reply = ''
    """ - Download files to local then upload - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if uri:
        if isfile(uri) and uri.endswith(".torrent"):
            downloads = aria2.add_torrent(uri,
                                          uris=None,
                                          options=None,
                                          position=None)
        else:
            uri = [uri]
            downloads = aria2.add_uris(uri, options=None, position=None)
        gid = downloads.gid
        await check_progress_for_dl(gdrive, gid, previous=None)
        file = aria2.get_download(gid)
        filename = file.name
        if file.followed_by_ids:
            new_gid = await check_metadata(gid)
            await check_progress_for_dl(gdrive, new_gid, previous=None)
        try:
            required_file_name = filenames
        except Exception:
            required_file_name = filename
    else:
        try:
            current_time = time.time()
            downloaded_file_name = await gdrive.client.download_media(
                await gdrive.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(progress(d, t, gdrive, current_time,
                                       "`[FILE - DOWNLOAD]`")))
        except Exception as e:
            await gdrive.edit(str(e))
        else:
            required_file_name = downloaded_file_name
    try:
        file_name = await get_raw_name(required_file_name)
    except AttributeError:
        reply += (
            "`[ENTRY - ERROR]`\n\n"
            "`Status :` **BAD**\n"
            "`Reason :` Replied entry is not media/file it's a messages.\n\n"
        )
        return reply
    mimeType = await get_mimeType(required_file_name)
    try:
        status = "[FILE - UPLOAD]"
        if isfile(required_file_name):
            result = await upload(
                     gdrive, service, required_file_name, file_name, mimeType)
            reply += (
                f"`{status}`\n\n"
                f"`Name     :`\n`{file_name}`\n"
                f"`Size     :` `{humanbytes(result[0])}`\n"
                f"`Download :` [{file_name}]({result[1]})\n"
                "`Status   :` **OK**\n\n"
            )
            return reply
        else:
            status = status.replace("[FILE", "[FOLDER")
            global parent_Id
            folder = await create_dir(service, file_name)
            parent_Id = folder.get('id')
            await task_directory(gdrive, service, required_file_name)
            webViewURL = "https://drive.google.com/drive/folders/" + parent_Id
            await reset_parentId()
            reply += (
                f"`{status}`\n\n"
                f"`Name   :`\n`{file_name}`\n"
                "`Status :` **OK**\n"
                f"`URL    :` [{file_name}]({webViewURL})\n\n"
            )
            return reply
    except Exception as e:
        status = status.replace("DOWNLOAD]", "ERROR]")
        reply += (
            f"`{status}`\n\n"
            "`Status :` **FAILED**\n"
            "`Reason :` failed to upload.\n"
            f"`{str(e)}`\n\n"
        )
        return reply
    return
Exemple #5
0
async def download(gdrive, service, uri=None):
    global is_cancelled
    reply = ""
    """ - Download files to local then upload - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if uri:
        full_path = os.getcwd() + TEMP_DOWNLOAD_DIRECTORY.strip(".")
        if isfile(uri) and uri.endswith(".torrent"):
            downloads = aria2.add_torrent(uri,
                                          uris=None,
                                          options={"dir": full_path},
                                          position=None)
        else:
            uri = [uri]
            downloads = aria2.add_uris(uri,
                                       options={"dir": full_path},
                                       position=None)
        gid = downloads.gid
        await check_progress_for_dl(gdrive, gid, previous=None)
        file = aria2.get_download(gid)
        filename = file.name
        if file.followed_by_ids:
            new_gid = await check_metadata(gid)
            await check_progress_for_dl(gdrive, new_gid, previous=None)
        try:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filenames
        except Exception:
            required_file_name = TEMP_DOWNLOAD_DIRECTORY + filename
    else:
        try:
            current_time = time.time()
            is_cancelled = False
            downloaded_file_name = await gdrive.client.download_media(
                await gdrive.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop().
                create_task(
                    progress(
                        d,
                        t,
                        gdrive,
                        current_time,
                        "[FILE - DOWNLOAD]",
                        is_cancelled=is_cancelled,
                    )),
            )
        except CancelProcess:
            reply += ("`[FILE - DIBATALKAN]`\n\n"
                      "`Status` : **OK** - sinyal yang diterima dibatalkan.")
            return reply
        else:
            required_file_name = downloaded_file_name
    try:
        file_name = await get_raw_name(required_file_name)
    except AttributeError:
        reply += "`[MASUK- ERROR]`\n\n" "`Status` : **BAD**\n"
        return reply
    mimeType = await get_mimeType(required_file_name)
    try:
        status = "[FILE - UPLOAD]"
        if isfile(required_file_name):
            try:
                result = await upload(gdrive, service, required_file_name,
                                      file_name, mimeType)
            except CancelProcess:
                reply += (
                    "`[FILE - DIBATALKAN]`\n\n"
                    "`Status` : **OK** - sinyal yang diterima dibatalkan.")
                return reply
            else:
                reply += (f"`{status}`\n\n"
                          f"`Nama   :` `{file_name}`\n"
                          f"`Ukuran :` `{humanbytes(result[0])}`\n"
                          f"`Link   :` [{file_name}]({result[1]})\n"
                          "`Status :` **OK** - Berhasil diunggah.\n\n")
                return reply
        else:
            status = status.replace("[FILE", "[FOLDER")
            global parent_Id
            folder = await create_dir(service, file_name)
            parent_Id = folder.get("id")
            webViewURL = "https://drive.google.com/drive/folders/" + parent_Id
            try:
                await task_directory(gdrive, service, required_file_name)
            except CancelProcess:
                reply += (
                    "`[FOLDER - DIBATALKAN]`\n\n"
                    "`Status` : **OK** - sinyal yang diterima dibatalkan.")
                await reset_parentId()
                return reply
            except Exception:
                await reset_parentId()
            else:
                reply += (f"`{status}`\n\n"
                          f"[{nama-file}]({webViewURL})\n"
                          "`Status` : **OK** - Berhasil Diunggah.\n\n")
                await reset_parentId()
                return reply
    except Exception as e:
        status = status.replace("DOWNLOAD]", "ERROR]")
        reply += (f"`{status}`\n\n"
                  "`Status` : **GAGAL**\n"
                  f"`Alasan` : `{str(e)}`\n\n")
        return reply
    return