def add_download(mega_link: str, path: str, listener): if MEGA_API_KEY is None: raise MegaDownloaderException( 'Mega API KEY not provided! Cannot mirror Mega links') if STOP_DUPLICATE_MEGA or MEGA_LIMIT is not None: msg = sendMessage('Checking Your Link...', listener.bot, listener.update) executor = AsyncExecutor() api = MegaApi(MEGA_API_KEY, None, None, 'telegram-mirror-bot') global listeners mega_listener = MegaAppListener(executor.continue_event, listener) listeners.append(mega_listener) api.addListener(mega_listener) if MEGA_EMAIL_ID is not None and MEGA_PASSWORD is not None: executor.do(api.login, (MEGA_EMAIL_ID, MEGA_PASSWORD)) link_type = get_mega_link_type(mega_link) if link_type == "file": executor.do(api.getPublicNode, (mega_link, )) node = mega_listener.public_node else: LOGGER.info("Logging into Mega folder") folder_api = MegaApi(MEGA_API_KEY, None, None, 'TgBot') folder_api.addListener(mega_listener) executor.do(folder_api.loginToFolder, (mega_link, )) node = folder_api.authorizeNode(mega_listener.node) if mega_listener.error is not None: return listener.onDownloadError(str(mega_listener.error)) if STOP_DUPLICATE_MEGA: LOGGER.info(f'Checking File/Folder if already in Drive') mname = node.getName() if listener.isTar: mname = mname + ".tar" if listener.extract: smsg = None else: gd = GoogleDriveHelper() smsg, button = gd.drive_list(mname) if smsg: deleteMessage(listener.bot, msg) msg1 = "File/Folder is already available in Drive.\nHere are the search results:" sendMarkup(msg1, listener.bot, listener.update, button) return else: if MEGA_LIMIT is None: deleteMessage(listener.bot, msg) if MEGA_LIMIT is not None: LOGGER.info(f'Checking File/Folder Size') limit = MEGA_LIMIT limit = limit.split(' ', maxsplit=1) limitint = int(limit[0]) msg3 = f'Failed, Mega limit is {MEGA_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' if 'GB' in limit or 'gb' in limit: if api.getSize(node) > limitint * 1024**3: deleteMessage(listener.bot, msg) sendMessage(msg3, listener.bot, listener.update) return else: deleteMessage(listener.bot, msg) elif 'TB' in limit or 'tb' in limit: if api.getSize(node) > limitint * 1024**4: deleteMessage(listener.bot, msg) sendMessage(msg3, listener.bot, listener.update) return else: deleteMessage(listener.bot, msg) with download_dict_lock: download_dict[listener.uid] = MegaDownloadStatus( mega_listener, listener) os.makedirs(path) gid = ''.join(random.SystemRandom().choices(string.ascii_letters + string.digits, k=8)) mega_listener.setValues(node.getName(), api.getSize(node), gid) sendStatusMessage(listener.update, listener.bot) executor.do(api.startDownload, (node, path))
def add_download(mega_link: str, path: str, listener): if MEGA_API_KEY is None: raise MegaDownloaderException( 'Mega API KEY not provided! Cannot mirror Mega links') executor = AsyncExecutor() api = MegaApi(MEGA_API_KEY, None, None, 'telegram-mirror-bot') global listeners mega_listener = MegaAppListener(executor.continue_event, listener) listeners.append(mega_listener) api.addListener(mega_listener) if MEGA_EMAIL_ID is not None and MEGA_PASSWORD is not None: executor.do(api.login, (MEGA_EMAIL_ID, MEGA_PASSWORD)) link_type = get_mega_link_type(mega_link) if link_type == "file": LOGGER.info( "File. If your download didn't start, then check your link if it's available to download" ) executor.do(api.getPublicNode, (mega_link, )) node = mega_listener.public_node else: LOGGER.info( "Folder. If your download didn't start, then check your link if it's available to download" ) folder_api = MegaApi(MEGA_API_KEY, None, None, 'TgBot') folder_api.addListener(mega_listener) executor.do(folder_api.loginToFolder, (mega_link, )) node = folder_api.authorizeNode(mega_listener.node) if mega_listener.error is not None: return listener.onDownloadError(str(mega_listener.error)) if STOP_DUPLICATE: LOGGER.info(f'Checking File/Folder if already in Drive') mname = node.getName() if listener.isTar: mname = mname + ".tar" if listener.extract: smsg = None else: gd = GoogleDriveHelper() smsg, button = gd.drive_list(mname) if smsg: msg1 = "File/Folder is already available in Drive.\nHere are the search results:" sendMarkup(msg1, listener.bot, listener.update, button) executor.continue_event.set() return if MEGA_LIMIT is not None or TAR_UNZIP_LIMIT is not None: size = api.getSize(node) if listener.isTar or listener.extract: is_tar_ext = True msg3 = f'Failed, Tar/Unzip limit is {TAR_UNZIP_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' else: is_tar_ext = False msg3 = f'Failed, Mega limit is {MEGA_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' result = check_limit(size, MEGA_LIMIT, TAR_UNZIP_LIMIT, is_tar_ext) if result: sendMessage(msg3, listener.bot, listener.update) executor.continue_event.set() return with download_dict_lock: download_dict[listener.uid] = MegaDownloadStatus( mega_listener, listener) os.makedirs(path) gid = ''.join(random.SystemRandom().choices(string.ascii_letters + string.digits, k=8)) mega_listener.setValues(node.getName(), api.getSize(node), gid) sendStatusMessage(listener.update, listener.bot) executor.do(api.startDownload, (node, path))
def add_mega_download(mega_link: str, path: str, listener): executor = AsyncExecutor() api = MegaApi(MEGA_API_KEY, None, None, 'mirror-leech-telegram-bot') mega_listener = MegaAppListener(executor.continue_event, listener) global listeners api.addListener(mega_listener) listeners.append(mega_listener) if MEGA_EMAIL_ID is not None and MEGA_PASSWORD is not None: executor.do(api.login, (MEGA_EMAIL_ID, MEGA_PASSWORD)) link_type = get_mega_link_type(mega_link) if link_type == "file": LOGGER.info( "File. If your download didn't start, then check your link if it's available to download" ) executor.do(api.getPublicNode, (mega_link, )) node = mega_listener.public_node else: LOGGER.info( "Folder. If your download didn't start, then check your link if it's available to download" ) folder_api = MegaApi(MEGA_API_KEY, None, None, 'mltb') folder_api.addListener(mega_listener) executor.do(folder_api.loginToFolder, (mega_link, )) node = folder_api.authorizeNode(mega_listener.node) if mega_listener.error is not None: return sendMessage(str(mega_listener.error), listener.bot, listener.update) if STOP_DUPLICATE and not listener.isLeech: LOGGER.info('Checking File/Folder if already in Drive') mname = node.getName() if listener.isZip: mname = mname + ".zip" elif listener.extract: try: mname = get_base_name(mname) except: mname = None if mname is not None: smsg, button = GoogleDriveHelper().drive_list(mname, True) if smsg: msg1 = "File/Folder is already available in Drive.\nHere are the search results:" return sendMarkup(msg1, listener.bot, listener.update, button) if any([STORAGE_THRESHOLD, ZIP_UNZIP_LIMIT, MEGA_LIMIT]): size = api.getSize(node) arch = any([listener.isZip, listener.extract]) if STORAGE_THRESHOLD is not None: acpt = check_storage_threshold(size, arch) if not acpt: msg = f'You must leave {STORAGE_THRESHOLD}GB free storage.' msg += f'\nYour File/Folder size is {get_readable_file_size(size)}' return sendMessage(msg, listener.bot, listener.update) limit = None if ZIP_UNZIP_LIMIT is not None and arch: msg3 = f'Failed, Zip/Unzip limit is {ZIP_UNZIP_LIMIT}GB.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' limit = ZIP_UNZIP_LIMIT elif MEGA_LIMIT is not None: msg3 = f'Failed, Mega limit is {MEGA_LIMIT}GB.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' limit = MEGA_LIMIT if limit is not None: LOGGER.info('Checking File/Folder Size...') if size > limit * 1024**3: return sendMessage(msg3, listener.bot, listener.update) with download_dict_lock: download_dict[listener.uid] = MegaDownloadStatus( mega_listener, listener) makedirs(path) gid = ''.join(random.SystemRandom().choices(string.ascii_letters + string.digits, k=8)) mega_listener.setValues(node.getName(), api.getSize(node), gid) sendStatusMessage(listener.update, listener.bot) executor.do(api.startDownload, (node, path))