Пример #1
0
async def download_api(dl):
    await dl.edit("`Collecting information...`")
    URL = dl.pattern_match.group(1)
    URL_MSG = await dl.get_reply_message()
    if URL:
        pass
    elif URL_MSG:
        URL = URL_MSG.text
    else:
        await dl.edit("`Empty information...`")
        return
    if not re.findall(r'\bhttps?://download.*pixelexperience.*\.org\S+', URL):
        await dl.edit("`Invalid information...`")
        return
    if URL.endswith('/'):
        file_name = URL.split("/")[-2]
    else:
        file_name = URL.split("/")[-1]
    build_date = datetime.strptime(file_name.split("-")[2],
                                   '%Y%m%d').strftime('%Y/%m/%d')  # Full ROM
    android_version = file_name.split("-")[1]
    if android_version == "9.0":
        await dl.edit("`Abort, only support android 10...`")
        return
    await dl.edit("`Sending information...`")
    driver = await chrome()
    await dl.edit("`Getting information...`")
    driver.get(URL)
    error = driver.find_elements_by_class_name("swal2-content")
    if len(error) > 0:
        if error[0].text == "File Not Found.":
            await dl.edit(f"`FileNotFoundError`: {URL} is not found.")
            return
    datas = driver.find_elements_by_class_name('download__meta')
    """ - enumerate data to make sure we download the matched version - """
    md5_origin = None
    i = None
    for index, value in enumerate(datas):
        for data in value.text.split("\n"):
            if data.startswith("MD5"):
                md5_origin = data.split(':')[1].strip()
                i = index
                break
        if md5_origin is not None and i is not None:
            break
    if md5_origin is None and i is None:
        await dl.edit("`There is no match version available...`")
    if URL.endswith('/'):
        file_name = URL.split("/")[-2]
    else:
        file_name = URL.split("/")[-1]
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    download = driver.find_elements_by_class_name("download__btn")[i]
    download.click()
    await dl.edit("`Starting download...`")
    file_size = human_to_bytes(download.text.split(None, 3)[-1].strip('()'))
    display_message = None
    complete = False
    start = time.time()
    while complete is False:
        if os.path.isfile(file_path + '.crdownload'):
            try:
                downloaded = os.stat(file_path + '.crdownload').st_size
                status = "Downloading"
            except OSError:  # Rare case
                await asyncio.sleep(1)
                continue
        elif os.path.isfile(file_path):
            downloaded = os.stat(file_path).st_size
            file_size = downloaded
            status = "Checking"
        else:
            await asyncio.sleep(0.3)
            continue
        diff = time.time() - start
        percentage = downloaded / file_size * 100
        speed = round(downloaded / diff, 2)
        eta = round((file_size - downloaded) / speed)
        prog_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status, "".join(["■" for i in range(math.floor(percentage / 10))]),
            "".join(["▨" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2))
        current_message = (
            "`[DOWNLOAD]`\n\n"
            f"`{file_name}`\n"
            f"`Status`\n{prog_str}\n"
            f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
            f" @ {humanbytes(speed)}`\n"
            f"`ETA` -> {time_formatter(eta)}")
        if round(diff % 15.00) == 0 and display_message != current_message or (
                downloaded == file_size):
            await dl.edit(current_message)
            display_message = current_message
        if downloaded == file_size:
            if not os.path.isfile(file_path):  # Rare case
                await asyncio.sleep(1)
                continue
            MD5 = await md5(file_path)
            if md5_origin == MD5:
                complete = True
            else:
                await dl.edit("`Download corrupt...`")
                os.remove(file_path)
                driver.quit()
                return
    await dl.respond(f"`{file_name}`\n\n"
                     f"Successfully downloaded to `{file_path}`.")
    await dl.delete()
    driver.quit()
    return
