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 - 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 - UPLOADED]" 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
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"[{file_name}]({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
--max-connection-per-server=10 \ --rpc-max-request-size=1024M \ --seed-time=0.01 \ --max-upload-limit=5K \ --max-concurrent-downloads=5 \ --min-split-size=10M \ --follow-torrent=mem \ --split=10 \ --bt-tracker={trackers} \ --daemon=true \ --allow-overwrite=true" subprocess_run(cmd) if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY): os.makedirs(TEMP_DOWNLOAD_DIRECTORY) download_path = os.getcwd() + TEMP_DOWNLOAD_DIRECTORY.strip('.') aria2 = aria2p.API(aria2p.Client(host="http://localhost", port=8210, secret="")) aria2.set_global_options({'dir': download_path}) @register(outgoing=True, pattern="^.amag(?: |$)(.*)") async def magnet_download(event): magnet_uri = event.pattern_match.group(1) # Add Magnet URI Into Queue try: download = aria2.add_magnet(magnet_uri) except Exception as e: LOGS.info(str(e))