Beispiel #1
0
async def ss_video(message: Message):
    reply_ = message.reply_to_message
    if not reply_:
        return await message.edit("`Reply to video...`", del_in=5)
    if not reply_.video and not reply_.animation:
        return await message.edit("`Reply to a video...`", del_in=5)
    try:
        frame = message.filtered_input_str
        if not frame:
            frame = 4
        else:
            frame = int(frame)
        if frame > 10:
            return await message.edit("`Limit is 10 frames...`", del_in=5)
    except BaseException:
        return await message.edit("`Please input number of frames...`",
                                  del_in=5)
    c_time = time.time()
    await message.edit("`Downloading media...`")
    ss = await userge.download_media(
        reply_,
        "ss.mp4",
        progress_args=lambda d, t: asyncio.get_event_loop().create_task(
            progress(d, t, reply_, c_time, "[DOWNLOAD]")),
    )
    try:
        await message.edit("`Proccessing...`")
        command = f"vcsi -g {frame}x{frame} {ss} -o ss.png "
        os.system(command)
        if "-d" in message.flags:
            await userge.send_document(
                message.chat.id,
                "ss.png",
                reply_to_message_id=reply_.message_id,
            )
        else:
            await userge.send_photo(
                message.chat.id,
                "ss.png",
                reply_to_message_id=reply_.message_id,
            )
        await message.delete()
    except BaseException as e:
        await message.edit(f"<b>Error:</b> `{e}`", del_in=5)
    os.remove(ss)
    os.system("rm -rf ss.png")
async def upload_google_photos(message: Message):
    if not message.reply_to_message:
        await message.edit_text(
            "©️ <b>[Forwarded from utubebot]</b>\nno one gonna help you 🤣🤣🤣🤣",
            parse_mode="html")
        return
    creds = await check_creds(message)
    if not creds:
        await message.edit_text("😏 <code>gphoto setup</code> first 😡😒😒",
                                parse_mode="html")
        return
    await message.edit("`proccesing ...`")
    service = build("photoslibrary", "v1", http=creds.authorize(Http()))
    # create directory if not exists
    if not os.path.isdir(Config.DOWN_PATH):
        os.makedirs(Config.DOWN_PATH)
    file_path = None
    vid = await message.reply_to_message.download(
        file_name=Config.DOWN_PATH,
        progress=progress,
        progress_args=(message, "downloading🧐?"))
    file_path = os.path.join(Config.DOWN_PATH, os.path.basename(vid))
    # LOG.info(file_path)
    if not file_path:
        await message.edit_text("<b>[stop spamming]</b>", parse_mode="html")
        return
    file_name, mime_type, file_size = file_ops(file_path)
    await message.edit_text("file downloaded, gathering upload informations ")
    async with aiohttp.ClientSession() as session:
        headers = {
            "Content-Length": "0",
            "X-Goog-Upload-Command": "start",
            "X-Goog-Upload-Content-Type": mime_type,
            "X-Goog-Upload-File-Name": file_name,
            "X-Goog-Upload-Protocol": "resumable",
            "X-Goog-Upload-Raw-Size": str(file_size),
            "Authorization": "Bearer " + creds.access_token,
        }
        # Step 1: Initiating an upload session
        step_one_response = await session.post(f"{PHOTOS_BASE_URI}/v1/uploads",
                                               headers=headers)
        if step_one_response.status != 200:
            await message.edit_text((await step_one_response.text()))
            return
        step_one_resp_headers = step_one_response.headers
        # LOG.info(step_one_resp_headers)
        # Step 2: Saving the session URL
        real_upload_url = step_one_resp_headers.get("X-Goog-Upload-URL")
        # LOG.info(real_upload_url)
        upload_granularity = int(
            step_one_resp_headers.get("X-Goog-Upload-Chunk-Granularity"))
        # LOG.info(upload_granularity)
        # https://t.me/c/1279877202/74
        number_of_req_s = int(file_size / upload_granularity)
        # LOG.info(number_of_req_s)
        loop = asyncio.get_event_loop()
        async with aiofiles.open(file_path, mode="rb") as f_d:
            for i in range(number_of_req_s):
                current_chunk = await f_d.read(upload_granularity)
                offset = i * upload_granularity
                part_size = len(current_chunk)
                headers = {
                    "Content-Length": str(part_size),
                    "X-Goog-Upload-Command": "upload",
                    "X-Goog-Upload-Offset": str(offset),
                    "Authorization": "Bearer " + creds.access_token,
                }
                # LOG.info(i)
                # LOG.info(headers)
                response = await session.post(real_upload_url,
                                              headers=headers,
                                              data=current_chunk)
                loop.create_task(
                    progress(offset + part_size, file_size, message,
                             "uploading(gphoto)🧐?"))
                # LOG.info(response.headers)
                # https://github.com/SpEcHiDe/UniBorg/commit/8267811b1248c00cd1e34041e2ae8c82b207970f
                # await f_d.seek(upload_granularity)
            # await f_d.seek(upload_granularity)
            current_chunk = await f_d.read(upload_granularity)
            # https://t.me/c/1279877202/74
            # LOG.info(number_of_req_s)
            headers = {
                "Content-Length": str(len(current_chunk)),
                "X-Goog-Upload-Command": "upload, finalize",
                "X-Goog-Upload-Offset":
                str(number_of_req_s * upload_granularity),
                "Authorization": "Bearer " + creds.access_token,
            }
            # LOG.info(headers)
            response = await session.post(real_upload_url,
                                          headers=headers,
                                          data=current_chunk)
            # LOG.info(response.headers)
        final_response_text = await response.text()
        # LOG.info(final_response_text)
    await message.edit_text("uploaded to Google Photos, getting FILE URI 🤔🤔")
    response_create_album = service.mediaItems().batchCreate(
        body={
            "newMediaItems": [{
                "description": "uploaded using @UniBorg v7",
                "simpleMediaItem": {
                    "fileName": file_name,
                    "uploadToken": final_response_text
                }
            }]
        }).execute()
    # LOG.info(response_create_album)
    try:
        photo_url = response_create_album.get("newMediaItemResults")[0].get(
            "mediaItem").get("productUrl")
        await message.edit_text(photo_url)
    except Exception as e:  # pylint: disable=broad-except
        await message.edit_text(str(e))