Пример #2
0
async def download_gdrive(gdrive, service, uri):
    reply = ''
    global is_cancelled
    """ - remove drivesdk and export=download from link - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.mkdir(TEMP_DOWNLOAD_DIRECTORY)
    if "&export=download" in uri:
        uri = uri.split("&export=download")[0]
    elif "file/d/" in uri and "/view" in uri:
        uri = uri.split("?usp=drivesdk")[0]
    try:
        file_Id = uri.split("uc?id=")[1]
    except IndexError:
        try:
            file_Id = uri.split("open?id=")[1]
        except IndexError:
            if "/view" in uri:
                file_Id = uri.split("/")[-2]
            else:
                try:
                    file_Id = uri.split(
                        "uc?export=download&confirm=")[1].split("id=")[1]
                except IndexError:
                    """ - if error parse in url, assume given value is Id - """
                    file_Id = uri
    try:
        file = await get_information(service, file_Id)
    except HttpError as e:
        if '404' in str(e):
            drive = 'https://drive.google.com'
            url = f'{drive}/uc?export=download&id={file_Id}'

            session = requests.session()
            download = session.get(url, stream=True)

            try:
                download.headers['Content-Disposition']
            except KeyError:
                page = BeautifulSoup(download.content, 'lxml')
                try:
                    export = drive + page.find('a', {
                        'id': 'uc-download-link'
                    }).get('href')
                except AttributeError:
                    try:
                        error = (page.find('p', {
                            'class': 'uc-error-caption'
                        }).text + '\n' +
                                 page.find('p', {
                                     'class': 'uc-error-subcaption'
                                 }).text)
                    except Exception:
                        reply += ("`[FILE - ERROR]`\n\n"
                                  "`Status` : **BAD** - failed to download.\n"
                                  "`Reason` : uncaught err.")
                    else:
                        reply += ("`[FILE - ERROR]`\n\n"
                                  "`Status` : **BAD** - failed to download.\n"
                                  f"`Reason` : {error}")
                    return reply
                download = session.get(export, stream=True)
                file_size = human_to_bytes(
                    page.find('span', {
                        'class': 'uc-name-size'
                    }).text.split()[-1].strip('()'))
            else:
                file_size = int(download.headers['Content-Length'])

            file_name = re.search(
                'filename="(.*)"',
                download.headers["Content-Disposition"]).group(1)
            file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
            with io.FileIO(file_path, 'wb') as files:
                CHUNK_SIZE = None
                current_time = time.time()
                display_message = None
                first = True
                is_cancelled = False
                for chunk in download.iter_content(CHUNK_SIZE):
                    if is_cancelled is True:
                        raise CancelProcess

                    if not chunk:
                        break

                    diff = time.time() - current_time
                    if first is True:
                        downloaded = len(chunk)
                        first = False
                    else:
                        downloaded += len(chunk)
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Downloading` | [{0}{1}] `{2}%`".format(
                        "".join(
                            ["▰" for i in range(math.floor(percentage / 10))]),
                        "".join([
                            "▱"
                            for i in range(10 - math.floor(percentage / 10))
                        ]), round(percentage, 2))
                    current_message = (
                        "`[FILE - DOWNLOAD]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`ETA` -> {time_formatter(eta)}")
                    if round(diff % 15.00) == 0 and (
                            display_message != current_message) or (
                                downloaded == file_size):
                        await gdrive.edit(current_message)
                        display_message = current_message
                    files.write(chunk)
    else:
        file_name = file.get('name')
        mimeType = file.get('mimeType')
        if mimeType == 'application/vnd.google-apps.folder':
            return await gdrive.edit("`Aborting, folder download not support`")
        file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
        request = service.files().get_media(fileId=file_Id)
        with io.FileIO(file_path, 'wb') as df:
            downloader = MediaIoBaseDownload(df, request)
            complete = False
            is_cancelled = False
            current_time = time.time()
            display_message = None
            while complete is False:
                if is_cancelled is True:
                    raise CancelProcess

                status, complete = downloader.next_chunk()
                if status:
                    file_size = status.total_size
                    diff = time.time() - current_time
                    downloaded = status.resumable_progress
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Downloading` | [{0}{1}] `{2}%`".format(
                        "".join(
                            ["▰" for i in range(math.floor(percentage / 10))]),
                        "".join([
                            "▱"
                            for i in range(10 - math.floor(percentage / 10))
                        ]), round(percentage, 2))
                    current_message = (
                        "`[FILE - DOWNLOAD]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`ETA` -> {time_formatter(eta)}")
                    if round(diff % 15.00) == 0 and (
                            display_message != current_message) or (
                                downloaded == file_size):
                        await gdrive.edit(current_message)
                        display_message = current_message
    await gdrive.edit("`[FILE - DOWNLOAD]`\n\n"
                      f"`Name   :` `{file_name}`\n"
                      f"`Size   :` `{humanbytes(file_size)}`\n"
                      f"`Path   :` `{file_path}`\n"
                      "`Status :` **OK** - Successfully downloaded.")
    msg = await gdrive.respond("`Answer the question in your BOTLOG group`")
    async with gdrive.client.conversation(BOTLOG_CHATID) as conv:
        ask = await conv.send_message("`Proceed with mirroring? [y/N]`")
        try:
            r = conv.wait_event(
                events.NewMessage(outgoing=True, chats=BOTLOG_CHATID))
            r = await r
        except Exception:
            ans = 'N'
        else:
            ans = r.message.message.strip()
            await gdrive.client.delete_messages(BOTLOG_CHATID, r.id)
        await gdrive.client.delete_messages(gdrive.chat_id, msg.id)
        await gdrive.client.delete_messages(BOTLOG_CHATID, ask.id)
    if ans.capitalize() == 'N':
        return reply
    elif ans.capitalize() == "Y":
        try:
            result = await upload(gdrive, service, file_path, file_name,
                                  mimeType)
        except CancelProcess:
            reply += ("`[FILE - CANCELLED]`\n\n"
                      "`Status` : **OK** - received signal cancelled.")
        else:
            reply += ("`[FILE - UPLOAD]`\n\n"
                      f"`Name   :` `{file_name}`\n"
                      f"`Size   :` `{humanbytes(result[0])}`\n"
                      f"`Link   :` [{file_name}]({result[1]})\n"
                      "`Status :` **OK**\n\n")
        return reply
    else:
        await gdrive.client.send_message(
            BOTLOG_CHATID, "`Invalid answer type [Y/N] only...`")
        return reply
Пример #3
0
async def download_gdrive(gdrive, service, uri):
    reply = ""
    global is_cancelled
    """ - remove drivesdk and export=download from link - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.mkdir(TEMP_DOWNLOAD_DIRECTORY)
    if "&export=download" in uri:
        uri = uri.split("&export=download")[0]
    elif "file/d/" in uri and "/view" in uri:
        uri = uri.split("?usp=drivesdk")[0]
    try:
        file_Id = uri.split("uc?id=")[1]
    except IndexError:
        try:
            file_Id = uri.split("open?id=")[1]
        except IndexError:
            if "/view" in uri:
                file_Id = uri.split("/")[-2]
            else:
                try:
                    file_Id = uri.split("uc?export=download&confirm=")[1].split("id=")[
                        1
                    ]
                except IndexError:
                    """ - if error parse in url, assume given value is Id - """
                    file_Id = uri
    try:
        file = await get_information(service, file_Id)
    except HttpError as e:
        if "404" in str(e):
            drive = "https://drive.google.com"
            url = f"{drive}/uc?export=download&id={file_Id}"

            session = requests.session()
            download = session.get(url, stream=True)

            try:
                download.headers["Content-Disposition"]
            except KeyError:
                page = BeautifulSoup(download.content, "lxml")
                try:
                    export = drive + page.find("a", {"id": "uc-download-link"}).get(
                        "href"
                    )
                except AttributeError:
                    try:
                        error = (
                            page.find("p", {"class": "uc-error-caption"}).text
                            + "\n"
                            + page.find("p", {"class": "uc-error-subcaption"}).text
                        )
                    except Exception:
                        reply += (
                            "`[ARQUIVO - ERRO]`\n\n"
                            "`Status` : **FALHA** - falha no download.\n"
                            "`Motivo` : uncaught err."
                        )
                    else:
                        reply += (
                            "`[ARQUIVO - ERRO]`\n\n"
                            "`Status` : **FALHA** - falha no download.\n"
                            f"`Motivo` : {error}"
                        )
                    return reply
                download = session.get(export, stream=True)
                file_size = human_to_bytes(
                    page.find("span", {"class": "uc-name-size"})
                    .text.split()[-1]
                    .strip("()")
                )
            else:
                file_size = int(download.headers["Content-Length"])

            file_name = re.search(
                'filename="(.*)"', download.headers["Content-Disposition"]
            ).group(1)
            file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
            with io.FileIO(file_path, "wb") as files:
                CHUNK_SIZE = None
                current_time = time.time()
                display_message = None
                first = True
                is_cancelled = False
                for chunk in download.iter_content(CHUNK_SIZE):
                    if is_cancelled is True:
                        raise CancelProcess

                    if not chunk:
                        break

                    diff = time.time() - current_time
                    if first is True:
                        downloaded = len(chunk)
                        first = False
                    else:
                        downloaded += len(chunk)
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Baixando` | [{0}{1}] `{2}%`".format(
                        "".join(["■" for i in range(math.floor(percentage / 10))]),
                        "".join(["▨" for i in range(10 - math.floor(percentage / 10))]),
                        round(percentage, 2),
                    )
                    current_message = (
                        "`[ARQUIVO - DOWNLOAD]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`Tempo Estimado` -> {time_formatter(eta)}"
                    )
                    if (
                        round(diff % 15.00) == 0
                        and (display_message != current_message)
                        or (downloaded == file_size)
                    ):
                        await gdrive.edit(current_message)
                        display_message = current_message
                    files.write(chunk)
    else:
        file_name = file.get("name")
        mimeType = file.get("mimeType")
        if mimeType == "application/vnd.google-apps.folder":
            await gdrive.edit("`Abortando, o download da pasta não é suportado...`")
            return False
        file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
        request = service.files().get_media(fileId=file_Id, supportsAllDrives=True)
        with io.FileIO(file_path, "wb") as df:
            downloader = MediaIoBaseDownload(df, request)
            complete = False
            is_cancelled = False
            current_time = time.time()
            display_message = None
            while complete is False:
                if is_cancelled is True:
                    raise CancelProcess

                status, complete = downloader.next_chunk()
                if status:
                    file_size = status.total_size
                    diff = time.time() - current_time
                    downloaded = status.resumable_progress
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Baixando` | [{0}{1}] `{2}%`".format(
                        "".join(["■" for i in range(math.floor(percentage / 10))]),
                        "".join(["▨" for i in range(10 - math.floor(percentage / 10))]),
                        round(percentage, 2),
                    )
                    current_message = (
                        "`[ARQUIVO - DOWNLOAD]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`Tempo Estimado` -> {time_formatter(eta)}"
                    )
                    if (
                        round(diff % 15.00) == 0
                        and (display_message != current_message)
                        or (downloaded == file_size)
                    ):
                        await gdrive.edit(current_message)
                        display_message = current_message
    await gdrive.edit(
        "`[ARQUIVO - DOWNLOAD]`\n\n"
        f"`Nome   :` `{file_name}`\n"
        f"`Tamanho   :` `{humanbytes(file_size)}`\n"
        f"`Caminho   :` `{file_path}`\n"
        "`Status :` **OK** - Download concluído."
    )
    msg = await gdrive.respond("`Responda à pergunta em seu grupo do BOTLOG`")
    async with gdrive.client.conversation(BOTLOG_CHATID) as conv:
        ask = await conv.send_message("`Prosseguir com o mirror? [y/N]`")
        try:
            r = conv.wait_event(events.NewMessage(outgoing=True, chats=BOTLOG_CHATID))
            r = await r
        except Exception:
            ans = "N"
        else:
            ans = r.message.message.strip()
            await gdrive.client.delete_messages(BOTLOG_CHATID, r.id)
        await gdrive.client.delete_messages(gdrive.chat_id, msg.id)
        await gdrive.client.delete_messages(BOTLOG_CHATID, ask.id)
    if ans.capitalize() == "N":
        return reply
    elif ans.capitalize() == "Y":
        try:
            result = await upload(gdrive, service, file_path, file_name, mimeType)
        except CancelProcess:
            reply += (
                "`[ARQUIVO - CANCELADO]`\n\n"
                "`Status` : **OK** - sinal recebido: cancelado."
            )
        else:
            reply += (
                "`[ARQUIVO - UPLOAD]`\n\n"
                f"`Nome   :` `{file_name}`\n"
                f"`Tamanho   :` `{humanbytes(result[0])}`\n"
                f"`Link   :` [{file_name}]({result[1]})\n"
                "`Status :` **OK**\n\n"
            )
        return reply
    else:
        await gdrive.client.send_message(
            BOTLOG_CHATID, "`Tipo de resposta inválido [Y/N] apenas...`"
        )
        return reply
