def __onDownloadComplete(self, api: API, gid): with self._resource_lock: if self.gid == gid: if api.get_download(gid).followed_by_ids: self.gid = api.get_download(gid).followed_by_ids[0] download_dict[self._listener.uid] = AriaDownloadStatus( self.gid, self._listener) update_all_messages() LOGGER.info(f'Changed gid from {gid} to {self.gid}') else: self._listener.onDownloadComplete()
def test_parse_input_file(): api = API() downloads = api.parse_input_file(INPUT_FILES[0]) assert len(downloads) == 2 downloads = api.parse_input_file(INPUT_FILES[1]) assert len(downloads) == 1 downloads = api.parse_input_file(INPUT_FILES[2]) assert len(downloads) == 0
def __onDownloadComplete(self, api: API, gid): LOGGER.info(f"onDownloadComplete: {gid}") dl = getDownloadByGid(gid) download = api.get_download(gid) if download.followed_by_ids: new_gid = download.followed_by_ids[0] new_download = api.get_download(new_gid) with download_dict_lock: download_dict[dl.uid()] = AriaDownloadStatus(new_gid, dl.getListener()) if new_download.is_torrent: download_dict[dl.uid()].is_torrent = True update_all_messages() LOGGER.info(f'Changed gid from {gid} to {new_gid}') elif dl: threading.Thread(target=dl.getListener().onDownloadComplete).start()
def HandleMultiPart(self, gid: str, api: aria2p.API, path: pathlib.Path, ext: str): multipartRegEx = [r'^(?P<filename>.+)\.part(?P<number>\d+)\.'] doExtract = False isMatched = False filename = path.name for regex in multipartRegEx: m = re.match(regex + ext[1:] + '$', filename) if (m != None): isMatched = True groupNumber = m.group('number') if groupNumber.isnumeric: dls = api.get_downloads() filterdDls = list( filter( lambda download: download.name.startswith( m.group('filename')), dls)) if all(e.is_complete for e in filterdDls): doExtract = True filename = m.group('filename') break # We have all the necessary data if not isMatched or doExtract: self.HandleArchive(gid, path, filename)
def on_complete_thread(self, api: aria2p.API, gid: str): print(datetime.datetime.now().strftime("%Y/%m/%dT%H:%M:%S.%f") + " " + gid + " OnComplete") dl = api.get_download(gid) for file in dl.files: self.HandleDownload(api, dl, file.path) print(datetime.datetime.now().strftime("%Y/%m/%dT%H:%M:%S.%f") + " " + gid + " Complete")
def __onDownloadStarted(self, api: API, gid): if STOP_DUPLICATE_MIRROR or TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: sleep(1) dl = getDownloadByGid(gid) download = api.get_download(gid) if STOP_DUPLICATE_MIRROR: LOGGER.info(f"Checking File/Folder if already in Drive...") sleep(1) self.name = aria2.get_download(gid).name sname = self.name if self.listener.isTar: sname = sname + ".tar" if self.listener.extract: smsg = None else: gdrive = GoogleDriveHelper(None) smsg, button = gdrive.drive_list(sname) if smsg: dl.getListener().onDownloadError(f'File/Folder already available in Drive.\n\n') aria2.remove([download], force = True) aria2.purge() sendMarkup("Here are the search results:", dl.getListener().bot, dl.getListener().update, button) return if TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: limit = None if TAR_UNZIP_LIMIT is not None and (self.listener.isTar or self.listener.extract): LOGGER.info(f"Checking File/Folder Size...") limit = TAR_UNZIP_LIMIT mssg = f'Tar/Unzip limit is {TAR_UNZIP_LIMIT}' if TORRENT_DIRECT_LIMIT is not None and limit is None: LOGGER.info(f"Checking File/Folder Size...") limit = TORRENT_DIRECT_LIMIT mssg = f'Torrent/Direct limit is {TORRENT_DIRECT_LIMIT}' if limit is not None: sleep(1.5) size = aria2.get_download(gid).total_length limit = limit.split(' ', maxsplit=1) limitint = int(limit[0]) if 'G' in limit[1] or 'g' in limit[1]: if size > limitint * 1024**3: dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') aria2.remove([download], force = True) aria2.purge() return elif 'T' in limit[1] or 't' in limit[1]: if size > limitint * 1024**4: dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') aria2.remove([download], force = True) aria2.purge() return update_all_messages()
def __onDownloadComplete(self, api: API, gid): with self._resource_lock: if self.gid == gid: download = api.get_download(gid) if download.followed_by_ids: self.gid = download.followed_by_ids[0] with download_dict_lock: download_dict[self.__listener.uid] = AriaDownloadStatus(self, self.__listener) if download.is_torrent: download_dict[self.__listener.uid].is_torrent = True update_all_messages() LOGGER.info(f'Changed gid from {gid} to {self.gid}') else: self.__listener.onDownloadComplete()
def __init__(self, tmp_dir, port, config=None, session=None, secret=""): self.tmp_dir = tmp_dir self.port = port # create the command used to launch an aria2c process command = [ "aria2c", f"--dir={self.tmp_dir}", "--file-allocation=none", "--quiet", "--enable-rpc=true", f"--rpc-listen-port={self.port}", ] if config: command.append(f"--conf-path={config}") else: # command.append("--no-conf") config = CONFIGS_DIR / "default.conf" command.append(f"--conf-path={config}") if session: if isinstance(session, list): session_path = self.tmp_dir / "_session.txt" with open(session_path, "w") as stream: stream.write("\n".join(session)) command.append(f"--input-file={session_path}") else: session_path = SESSIONS_DIR / session if not session_path.exists(): raise ValueError(f"no such session: {session}") command.append(f"--input-file={session_path}") if secret: command.append(f"--rpc-secret={secret}") self.command = command self.process = None # create the client with port self.client = Client(port=self.port, secret=secret, timeout=20) # create the API instance self.api = API(self.client)
def HandleDownload(self, api: aria2p.API, dl: aria2p.Download, path: pathlib.Path): archiveExt = ['.zip', '.rar'] _, file_extension = os.path.splitext(path) if file_extension == ".nfo": # API + RemoveApi/DeleteApi api.remove(downloads=[dl], files=True, clean=True) elif any(file_extension == ext for ext in archiveExt): # Extract -> MoveFs -> RemoveApi self.HandleMultiPart(dl.gid, api, path, file_extension) api.remove(downloads=[dl], clean=True) else: # MoveFs and RemoveApi self.Move(path, self.__endedpath) api.remove(downloads=[dl], clean=True)
def test_listen_to_notifications_then_stop(port): api = API(Client(port=port)) api.listen_to_notifications(threaded=True, timeout=1) api.stop_listening() assert api.listener is None
def setup_method(self): self.bitfield = ( "9e7ef5fbdefdffffdfffffffffffffdf7fff5dfefff7feffefffdff7fffffffbfeffbffddf7de9f7eefffffffeffe77bb" "f8fdbdcffef7fffffbefad7ffffffbf55bff7edfedfeff7ffff7ffffff3ffff7d3ffbfffddddffe7ffffffffffdedf7fd" "fef62fbdfffffbffbffdcfaffffafebeff3ebff7d5fffbbb2bafef77ffaffe7d37fbffffffb6dfffffffedfebbecbbffe" "bdefffffffff977f7ffdffee7fffffffdfeb3f67dddffeedfffffbfffffdaffbfeedfadef6dfd9d2df9fb7f689ffcff3f" "fbfebbfdbd7fcddfa77dfddffdfe327fdcbf77fad87ffff6ff7ffebfefdfbbffdefdafeefed7fefffe7ffad9ffdcffefc" "ffbbf3c07ef7fc0") self.download = Download( API(), { "bitfield": self.bitfield, "bittorrent": {}, "completedLength": "889592532", "connections": "44", "dir": "/home/pawamoy/Downloads/torrents/tmp", "downloadSpeed": "745509", "files": [ { "completedLength": "58132", "index": "1", "length": "58132", "path": "/home/pawamoy/Downloads/torrents/tmp/dl/logo.jpg", "selected": "true", "uris": [], }, { "completedLength": "885864384", "index": "2", "length": "1045247936", "path": "/home/pawamoy/Downloads/torrents/tmp/dl/video.mp4", "selected": "true", "uris": [], }, ], "following": "a89a9c5ac990e6ef", "gid": "0a6635c602761000", "infoHash": "4f1da018803b65f61ed76612af9ad00d4373a771", "numPieces": "1994", "numSeeders": "12", "pieceLength": "524288", "seeder": "false", "status": "active", "totalLength": "1045306068", "uploadLength": "97207027", "uploadSpeed": "11032", }, )
def test_init_method(self): assert Download(API(), {})
def test_eq_method(self): assert self.download == Download(API(), {"gid": "0a6635c602761000"})
def setup_method(self): self.api = API()
def setup_method(self): self.api = API() self.api.set_global_options = lambda x: True self.options = Options(self.api, {})