diff --git a/.gitignore b/.gitignore index 5ab84f8..1215f87 100644 --- a/.gitignore +++ b/.gitignore @@ -54,6 +54,7 @@ .hypothesis/ .pytest_cache/ cover/ +.idea/ # Translations *.mo diff --git a/.idea/misc.xml b/.idea/misc.xml index 2d0b49d..e17cdf0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/thu-learn-downloader.iml b/.idea/thu-learn-downloader.iml index 8ff5685..988d67c 100644 --- a/.idea/thu-learn-downloader.iml +++ b/.idea/thu-learn-downloader.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index ae9938c..61567ec 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,6 @@ - download pre-built binary form [GitHub Releases](https://github.com/liblaf/thu-learn-downloader/releases) - `pip install thu-learn-downloader` - `pipx install thu-learn-downloader` + +## test run command +python.exe -m thu_learn_downloader.main \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2543993..322dd72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ - annotated-types==0.7.0 ; python_version >= "3.10" and python_version < "4.0" +annotated-types==0.7.0 ; python_version >= "3.10" and python_version < "4.0" beautifulsoup4==4.12.3 ; python_version >= "3.10" and python_version < "4.0" certifi==2024.8.30 ; python_version >= "3.10" and python_version < "4.0" charset-normalizer==3.4.0 ; python_version >= "3.10" and python_version < "4.0" @@ -18,12 +18,4 @@ tenacity==9.0.0 ; python_version >= "3.10" and python_version < "4.0" typer==0.12.5 ; python_version >= "3.10" and python_version < "4.0" typing-extensions==4.12.2 ; python_version >= "3.10" and python_version < "4.0" -urllib3==2.2.3 ; python_version >= "3.10" and python_version < "4.0" - -beautifulsoup4~=4.12.3 -requests~=2.32.3 -pydantic~=2.9.2 -rich~=13.9.2 -typer~=0.12.5 -python-dateutil~=2.9.0.post0 -tenacity~=9.0.0 \ No newline at end of file +urllib3==2.2.3 ; python_version >= "3.10" and python_version < "4.0" \ No newline at end of file diff --git a/thu_learn_downloader/client/__init__.py b/thu_learn_downloader/client/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/thu_learn_downloader/client/__init__.py diff --git a/thu_learn_downloader/client/learn.py b/thu_learn_downloader/client/learn.py index 8c58077..4b822da 100644 --- a/thu_learn_downloader/client/learn.py +++ b/thu_learn_downloader/client/learn.py @@ -20,14 +20,14 @@ self.client = Client(language, *args, **kwargs) def login(self, username: str, password: str) -> None: - response: Response = self.client.get(url=url.make_url()) + response: Response = self.client.get(url=url.make_url(), verify=False) soup: BeautifulSoup = BeautifulSoup( markup=response.text, features="html.parser" ) login_form: Tag = cast(Tag, soup.select_one(selector="#loginForm")) action: str = cast(str, login_form["action"]) response: Response = self.client.post( - url=action, data={"i_user": username, "i_pass": password, "atOnce": True} + url=action, data={"i_user": username, "i_pass": password, "atOnce": True}, verify=False ) soup: BeautifulSoup = BeautifulSoup( markup=response.text, features="html.parser" @@ -36,13 +36,22 @@ href: str = cast(str, a["href"]) parse_result: ParseResult = urllib.parse.urlparse(url=href) query: dict[str, list[str]] = urllib.parse.parse_qs(qs=parse_result.query) - status, ticket = query["status"][0], query["ticket"][0] - self.client.get(url=href) + print("Query received:", query) + + status = query.get("status", ["unknown"])[0] + ticket = query.get("ticket", [None])[0] + if ticket is None: + print("Login probably failed — no ticket received.") + print("Full query dict:", query) + return + + self.client.get(url=href, verify=False) self.client.get( url=url.make_url(path="/b/j_spring_security_thauth_roaming_entry"), params={"ticket": ticket}, + verify = False ) - self.client.get(url=url.make_url(path="/f/wlxt/index/course/student/")) + self.client.get(url=url.make_url(path="/f/wlxt/index/course/student/"), verify=False) assert status == "SUCCESS" @functools.cached_property diff --git a/thu_learn_downloader/download/description.py b/thu_learn_downloader/download/description.py index dad1ded..7ca2276 100644 --- a/thu_learn_downloader/download/description.py +++ b/thu_learn_downloader/download/description.py @@ -8,18 +8,18 @@ semester: Semester, course: Course, document_class: DocumentClass, - document_content: Document, + document: Document, index: int, ) -> str: - filename: str = f"{index:02d}-{document_content.title}" - if document_content.file_type: - filename += "." + document_content.file_type + filename: str = f"{index:02d}-{document.title}" + if document.file_type: + filename += "." + document.file_type return f"{course.name} > {filename}" def attachment( - semester: Semester, course: Course, homework: Homework, attachment_content: Attachment + semester: Semester, course: Course, homework: Homework, attachment: Attachment ) -> str: return ( - f"{course.name} > {homework.number:02d}-{homework.title} > {attachment_content.type_}" + f"{course.name} > {homework.number:02d}-{homework.title} > {attachment.type_}" ) diff --git a/thu_learn_downloader/download/downloader.py b/thu_learn_downloader/download/downloader.py index 426c99f..c2c0675 100644 --- a/thu_learn_downloader/download/downloader.py +++ b/thu_learn_downloader/download/downloader.py @@ -270,14 +270,14 @@ semester=semester, course=course, document_class=document_class, - document_content=document, + document=document, index=index, ), description=description.document( semester=semester, course=course, document_class=document_class, - document_content=document, + document=document, index=index, ), remote_size=document.size, @@ -301,7 +301,7 @@ self, semester: Semester, course: Course, homework: Homework ) -> None: readme_path: Path = filename.homework( - prefix=self.prefix, semester=semester, course=course, homework_content=homework + prefix=self.prefix, semester=semester, course=course, homework=homework ) readme_path = Path(re.sub(r'[<>:"\\|?*\x00-\x1F]', '_', str(readme_path))) @@ -315,14 +315,14 @@ prefix=self.prefix, semester=semester, course=course, - homework_content=homework, - attachment_content=attachment, + homework=homework, + attachment=attachment, ), description=description.attachment( semester=semester, course=course, homework=homework, - attachment_content=attachment, + attachment=attachment, ), style=style.HOMEWORK, ) diff --git a/thu_learn_downloader/download/filename.py b/thu_learn_downloader/download/filename.py index cd2dffc..e30ba24 100644 --- a/thu_learn_downloader/download/filename.py +++ b/thu_learn_downloader/download/filename.py @@ -11,7 +11,7 @@ semester: Semester, course: Course, document_class: DocumentClass, - document_content: Document, + document: Document, index: int, ) -> Path: filename: Path = ( @@ -20,22 +20,22 @@ / course.name / "docs" / document_class.title - / f"{index:02d}-{document_content.title}".replace("/", "-slash-") + / f"{index:02d}-{document.title}".replace("/", "-slash-") ) - if document_content.file_type: - filename = filename.with_suffix("." + document_content.file_type) + if document.file_type: + filename = filename.with_suffix("." + document.file_type) return filename def homework( - prefix: Path, semester: Semester, course: Course, homework_content: Homework + prefix: Path, semester: Semester, course: Course, homework: Homework ) -> Path: return ( prefix / semester.id / course.name / "work" - / f"{homework_content.number:02d}-{homework_content.title}".replace("/", "-slash-") + / f"{homework.number:02d}-{homework.title}".replace("/", "-slash-") / "README.md" ) @@ -44,12 +44,12 @@ prefix: Path, semester: Semester, course: Course, - homework_content: Homework, - attachment_content: Attachment, + homework: Homework, + attachment: Attachment, ) -> Path: - filename: Path = Path(attachment_content.name) + filename: Path = Path(attachment.name) filename = filename.with_stem( - f"{homework_content.number:02d}-{homework_content.title}-{attachment_content.type_}".replace( + f"{homework.number:02d}-{homework.title}-{attachment.type_}".replace( "/", "-slash-" ) ) @@ -58,6 +58,6 @@ / semester.id / course.name / "work" - / f"{homework_content.number:02d}-{homework_content.title}".replace("/", "-slash-") + / f"{homework.number:02d}-{homework.title}".replace("/", "-slash-") / filename ) diff --git a/thu_learn_downloader/download/selector.py b/thu_learn_downloader/download/selector.py index 01df79f..a5725e0 100644 --- a/thu_learn_downloader/download/selector.py +++ b/thu_learn_downloader/download/selector.py @@ -2,7 +2,6 @@ from pydantic import BaseModel - class Selector(BaseModel): semesters: Sequence[str] = [] courses: Sequence[str] = [] diff --git a/thu_learn_downloader/login/auto.py b/thu_learn_downloader/login/auto.py index d27c007..d15aa4c 100644 --- a/thu_learn_downloader/login/auto.py +++ b/thu_learn_downloader/login/auto.py @@ -3,9 +3,9 @@ def username() -> str: try: - username_bitwarden: str = bitwarden.username() - if username_bitwarden: - return username_bitwarden + username: str = bitwarden.username() + if username: + return username except Exception: pass return "" @@ -13,9 +13,9 @@ def password() -> str: try: - password_bitwarden: str = bitwarden.password() - if password_bitwarden: - return password_bitwarden + password: str = bitwarden.password() + if password: + return password except Exception: pass return "" diff --git a/thu_learn_downloader/main.py b/thu_learn_downloader/main.py index b92568a..dbbb2bc 100644 --- a/thu_learn_downloader/main.py +++ b/thu_learn_downloader/main.py @@ -5,16 +5,17 @@ import typer from typer import Option, Typer -from .client.client import Language -from .client.learn import Learn -from .common.logging import LogLevel -from .download.downloader import Downloader -from .download.selector import Selector -from .login import auto as login +from thu_learn_downloader.client.client import Language +from thu_learn_downloader.client.learn import Learn +from thu_learn_downloader.common.logging import LogLevel +from thu_learn_downloader.download.downloader import Downloader +from thu_learn_downloader.download.selector import Selector +from thu_learn_downloader.login import auto as login +# import os +# os.environ['OPENSSL_CONF'] = r'D:\git-repo\thu-learn-downloader\thu_learn_downloader\openssl.conf' app: Typer = Typer(name="tld") - @app.command() def main( username: Annotated[str, Option("-u", "--username")] = "", @@ -23,7 +24,7 @@ prefix: Annotated[Path, Option(file_okay=False, writable=True)] = Path.home() # noqa: B008 / "thu-learn", semesters: Annotated[list[str], Option("-s", "--semester")] = [ # noqa: B006 - "2023-2024-1" + "2023-2024-5" ], courses: Annotated[list[str], Option("-c", "--course")] = [], # noqa: B006 document: Annotated[bool, Option()] = True,