Beispiel #3
0
async def upload_google_photos(message: Message):
    creds = await check_creds(message)
    if not creds:
        await message.edit_text("😏 <code>gpsetup</code> first 😡😒😒", parse_mode="html")
        return
    path_ = ""
    if message.input_str:
        if re.search(r"(?:https?|ftp)://[^|\s]+\.[^|\s]+", message.input_str):
            path_, _ = await url_download(message, message.input_str)
        elif os.path.exists(message.input_str):
            path_ = message.input_str
    elif message.reply_to_message and message.reply_to_message.media:
        path_, _ = await tg_download(message, message.reply_to_message)
    if not path_:
        await message.err("what should i upload ?")
        return
    await message.edit("`proccesing ...`")
    service = build("photoslibrary", "v1", http=creds.authorize(Http()))
    file_name, mime_type, file_size = file_ops(path_)
    await message.edit_text("file downloaded, gathering upload informations ")
    async with aiohttp.ClientSession() as session:
        headers = {
            "Content-Length": "0",
            "X-Goog-Upload-Command": "start",
            "X-Goog-Upload-Content-Type": mime_type,
            "X-Goog-Upload-File-Name": file_name,
            "X-Goog-Upload-Protocol": "resumable",
            "X-Goog-Upload-Raw-Size": str(file_size),
            "Authorization": "Bearer " + creds.access_token,
        }
        # Step 1: Initiating an upload session
        step_one_response = await session.post(
            f"{PHOTOS_BASE_URI}/v1/uploads", headers=headers
        )
        if step_one_response.status != 200:
            await message.edit_text((await step_one_response.text()))
            return
        step_one_resp_headers = step_one_response.headers
        # LOG.info(step_one_resp_headers)
        # Step 2: Saving the session URL
        real_upload_url = step_one_resp_headers.get("X-Goog-Upload-URL")
        # LOG.info(real_upload_url)
        upload_granularity = int(
            step_one_resp_headers.get("X-Goog-Upload-Chunk-Granularity")
        )
        # LOG.info(upload_granularity)
        # https://t.me/c/1279877202/74
        number_of_req_s = int(file_size / upload_granularity)
        # LOG.info(number_of_req_s)
        loop = asyncio.get_event_loop()
        async with aiofiles.open(path_, mode="rb") as f_d:
            for i in range(number_of_req_s):
                current_chunk = await f_d.read(upload_granularity)
                offset = i * upload_granularity
                part_size = len(current_chunk)
                headers = {
                    "Content-Length": str(part_size),
                    "X-Goog-Upload-Command": "upload",
                    "X-Goog-Upload-Offset": str(offset),
                    "Authorization": "Bearer " + creds.access_token,
                }
                # LOG.info(i)
                # LOG.info(headers)
                response = await session.post(
                    real_upload_url, headers=headers, data=current_chunk
                )
                loop.create_task(
                    progress(
                        offset + part_size, file_size, message, "uploading(gphoto)🧐?"
                    )
                )
                # LOG.info(response.headers)
                # https://github.com/SpEcHiDe/UniBorg/commit/8267811b1248c00cd1e34041e2ae8c82b207970f
                # await f_d.seek(upload_granularity)
            # await f_d.seek(upload_granularity)
            current_chunk = await f_d.read(upload_granularity)
            # https://t.me/c/1279877202/74
            # LOG.info(number_of_req_s)
            headers = {
                "Content-Length": str(len(current_chunk)),
                "X-Goog-Upload-Command": "upload, finalize",
                "X-Goog-Upload-Offset": str(number_of_req_s * upload_granularity),
                "Authorization": "Bearer " + creds.access_token,
            }
            # LOG.info(headers)
            response = await session.post(
                real_upload_url, headers=headers, data=current_chunk
            )
            # LOG.info(response.headers)
        final_response_text = await response.text()
        # LOG.info(final_response_text)
    await message.edit_text("uploaded to Google Photos, getting FILE URI 🤔🤔")
    response_create_album = (
        service.mediaItems()
        .batchCreate(
            body={
                "newMediaItems": [
                    {
                        "description": "uploaded using @UniBorg v7",
                        "simpleMediaItem": {
                            "fileName": file_name,
                            "uploadToken": final_response_text,
                        },
                    }
                ]
            }
        )
        .execute()
    )
    # LOG.info(response_create_album)
    try:
        photo_url = (
            response_create_album.get("newMediaItemResults")[0]
            .get("mediaItem")
            .get("productUrl")
        )
        await message.edit_text(photo_url)
    except Exception as e:  # pylint: disable=broad-except
        await message.edit_text(str(e))