diff --git a/main.py b/main.py index a5f75aa..57ef06b 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from functools import wraps import logging import threading import colorlog -from pikpakFs import PKFs, IsDir, IsFile +from pikpakFs import PKFs, IsDir, IsFile, PKTaskStatus import os def setup_logging(): @@ -122,8 +122,8 @@ class Console(cmd2.Cmd): logging.info("Debug mode disabled") login_parser = cmd2.Cmd2ArgumentParser() - login_parser.add_argument("username", help="username") - login_parser.add_argument("password", help="password") + login_parser.add_argument("username", help="username", nargs="?") + login_parser.add_argument("password", help="password", nargs="?") @RunSync @cmd2.with_argparser(login_parser) async def do_login(self, args): @@ -260,12 +260,20 @@ class Console(cmd2.Cmd): if not IsDir(node): await self.AsyncPrint("Invalid directory") return - await Client.Download(args.url, node) - - async def ani(self): - while True: - await asyncio.sleep(1) - await self.AsyncPrint("ani") + task = await Client.Download(args.url, node) + await self.AsyncPrint(f"Task {task.id} created") + + query_parser = cmd2.Cmd2ArgumentParser() + query_parser.add_argument("-f", "--filter", help="filter", nargs="?", choices=[member.value for member in PKTaskStatus]) + @RunSync + @cmd2.with_argparser(query_parser) + async def do_query(self, args): + """ + Query All Tasks + """ + tasks = await Client.QueryTasks(PKTaskStatus(args.filter) if args.filter is not None else None) + for task in tasks: + await self.AsyncPrint(f"{task.id}: {task.status.name}") async def mainLoop(): global MainLoop, Client diff --git a/pikpakFs.py b/pikpakFs.py index d57a87b..45fec2e 100644 --- a/pikpakFs.py +++ b/pikpakFs.py @@ -19,8 +19,8 @@ class PKTaskStatus(Enum): class PkTask: id = 0 def __init__(self, torrent : str, toDirId : str, status : PKTaskStatus = PKTaskStatus.pending): - id += 1 - self.taskId = id + PkTask.id += 1 + self.taskId = PkTask.id self.status = status self.runningTask : asyncio.Task = None @@ -86,8 +86,24 @@ class PkToken: return cls(**data) class PKFs: + async def _task_pending(self, task : PkTask): + pkTask = await self.client.offline_download(task.torrent, task.toDirId) + task.pkTaskId = pkTask["task"]["id"] + task.status = PKTaskStatus.offline_downloading + + async def _task_offline_downloading(self, task : PkTask): + waitTime = 1 + await asyncio.sleep(waitTime) + # status = await self.client.get_task_status(task.pkTaskId) + async def _task_worker(self, task : PkTask): - pass + while task.status != PKTaskStatus.done and task.status != PKTaskStatus.error: + if task.status == PKTaskStatus.pending: + await self._task_pending(task) + if task.status == PKTaskStatus.offline_downloading: + await self._task_offline_downloading(task) + break + def RunTask(self, task : PkTask): self.tasks.append(task) diff --git a/readme.md b/readme.md index 193eed4..ee580ff 100644 --- a/readme.md +++ b/readme.md @@ -6,4 +6,6 @@ Todo: - [x] 实现自定义根路径 - [x] 异步输出频率过高会导致卡死,似乎会多创建一个线程 -- [ ] 实现Task系统,自动刷新文件系统 \ No newline at end of file +- [ ] 实现Task队列管理 +- [ ] 自动刷新文件系统缓存 +- [ ] 分析以下方法的返回值:offline_file_info、offline_list、offline_task_retry、delete_tasks \ No newline at end of file