Пример #4
0
async def download_api(dl):
    await dl.edit("`Mengumpulkan informasi...`")
    URL = dl.pattern_match.group(1)
    URL_MSG = await dl.get_reply_message()
    if URL:
        pass
    elif URL_MSG:
        URL = URL_MSG.text
    else:
        await dl.edit("`Informasi kosong...`")
        return
    if not re.findall(r"\bhttps?://download.*pixelexperience.*\.org\S+", URL):
        await dl.edit("`Informasi tidak valid...`")
        return
    driver = await chrome()
    await dl.edit("`Mendapatkan informasi...`")
    driver.get(URL)
    error = driver.find_elements_by_class_name("swal2-content")
    if len(error) > 0:
        if error[0].text == "File Not Found.":
            await dl.edit(f"**Kesalahan** : {URL} tidak ditemukan.")
            return
    datas = driver.find_elements_by_class_name("download__meta")
    """ - enumerate data to make sure we download the matched version - """
    md5_origin = None
    i = None
    for index, value in enumerate(datas):
        for data in value.text.split("\n"):
            if data.startswith("MD5"):
                md5_origin = data.split(":")[1].strip()
                i = index
                break
        if md5_origin is not None and i is not None:
            break
    if md5_origin is None and i is None:
        await dl.edit("`Tidak ada versi cocok yang tersedia...`")
    if URL.endswith("/"):
        file_name = URL.split("/")[-2]
    else:
        file_name = URL.split("/")[-1]
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    download = driver.find_elements_by_class_name("download__btn")[i]
    download.click()
    await dl.edit("`Mulai mengunduh...`")
    file_size = human_to_bytes(download.text.split(None, 2)[-1].strip("()"))
    display_message = None
    complete = False
    start = time.time()
    while complete is False:
        if os.path.isfile(file_path + ".crdownload"):
            try:
                downloaded = os.stat(file_path + ".crdownload").st_size
                status = "Downloading"
            except OSError:  # Rare case
                await asyncio.sleep(1)
                continue
        elif os.path.isfile(file_path):
            downloaded = os.stat(file_path).st_size
            file_size = downloaded
            status = "Checking"
        else:
            await asyncio.sleep(0.3)
            continue
        diff = time.time() - start
        percentage = downloaded / file_size * 100
        speed = round(downloaded / diff, 2)
        eta = round((file_size - downloaded) / speed)
        prog_str = "**{0}** | [{1}{2}] `{3}%`".format(
            status,
            "".join(["■" for i in range(math.floor(percentage / 10))]),
            "".join(["▨" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2),
        )
        current_message = (
            "`[UNDUH]`\n\n"
            f"`{file_name}`\n"
            f"`Status`\n{prog_str}\n"
            f"`{humanbytes(downloaded)} dari {humanbytes(file_size)}"
            f" @ {humanbytes(speed)}`\n"
            f"`Perkiraan` -> {time_formatter(eta)}")
        if (round(diff % 15.00) == 0 and display_message != current_message
                or (downloaded == file_size)):
            await dl.edit(current_message)
            display_message = current_message
        if downloaded == file_size:
            if not os.path.isfile(file_path):  # Rare case
                await asyncio.sleep(1)
                continue
            MD5 = await md5(file_path)
            if md5_origin == MD5:
                complete = True
            else:
                await dl.edit("`Unduhan rusak...`")
                os.remove(file_path)
                driver.quit()
                return
    await dl.respond(f"`{file_name}`\n\n"
                     f"Berhasil diunduh ke `{file_path}`.")
    await dl.delete()
    driver.quit()
    return
Пример #5
0
async def download_api(dl):
    await dl.edit("`Collecting information...`")
    URL = dl.pattern_match.group(1)
    URL_MSG = await dl.get_reply_message()
    if URL:
        pass
    elif URL_MSG:
        URL = URL_MSG.text
    else:
        await dl.edit("`Empty information...`")
        return
    if not re.findall(r"\bhttps?://download.*pixelexperience.*\.org\S+", URL):
        await dl.edit("`Invalid information...`")
        return
    driver = await chrome()
    await dl.edit("`Getting information...`")
    driver.get(URL)
    error = driver.find_elements_by_class_name("swal2-content")
    if len(error) > 0 and error[0].text == "File Not Found.":
        await dl.edit(f"`FileNotFoundError`: {URL} is not found.")
        return
    datas = driver.find_elements_by_class_name("download__meta")
    md5_origin = None
    i = None
    for index, value in enumerate(datas):
        for data in value.text.split("\n"):
            if data.startswith("MD5"):
                md5_origin = data.split(":")[1].strip()
                i = index
                break
        if md5_origin is not None and i is not None:
            break
    if md5_origin is None and i is None:
        await dl.edit("`There is no match version available...`")
    file_name = URL.split("/")[-2] if URL.endswith("/") else URL.split("/")[-1]
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    download = driver.find_elements_by_class_name("download__btn")[i]
    download.click()
    await dl.edit("`Starting download...`")
    file_size = human_to_bytes(download.text.split(None, 3)[-1].strip("()"))
    display_message = None
    complete = False
    start = time.time()
    while not complete:
        if os.path.isfile(file_path + ".crdownload"):
            try:
                downloaded = os.stat(file_path + ".crdownload").st_size
                status = "Downloading"
            except OSError:  # Rare case
                await asyncio.sleep(1)
                continue
        elif os.path.isfile(file_path):
            downloaded = os.stat(file_path).st_size
            file_size = downloaded
            status = "Checking"
        else:
            await asyncio.sleep(0.3)
            continue
        diff = time.time() - start
        percentage = downloaded / file_size * 100
        speed = round(downloaded / diff, 2)
        eta = round((file_size - downloaded) / speed)
        prog_str = "[{0}{1}] `{2}%`".format(
            "".join("█" for i in range(math.floor(percentage / 10))),
            "".join("░" for i in range(10 - math.floor(percentage / 10))),
            round(percentage, 2),
        )
        current_message = (
            f"{file_name} - {status}\n"
            f"{prog_str}\n"
            f"`Size:` {humanbytes(downloaded)} of {humanbytes(file_size)}\n"
            f"`Speed:` {humanbytes(speed)}/s\n"
            f"`ETA:` {time_formatter(eta)}\n")
        if (round(diff % 15.00) == 0 and display_message != current_message
                or (downloaded == file_size)):
            await dl.edit(current_message)
            display_message = current_message
        if downloaded == file_size:
            if not os.path.isfile(file_path):  # Rare case
                await asyncio.sleep(1)
                continue
            MD5 = await md5(file_path)
            if md5_origin == MD5:
                complete = True
            else:
                await dl.edit("`Download corrupt...`")
                os.remove(file_path)
                driver.quit()
                return
    await dl.respond(f"`{file_name}`\n\n"
                     f"Successfully downloaded to `{file_path}`.")
    await dl.delete()
    driver.quit()
    return
Пример #6
0
async def download_api(dl):
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.mkdir(TEMP_DOWNLOAD_DIRECTORY)
    await dl.edit("`Collecting information...`")
    URL = dl.pattern_match.group(1)
    URL_MSG = await dl.get_reply_message()
    if URL:
        pass
    elif URL_MSG:
        URL = URL_MSG.text
    else:
        await dl.edit("`Empty information...`")
        return
    if not re.findall(r'\bhttps?://download.*pixelexperience.*\.org\S+', URL):
        await dl.edit("`Invalid information...`")
        return
    await dl.edit("`Sending information...`")
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.binary_location = GOOGLE_CHROME_BIN
    chrome_options.add_argument("--window-size=1920x1080")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")
    prefs = {'download.default_directory': './'}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(executable_path=CHROME_DRIVER,
                              options=chrome_options)
    await dl.edit("`Getting information...`")
    driver.get(URL)
    driver.command_executor._commands["send_command"] = (
        "POST", '/session/$sessionId/chromium/send_command')
    params = {
        'cmd': 'Page.setDownloadBehavior',
        'params': {
            'behavior': 'allow',
            'downloadPath': TEMP_DOWNLOAD_DIRECTORY
        }
    }
    driver.execute("send_command", params)
    md5_origin = driver.find_elements_by_class_name(
        'download__meta')[0].text.split('\n')[2].split(':')[1].strip()
    file_name = driver.find_elements_by_class_name(
        'download__meta')[0].text.split('\n')[1].split(':')[1].strip()
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    download = driver.find_elements_by_class_name("download__btn")[0]
    download.click()
    x = download.get_attribute('text').split()[-2:]
    file_size = human_to_bytes((x[0] + x[1]).strip('()'))
    await asyncio.sleep(5)
    start = time.time()
    display_message = None
    complete = False
    while complete is False:
        try:
            downloaded = os.stat(file_path + '.crdownload').st_size
            status = "Downloading"
        except Exception:
            downloaded = os.stat(file_path).st_size
            file_size = downloaded
            status = "Checking"
        diff = time.time() - start
        percentage = downloaded / file_size * 100
        speed = round(downloaded / diff, 2)
        eta = round((file_size - downloaded) / speed)
        prog_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status, "".join(["●" for i in range(math.floor(percentage / 10))]),
            "".join(["○" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2))
        current_message = (
            "`[DOWNLOAD]`\n\n"
            f"`{file_name}`\n"
            f"`Status`\n{prog_str}\n"
            f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
            f" @ {humanbytes(speed)}`\n"
            f"`ETA` -> {time_formatter(eta)}")
        if round(diff % 10.00) == 0 and display_message != current_message or (
                downloaded == file_size):
            await dl.edit(current_message)
            display_message = current_message
        if downloaded == file_size:
            MD5 = await md5(file_path)
            if md5_origin == MD5:
                complete = True
            else:
                await dl.edit("`Download corrupt...`")
                os.remove(file_path)
                return
    await dl.respond(f"`{file_name}`\n\n"
                     f"Successfully downloaded to `{file_path}`.")
    await dl.delete()
    return
Пример #7
0
async def download_gdrive(gdrive, service, uri):
    reply = ""
    global is_cancelled
    """ - remove drivesdk and export=download from link - """
    if not isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.mkdir(TEMP_DOWNLOAD_DIRECTORY)
    if "&export=download" in uri:
        uri = uri.split("&export=download")[0]
    elif "file/d/" in uri and "/view" in uri:
        uri = uri.split("?usp=drivesdk")[0]
    try:
        file_Id = uri.split("uc?id=")[1]
    except IndexError:
        try:
            file_Id = uri.split("open?id=")[1]
        except IndexError:
            if "/view" in uri:
                file_Id = uri.split("/")[-2]
            else:
                try:
                    file_Id = uri.split(
                        "uc?export=download&confirm=")[1].split("id=")[1]
                except IndexError:
                    """- if error parse in url, assume given value is Id -"""
                    file_Id = uri
    try:
        file = await get_information(service, file_Id)
    except HttpError as e:
        if "404" in str(e):
            drive = "https://drive.google.com"
            url = f"{drive}/uc?export=download&id={file_Id}"

            session = requests.session()
            download = session.get(url, stream=True)

            try:
                download.headers["Content-Disposition"]
            except KeyError:
                page = BeautifulSoup(download.content, "lxml")
                try:
                    export = drive + \
                        page.find("a", {"id": "uc-download-link"}).get("href")
                except AttributeError:
                    try:
                        error = (page.find("p", {
                            "class": "uc-error-caption"
                        }).text + "\n" +
                                 page.find("p", {
                                     "class": "uc-error-subcaption"
                                 }).text)
                    except Exception:
                        reply += ("`[FILE - ERROR]`\n\n"
                                  "`Status` : **BAD** - gagal mengunduh.\n"
                                  "`Alasan` : Kesalahan tidak diketahui.")
                    else:
                        reply += ("`[FILE - ERROR]`\n\n"
                                  "`Status` : **BAD** - gagal mengunduh.\n"
                                  f"`Alasan` : {error}")
                    return reply
                download = session.get(export, stream=True)
                file_size = human_to_bytes(
                    page.find("span", {
                        "class": "uc-name-size"
                    }).text.split()[-1].strip("()"))
            else:
                file_size = int(download.headers["Content-Length"])

            file_name = re.search(
                'filename="(.*)"',
                download.headers["Content-Disposition"]).group(1)
            file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
            with io.FileIO(file_path, "wb") as files:
                CHUNK_SIZE = None
                current_time = time.time()
                display_message = None
                first = True
                is_cancelled = False
                for chunk in download.iter_content(CHUNK_SIZE):
                    if is_cancelled is True:
                        raise CancelProcess

                    if not chunk:
                        break

                    diff = time.time() - current_time
                    if first is True:
                        downloaded = len(chunk)
                        first = False
                    else:
                        downloaded += len(chunk)
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Downloading` | [{0}{1}] `{2}%`".format(
                        "".join(
                            ["■" for i in range(math.floor(percentage / 10))]),
                        "".join([
                            "▨"
                            for i in range(10 - math.floor(percentage / 10))
                        ]),
                        round(percentage, 2),
                    )
                    current_message = (
                        "`[FILE - UNDUHAN]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`ETA` -> {time_formatter(eta)}")
                    if (round(diff % 15.00) == 0 and
                        (display_message != current_message)
                            or (downloaded == file_size)):
                        await gdrive.edit(current_message)
                        display_message = current_message
                    files.write(chunk)
    else:
        file_name = file.get("name")
        mimeType = file.get("mimeType")
        if mimeType == "application/vnd.google-apps.folder":
            await gdrive.edit(
                "`Membatalkan, unduhan folder tidak mendukung...`")
            return False
        file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
        request = service.files().get_media(fileId=file_Id,
                                            supportsAllDrives=True)
        with io.FileIO(file_path, "wb") as df:
            downloader = MediaIoBaseDownload(df, request)
            complete = False
            is_cancelled = False
            current_time = time.time()
            display_message = None
            while complete is False:
                if is_cancelled is True:
                    raise CancelProcess

                status, complete = downloader.next_chunk()
                if status:
                    file_size = status.total_size
                    diff = time.time() - current_time
                    downloaded = status.resumable_progress
                    percentage = downloaded / file_size * 100
                    speed = round(downloaded / diff, 2)
                    eta = round((file_size - downloaded) / speed)
                    prog_str = "`Downloading` | [{0}{1}] `{2}%`".format(
                        "".join(
                            ["■" for i in range(math.floor(percentage / 10))]),
                        "".join([
                            "▨"
                            for i in range(10 - math.floor(percentage / 10))
                        ]),
                        round(percentage, 2),
                    )
                    current_message = (
                        "`[FILE - DOWNLOAD]`\n\n"
                        f"`{file_name}`\n"
                        f"`Status`\n{prog_str}\n"
                        f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
                        f" @ {humanbytes(speed)}`\n"
                        f"`ETA` -> {time_formatter(eta)}")
                    if (round(diff % 15.00) == 0 and
                        (display_message != current_message)
                            or (downloaded == file_size)):
                        await gdrive.edit(current_message)
                        display_message = current_message
    await gdrive.edit("`[FILE - DOWNLOAD]`\n\n"
                      f"`Nama    :` `{file_name}`\n"
                      f"`Ukuran  :` `{humanbytes(file_size)}`\n"
                      f"`Path    :` `{file_path}`\n"
                      "`Status    :` **OK** - Berhasil Diunduh.")
    msg = await gdrive.respond("`Jawab pertanyaan di grup BOTLOG Anda`")
    async with gdrive.client.conversation(BOTLOG_CHATID) as conv:
        ask = await conv.send_message("`Lanjutkan dengan mirroring? [y/N]`")
        try:
            r = conv.wait_event(
                events.NewMessage(outgoing=True, chats=BOTLOG_CHATID))
            r = await r
        except Exception:
            ans = "N"
        else:
            ans = r.message.message.strip()
            await gdrive.client.delete_messages(BOTLOG_CHATID, r.id)
        await gdrive.client.delete_messages(gdrive.chat_id, msg.id)
        await gdrive.client.delete_messages(BOTLOG_CHATID, ask.id)
    if ans.capitalize() == "N":
        return reply
    elif ans.capitalize() == "Y":
        try:
            result = await upload(gdrive, service, file_path, file_name,
                                  mimeType)
        except CancelProcess:
            reply += ("`[FILE - DIBATALKAN]`\n\n"
                      "`Status` : **OK** - sinyal yang diterima dibatalkan.")
        else:
            reply += ("`[FILE - UNGGAH]`\n\n"
                      f"`Nama   :` `{file_name}`\n"
                      f"`Ukuran :` `{humanbytes(result[0])}`\n"
                      f"`Link   :` [{file_name}]({result[1]})\n"
                      "`Status :` **OK**\n\n")
        return reply
    else:
        await gdrive.client.send_message(
            BOTLOG_CHATID, "`Jenis jawaban tidak valid [Y/N] saja...`")
        return reply
Пример #8
0
async def download_api(dl):
    await dl.edit("`Coletando informações...`")
    URL = dl.pattern_match.group(1)
    URL_MSG = await dl.get_reply_message()
    if URL:
        pass
    elif URL_MSG:
        URL = URL_MSG.text
    else:
        await dl.edit("`Informação vazia...`")
        return
    if not re.findall(r"\bhttps?://download.*pixelexperience.*\.org\S+", URL):
        await dl.edit("`Informação inválida...`")
        return
    driver = await chrome()
    await dl.edit("`Obtendo informações...`")
    driver.get(URL)
    error = driver.find_elements_by_class_name("swal2-content")
    if len(error) > 0:
        if error[0].text == "Arquivo Inválido.":
            await dl.edit(f"`FileNotFoundError`: {URL} inválido.")
            return
    datas = driver.find_elements_by_class_name("download__meta")
    """ - enumere os dados para ter certeza de que o download corresponda com a versão - """
    md5_origin = None
    i = None
    for index, value in enumerate(datas):
        for data in value.text.split("\n"):
            if data.startswith("MD5"):
                md5_origin = data.split(":")[1].strip()
                i = index
                break
        if md5_origin is not None and i is not None:
            break
    if md5_origin is None and i is None:
        await dl.edit("`Não há versão equivalente disponível...`")
    if URL.endswith("/"):
        file_name = URL.split("/")[-2]
    else:
        file_name = URL.split("/")[-1]
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    download = driver.find_elements_by_class_name("download__btn")[i]
    download.click()
    await dl.edit("`Começando download...`")
    file_size = human_to_bytes(download.text.split(None, 3)[-1].strip("()"))
    display_message = None
    complete = False
    start = time.time()
    while complete is False:
        if os.path.isfile(file_path + ".crdownload"):
            try:
                downloaded = os.stat(file_path + ".crdownload").st_size
                status = "Baixando"
            except OSError:  # Rare case
                await asyncio.sleep(1)
                continue
        elif os.path.isfile(file_path):
            downloaded = os.stat(file_path).st_size
            file_size = downloaded
            status = "Checando"
        else:
            await asyncio.sleep(0.3)
            continue
        diff = time.time() - start
        percentage = downloaded / file_size * 100
        speed = round(downloaded / diff, 2)
        eta = round((file_size - downloaded) / speed)
        prog_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status,
            "".join(["■" for i in range(math.floor(percentage / 10))]),
            "".join(["▨" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2),
        )
        current_message = (
            "`[DOWNLOAD]`\n\n"
            f"`{file_name}`\n"
            f"`Status`\n{prog_str}\n"
            f"`{humanbytes(downloaded)} of {humanbytes(file_size)}"
            f" @ {humanbytes(speed)}`\n"
            f"`ETA` -> {time_formatter(eta)}")
        if (round(diff % 15.00) == 0 and display_message != current_message
                or (downloaded == file_size)):
            await dl.edit(current_message)
            display_message = current_message
        if downloaded == file_size:
            if not os.path.isfile(file_path):  # Rare case
                await asyncio.sleep(1)
                continue
            MD5 = await md5(file_path)
            if md5_origin == MD5:
                complete = True
            else:
                await dl.edit("`Download corrompido...`")
                os.remove(file_path)
                driver.quit()
                return
    await dl.respond(f"`{file_name}`\n\n"
                     f"Download finalizado em `{file_path}`.")
    await dl.delete()
    driver.quit()
    return