monash-study-kit 1.8.0__tar.gz → 1.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/CHANGELOG.md +11 -0
  2. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/PKG-INFO +30 -1
  3. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/README.md +29 -0
  4. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/README.zh-CN.md +29 -0
  5. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/__init__.py +1 -1
  6. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/cli.py +42 -9
  7. monash_study_kit-1.9.0/src/monash_study_kit/cli_google.py +32 -0
  8. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/edexport.py +11 -1
  9. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/edsync.py +34 -8
  10. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/export.py +63 -6
  11. monash_study_kit-1.9.0/src/monash_study_kit/google_export.py +348 -0
  12. monash_study_kit-1.9.0/src/monash_study_kit/htmlmd.py +181 -0
  13. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/jobs.py +23 -8
  14. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/lessons.py +67 -2
  15. monash_study_kit-1.9.0/src/monash_study_kit/moodle_content.py +108 -0
  16. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/moodlelib.py +6 -0
  17. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/paths.py +1 -0
  18. monash_study_kit-1.9.0/src/monash_study_kit/syncer.py +718 -0
  19. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_course_export.py +121 -1
  20. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_ed_export.py +86 -0
  21. monash_study_kit-1.9.0/tests/test_ed_tracking.py +35 -0
  22. monash_study_kit-1.9.0/tests/test_google_export.py +216 -0
  23. monash_study_kit-1.9.0/tests/test_moodle_content.py +178 -0
  24. monash_study_kit-1.8.0/src/monash_study_kit/syncer.py +0 -393
  25. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  26. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  27. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
  28. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/pull_request_template.md +0 -0
  29. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/workflows/release.yml +0 -0
  30. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.github/workflows/test.yml +0 -0
  31. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/.gitignore +0 -0
  32. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/CODE_OF_CONDUCT.md +0 -0
  33. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/CONTRIBUTING.md +0 -0
  34. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/LICENSE +0 -0
  35. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/PRIVACY.md +0 -0
  36. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/SECURITY.md +0 -0
  37. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/SECURITY-REVIEW-1.5-SCOPE.md +0 -0
  38. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/adr/0001-one-shared-core.md +0 -0
  39. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/adr/0002-read-only-public-package.md +0 -0
  40. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/adr/0003-release-from-a-tag.md +0 -0
  41. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/adr/0004-confirmed-mail-and-attendance-actions.md +0 -0
  42. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/adr/README.md +0 -0
  43. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/gmail-foundation.md +0 -0
  44. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/google-and-attendance-setup.md +0 -0
  45. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/panels-library-remote.md +0 -0
  46. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/releasing.md +0 -0
  47. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/screenshots/todo.png +0 -0
  48. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/docs/security.md +0 -0
  49. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/glama.json +0 -0
  50. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/pyproject.toml +0 -0
  51. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/scripts/check_dist.py +0 -0
  52. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/scripts/measure_tokens.py +0 -0
  53. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/scripts/smoke_test.py +0 -0
  54. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/skills/monash-study-kit/SKILL.md +0 -0
  55. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/__main__.py +0 -0
  56. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance.py +0 -0
  57. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_assets/PROVENANCE.md +0 -0
  58. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_assets/extract-rows.js +0 -0
  59. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_assets/shared.js +0 -0
  60. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_auto.py +0 -0
  61. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_evidence.py +0 -0
  62. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/attendance_http.py +0 -0
  63. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/browser_login.py +0 -0
  64. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/cdp.py +0 -0
  65. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/claude_setup.py +0 -0
  66. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/cli_attendance.py +0 -0
  67. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/cli_ed.py +0 -0
  68. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/cli_gmail.py +0 -0
  69. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/clients.py +0 -0
  70. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/content_index.py +0 -0
  71. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/doctor.py +0 -0
  72. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/doctor_deep.py +0 -0
  73. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/edlib.py +0 -0
  74. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/edquery.py +0 -0
  75. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/features.py +0 -0
  76. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/gmail.py +0 -0
  77. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/gmail_tools.py +0 -0
  78. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/google_client.json +0 -0
  79. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/help_menu.py +0 -0
  80. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/htmldom.py +0 -0
  81. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/lesson_reader.py +0 -0
  82. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/library.py +0 -0
  83. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/location.py +0 -0
  84. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/mcp_server.py +0 -0
  85. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/output.py +0 -0
  86. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/recordings.py +0 -0
  87. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/remote.py +0 -0
  88. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/semester.py +0 -0
  89. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/service_actions.py +0 -0
  90. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/service_jobs.py +0 -0
  91. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/service_setup.py +0 -0
  92. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/student_services.py +0 -0
  93. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/student_tools.py +0 -0
  94. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/textextract.py +0 -0
  95. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/todo.py +0 -0
  96. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/transcribe.py +0 -0
  97. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/ui/panel.html +0 -0
  98. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/update_check.py +0 -0
  99. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/webnotes.py +0 -0
  100. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/src/monash_study_kit/widgets.py +0 -0
  101. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/conftest.py +0 -0
  102. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_attendance_browser.py +0 -0
  103. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_attendance_fast.py +0 -0
  104. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_attendance_parity.py +0 -0
  105. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_cdp.py +0 -0
  106. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_cli.py +0 -0
  107. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_clients.py +0 -0
  108. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_content_index.py +0 -0
  109. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_doctor.py +0 -0
  110. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_doctor_deep.py +0 -0
  111. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_ed_auth.py +0 -0
  112. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_ed_markdown.py +0 -0
  113. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_ed_query.py +0 -0
  114. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_ed_quiz.py +0 -0
  115. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_feature_cli.py +0 -0
  116. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_features.py +0 -0
  117. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_gmail.py +0 -0
  118. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_help_menu.py +0 -0
  119. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_layout.py +0 -0
  120. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_lesson_reader.py +0 -0
  121. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_library.py +0 -0
  122. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_location.py +0 -0
  123. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_mcp.py +0 -0
  124. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_panels.py +0 -0
  125. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_recordings.py +0 -0
  126. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_remote.py +0 -0
  127. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_removed_files.py +0 -0
  128. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_security.py +0 -0
  129. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_semester.py +0 -0
  130. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_service_setup.py +0 -0
  131. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_setup.py +0 -0
  132. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_shared_core.py +0 -0
  133. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_skill.py +0 -0
  134. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_student_gmail.py +0 -0
  135. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_student_tools.py +0 -0
  136. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_sync_robustness.py +0 -0
  137. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_todo.py +0 -0
  138. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_update_check.py +0 -0
  139. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_webnotes.py +0 -0
  140. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/tests/test_without_ed.py +0 -0
  141. {monash_study_kit-1.8.0 → monash_study_kit-1.9.0}/uv.lock +0 -0
@@ -2,6 +2,17 @@
2
2
 
3
3
  Version numbers follow `__version__` in `src/monash_study_kit/__init__.py`: patch for bug fixes, minor for new features.
4
4
 
5
+ ## 1.9.0
6
+
7
+ Course exports now contain what used to be left as links, with the reason for anything still missing.
8
+
9
+ - Moodle Page, Book and assignment text, and the rubric or marking guide students can see, are saved as Markdown. Images in them are downloaded to `assets/` (named by content hash, so duplicates are stored once). Links to this unit's pages, files, folders and books are followed instead of listed. Books are read through the print view, which Moodle does not count as a view.
10
+ - Folders are downloaded whole (`download_folder.php`, not counted as a view), keeping subfolders, case and original file names. They are refreshed at most weekly, and if the zip is unavailable the files are fetched one by one. Images that a notebook references but that are missing (or differ only in case) are reported.
11
+ - Images in Ed lesson pages are downloaded and de-duplicated, with links rewritten to relative paths. Failed downloads keep the remote link and are listed in the export index. Ed and whole-unit exports include `assets/`.
12
+ - Google Slides, Docs, Sheets and Drive files linked from a unit are exported read-only through the Drive API: Slides and Docs to PDF (Docs optionally DOCX with `monash config google_docs_format docx`), Sheets to XLSX, Drive files as they are. When a file is too large or an administrator blocks the API, the dedicated login browser's Google session is tried; its cookies stay in memory. Failures keep the link and record the reason. New `monash google login` / `status` / `logout` use a separate token with only `drive.readonly` (`logout` revokes it first); `monash sync --refresh-google` exports again.
13
+ - Ed units whose code matches a tracked Moodle unit are tracked automatically, including archived and combined units. This is why a past unit's Ed Lessons could be missing. A choice made in `monash courses` is kept. `monash export --sync` syncs the unit's Moodle and Ed content first.
14
+ - The export index lists everything not exported with its reason, and gains a statistics section (links files, remote images left, Google links exported or failed by reason), also in `--json`.
15
+
5
16
  ## 1.8.0
6
17
 
7
18
  One export for a whole unit, and this package is now the only copy of the Moodle, Ed, Google and attendance code (the author's private server deployment depends on it instead of carrying its own copies).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: monash-study-kit
3
- Version: 1.8.0
3
+ Version: 1.9.0
4
4
  Summary: Connect Monash Moodle and Ed to Claude, Codex, Cursor or any local MCP client via a local MCP server and CLI: deadlines, announcements, grades, full-text search over course materials and Ed posts, all on your own machine.
5
5
  Project-URL: Homepage, https://github.com/Waldo0926/monash-study-kit
6
6
  Project-URL: Issues, https://github.com/Waldo0926/monash-study-kit/issues
@@ -71,6 +71,35 @@ The picture is real output of `monash todo` against a database filled with made-
71
71
 
72
72
  For the original v1.1 security review and the [1.5 feature review checklist](docs/SECURITY-REVIEW-1.5-SCOPE.md), see [security documentation](docs/security.md). The earlier review does not automatically certify features added later.
73
73
 
74
+
75
+ ## 1.9.0: more complete course exports
76
+
77
+ Moodle Pages, Book chapters, assignment descriptions and visible rubrics / marking guides are saved as Markdown. Moodle images are stored in course `assets/` by content hash, and internal links to Page, Resource, Folder and Book activities are followed. Folder downloads preserve original names, subdirectories and case, reuse files for seven days, and fall back to individual downloads. Missing notebook images, case mismatches, empty pages and hidden grading criteria are explained in the index.
78
+
79
+ Ed Lessons document / code images hosted on `edusercontent.com` are cached locally, deduplicated by content and limited to 200 MB each. Failed images keep their original URL and an error record. Both Ed-only and whole-unit exports include `assets/` with working relative references.
80
+
81
+ Google Slides and Docs export as PDF, Sheets as XLSX, and ordinary Drive files retain their format. Only file links found in Moodle and Ed Lessons are processed; Google Forms cannot be exported. Existing exports are reused while the file remains on disk. Successful file links disappear from generated link lists; failures retain their URL and a Chinese reason in the index.
82
+
83
+ ```bash
84
+ uv tool install --reinstall 'monash-study-kit[gmail]'
85
+ monash google login # bundled desktop OAuth client
86
+ monash google login --client client.json
87
+ monash google status
88
+ monash config google_docs_format docx # default: pdf; Google Docs only
89
+ monash sync FIT1045 --refresh-google # force Google exports to refresh
90
+ monash export FIT1045 --sync --json # sync this Moodle unit, then matching Ed units
91
+ monash google logout # revoke first, then remove the local token
92
+ ```
93
+
94
+ The bundled OAuth client is currently in Google's **Testing** state: its maintainer must add your Google account to the test-user list before you can authorize it. Google classifies `drive.readonly` as a **restricted scope**. It permits reading Drive files; this tool only downloads files linked from courses and does not enumerate your Drive. Google authorization shares the optional `[gmail]` dependency, but uses an independent flow and `secrets/google-drive-token.json` under the data home (`MONASH_KIT_SECRETS` can override the directory). Gmail authorization and `gmail-token.json` are unchanged. Credentials stay in the terminal / browser authorization flow; never paste them into chat.
95
+
96
+ Downloads use read-only GET requests. For Drive export-size or administrator restrictions, a fallback tries the existing Google session in the dedicated login browser. Cookies are used only in memory, never logged or separately saved, and redirects outside Google's domains stop. The fallback may still be blocked by policy; failures keep the original link and explain why. Missing authorization points to `monash google login`. Moodle Page, Resource and assignment `view.php` requests still record a view. Normal folder downloads and Book print requests avoid their `view.php`; the individual-file fallback must open the folder page and records a view.
97
+
98
+ Every sync checks newly discovered Ed units against tracked Moodle unit codes, including archived units and combined codes such as `FIT1045/FIT1053`; later user tracking choices are preserved. `monash export <unit> --sync` refreshes Moodle first, then matching Ed discussions and Lessons, before exporting. Ordinary exports use only local data. The README index includes all omission notes and failed Ed images, plus counts of link lists, remote Markdown images, Google links, successful exports and failures grouped by reason; `--json` exposes the same counts in `statistics`.
99
+
100
+ New tests use synthetic HTML / JSON, fake clients and mocked HTTP, with no real account requests. Ed code-challenge workspace downloads still require a real read-only response to establish the interface and are excluded, as are multi-account profiles.
101
+
102
+
74
103
  ## Two parts, one codebase
75
104
 
76
105
  The same core (login, sync, queries) serves two kinds of "users":
@@ -35,6 +35,35 @@ The picture is real output of `monash todo` against a database filled with made-
35
35
 
36
36
  For the original v1.1 security review and the [1.5 feature review checklist](docs/SECURITY-REVIEW-1.5-SCOPE.md), see [security documentation](docs/security.md). The earlier review does not automatically certify features added later.
37
37
 
38
+
39
+ ## 1.9.0: more complete course exports
40
+
41
+ Moodle Pages, Book chapters, assignment descriptions and visible rubrics / marking guides are saved as Markdown. Moodle images are stored in course `assets/` by content hash, and internal links to Page, Resource, Folder and Book activities are followed. Folder downloads preserve original names, subdirectories and case, reuse files for seven days, and fall back to individual downloads. Missing notebook images, case mismatches, empty pages and hidden grading criteria are explained in the index.
42
+
43
+ Ed Lessons document / code images hosted on `edusercontent.com` are cached locally, deduplicated by content and limited to 200 MB each. Failed images keep their original URL and an error record. Both Ed-only and whole-unit exports include `assets/` with working relative references.
44
+
45
+ Google Slides and Docs export as PDF, Sheets as XLSX, and ordinary Drive files retain their format. Only file links found in Moodle and Ed Lessons are processed; Google Forms cannot be exported. Existing exports are reused while the file remains on disk. Successful file links disappear from generated link lists; failures retain their URL and a Chinese reason in the index.
46
+
47
+ ```bash
48
+ uv tool install --reinstall 'monash-study-kit[gmail]'
49
+ monash google login # bundled desktop OAuth client
50
+ monash google login --client client.json
51
+ monash google status
52
+ monash config google_docs_format docx # default: pdf; Google Docs only
53
+ monash sync FIT1045 --refresh-google # force Google exports to refresh
54
+ monash export FIT1045 --sync --json # sync this Moodle unit, then matching Ed units
55
+ monash google logout # revoke first, then remove the local token
56
+ ```
57
+
58
+ The bundled OAuth client is currently in Google's **Testing** state: its maintainer must add your Google account to the test-user list before you can authorize it. Google classifies `drive.readonly` as a **restricted scope**. It permits reading Drive files; this tool only downloads files linked from courses and does not enumerate your Drive. Google authorization shares the optional `[gmail]` dependency, but uses an independent flow and `secrets/google-drive-token.json` under the data home (`MONASH_KIT_SECRETS` can override the directory). Gmail authorization and `gmail-token.json` are unchanged. Credentials stay in the terminal / browser authorization flow; never paste them into chat.
59
+
60
+ Downloads use read-only GET requests. For Drive export-size or administrator restrictions, a fallback tries the existing Google session in the dedicated login browser. Cookies are used only in memory, never logged or separately saved, and redirects outside Google's domains stop. The fallback may still be blocked by policy; failures keep the original link and explain why. Missing authorization points to `monash google login`. Moodle Page, Resource and assignment `view.php` requests still record a view. Normal folder downloads and Book print requests avoid their `view.php`; the individual-file fallback must open the folder page and records a view.
61
+
62
+ Every sync checks newly discovered Ed units against tracked Moodle unit codes, including archived units and combined codes such as `FIT1045/FIT1053`; later user tracking choices are preserved. `monash export <unit> --sync` refreshes Moodle first, then matching Ed discussions and Lessons, before exporting. Ordinary exports use only local data. The README index includes all omission notes and failed Ed images, plus counts of link lists, remote Markdown images, Google links, successful exports and failures grouped by reason; `--json` exposes the same counts in `statistics`.
63
+
64
+ New tests use synthetic HTML / JSON, fake clients and mocked HTTP, with no real account requests. Ed code-challenge workspace downloads still require a real read-only response to establish the interface and are excluded, as are multi-account profiles.
65
+
66
+
38
67
  ## Two parts, one codebase
39
68
 
40
69
  The same core (login, sync, queries) serves two kinds of "users":
@@ -36,6 +36,35 @@ Moodle和Ed始终**只读**:不会交作业、做测验或在Ed发帖。可选
36
36
 
37
37
  之前的[1.1安全审计](docs/security.md)只覆盖当时的功能;新增的远程连接、邮箱与考勤另列有[1.5审查清单](docs/SECURITY-REVIEW-1.5-SCOPE.md),不能直接沿用旧版结论。
38
38
 
39
+
40
+ ## 1.9.0:补全课程资料导出
41
+
42
+ - Moodle 的 Page、Book 章节、作业说明和学生能看到的 rubric / marking guide 存成 Markdown;正文里的 Moodle 图片存到课程的 `assets/`,同内容去重。指向本课程 Page、Resource、Folder、Book 的内部链接也会跟进。
43
+ - 文件夹优先整包下载,保留 notebook、图片的原名、子目录和大小写,7 天内跳过重复下载;失败时逐文件下载。索引会说明 notebook 图片缺失、大小写不一致、正文为空或评分细则未显示等情况。
44
+ - Ed Lessons 的 document / code 正文图片只下载 Ed 自己的 `edusercontent.com` 地址,按内容去重并缓存 URL,每张最多 200 MB。下载失败保留原链接并记录原因;独立 Ed 导出和整课导出都包含 `assets/`,图片使用相对路径。
45
+ - Google Slides / Docs 默认导出 PDF,Sheets 导出 XLSX,Drive 普通文件按原格式下载。只处理 Moodle 和 Ed Lessons 里出现过的文件链接;Google 表单不能导出。文件还在时跳过,`monash sync --refresh-google` 强制重新导出。
46
+ - 每次同步检查新出现的 Ed 课程:课号与已跟踪 Moodle 课程相同就自动跟踪,包括归档课程和 `FIT1045/FIT1053` 这样的联合课号;处理过的课程保留用户选择。
47
+ - `monash export FIT1045 --sync` 先同步这门 Moodle 课及对应 Ed 课再导出。普通导出继续只读本机缓存。README 索引列出未导出内容和原因,并统计链接清单数量、Markdown 远程图片数、Google 总数 / 成功 / 按原因分组的失败数;`--json` 的 `statistics` 提供相同数字。
48
+
49
+ ```bash
50
+ # Google 授权依赖与 Gmail 共用,但授权流程和 token 分开
51
+ uv tool install --reinstall 'monash-study-kit[gmail]'
52
+ monash google login # 默认使用安装包里的桌面 OAuth 客户端
53
+ monash google login --client client.json # 也可以用自己的桌面客户端
54
+ monash google status
55
+ monash config google_docs_format docx # 默认 pdf;只影响 Google Docs
56
+ monash sync FIT1045 --refresh-google
57
+ monash export FIT1045 --sync --json
58
+ monash google logout # 先向 Google 吊销,再删除本机令牌
59
+ ```
60
+
61
+ 自带 OAuth 客户端目前处于 Google 的“测试”状态,授权账号需要先由客户端维护者加入测试用户名单。`drive.readonly` 在 Google 属于受限 scope,允许读取 Drive 文件;本工具仅下载课程链接里出现的文件,不遍历整个 Drive。令牌单独存在数据目录的 `secrets/google-drive-token.json`(可通过 `MONASH_KIT_SECRETS` 改位置),不修改 Gmail 的授权流程或 `gmail-token.json`。令牌只在终端 / 浏览器授权流程中流转,不要贴进聊天。
62
+
63
+ 课程下载走只读 GET。Drive API 报大小限制或管理员限制时,会尝试专用登录浏览器中已有的 Google 会话;cookie 只在内存使用,不写日志或另存文件,跳转离开 Google 域名会停止。后备路径不能保证绕过管理员限制,仍失败时保留原链接并在索引说明原因。未授权时提示 `monash google login`。Moodle Page、Resource、作业仍会打开 `view.php`,Moodle 会记录查看;文件夹正常下载和 Book 打印视图不打开各自的 `view.php`。文件夹整包下载失败后的逐文件回退需要打开文件夹页,也会记录一次查看。
64
+
65
+ 本版新增测试均使用合成 HTML / JSON、假客户端或假 HTTP,不访问真实课程、Google 账号或 Ed 工作区。Ed code challenge 工作区下载暂未实现,仍需真实只读响应确认接口;多账号 profile 等不在本版范围内。
66
+
67
+
39
68
  ## 它由两部分组成
40
69
 
41
70
  同一套底层代码(登录、同步、查询),给两种“用户”用:
@@ -1,4 +1,4 @@
1
1
  """Monash 学习助手:把 Moodle 和 Ed 接进 Claude。"""
2
2
  # 发新版就改这里(pyproject 从这里读):朋友那边的更新提示比的就是 main 上的这个号。
3
3
  # 修 bug 加最后一位,加功能加中间一位。CI 会检查改了 src/ 的 PR 有没有改它。
4
- __version__ = "1.8.0"
4
+ __version__ = "1.9.0"
@@ -49,7 +49,7 @@ from .edlib import EdAuthError
49
49
  from .moodlelib import FILES_DIR, MoodleAuthError, MoodleClient, MoodleError, db_connect
50
50
  from .output import emit_json, local_time, relative, table, want_json
51
51
  from .paths import HOME, ensure_private_dir, load_settings, save_settings
52
- from .syncer import course_code, course_folder
52
+ from .syncer import course_code, course_folder, write_links_md
53
53
 
54
54
 
55
55
  def _fmt_secs(s: int | None) -> str:
@@ -128,7 +128,7 @@ def _pick(items: list[str], chosen: set[int], title: str) -> set[int] | None:
128
128
 
129
129
 
130
130
  def choose_courses(interactive: bool = True) -> None:
131
- """Moodle 默认跟踪本学期的课,Ed 默认跟踪 active 的课;interactive 时让人改。"""
131
+ """Moodle 默认跟踪本学期的课,Ed 按已跟踪的 Moodle 课号匹配;interactive 时让人改。"""
132
132
  try:
133
133
  client, con = MoodleClient(), db_connect()
134
134
  courses = sorted(jobs.load_courses(client, con), key=lambda c: -(c.get("startdate") or 0))
@@ -152,8 +152,7 @@ def choose_courses(interactive: bool = True) -> None:
152
152
  conn = edlib.db_connect()
153
153
  ecs = ed.courses()
154
154
  edsync.upsert_courses(conn, ecs)
155
- if not conn.execute("SELECT 1 FROM courses WHERE tracked=1").fetchone():
156
- edsync.auto_track(conn, ecs)
155
+ edsync.auto_track(conn, ecs)
157
156
  rows = [dict(r) for r in conn.execute("SELECT id, code, name, status, tracked FROM courses "
158
157
  "ORDER BY status != 'active', id DESC")]
159
158
  if interactive:
@@ -291,10 +290,11 @@ def cmd_sync(args):
291
290
  client, con = MoodleClient(), db_connect()
292
291
  courses = jobs.load_courses(client, con)
293
292
  only = [F.find_course(courses, r) for r in args.courses] if args.courses else None
294
- st = jobs.sync_moodle(only=only, dry_run=args.dry_run)
293
+ con.close()
294
+ st = jobs.sync_moodle(only=only, dry_run=args.dry_run, refresh_google=getattr(args, "refresh_google", False))
295
295
  print(f"新下载 {st['downloaded']} 个文件,{st['bytes'] / 1e6:.1f} MB")
296
296
  return
297
- st = jobs.sync_all()
297
+ st = jobs.sync_all(refresh_google=True) if getattr(args, "refresh_google", False) else jobs.sync_all()
298
298
  m, e, idx = st.get("moodle") or {}, st.get("ed") or {}, st.get("index") or {}
299
299
  print("\n同步完成:" + ",".join(x for x in [
300
300
  f"Ed 新帖 {e.get('new_threads', 0)}、新回复 {e.get('new_replies', 0)}" if "ed" not in st["errors"] else "",
@@ -329,6 +329,8 @@ def cmd_open(args):
329
329
 
330
330
  def parse_setting(key: str, raw: str, current):
331
331
  """按默认值的类型解析。数字一律先当小数读:阿德莱德是 UTC+9.5,auto_sync_hours 也可以写 0.5。"""
332
+ if key == "google_docs_format" and raw not in {"pdf", "docx"}:
333
+ raise ValueError("google_docs_format 只能是 pdf 或 docx")
332
334
  if isinstance(current, bool):
333
335
  if raw.lower() in ("true", "yes", "1", "on"):
334
336
  return True
@@ -626,12 +628,40 @@ def cmd_due(args):
626
628
  return emit_json(items) if want_json(args) else _print_due(items)
627
629
 
628
630
 
631
+ def sync_export_course(ref: str, log=print) -> None:
632
+ """先同步指定 Moodle 课,再同步对应 Ed 课,最后补导 Ed 的 Google 链接。"""
633
+ from . import google_export
634
+ client, con = MoodleClient(), db_connect()
635
+ try:
636
+ courses = jobs.load_courses(client, con)
637
+ course = F.find_course(courses, ref)
638
+ jobs.sync_moodle(only=[course], log=log)
639
+ if jobs.have_ed_token():
640
+ jobs.sync_ed(log=log, course_code=course_code(course))
641
+ econ = edlib.db_connect()
642
+ try:
643
+ google_export.sync_course(con, course, ed_conn=econ, log=log)
644
+ write_links_md(con, course["id"], course_folder(course))
645
+ finally:
646
+ econ.close()
647
+ elif load_settings().get("use_ed", True):
648
+ log("还没设置 Ed 令牌(monash login ed;不用 Ed 就 monash config use_ed false)")
649
+ finally:
650
+ con.close()
651
+
652
+
629
653
  def cmd_export(args):
630
654
  """一门课的 Moodle 课件 + Ed Lessons 合成一份课程资料(zip 或文件夹),附索引。只用本机已同步的数据。"""
631
655
  from . import export as X, moodlelib
632
656
  try:
633
- d = X.export(args.course, args.to, as_dir=args.dir,
634
- moodle_con=moodlelib.db_connect(), ed_conn=edlib.db_connect())
657
+ if getattr(args, "sync", False):
658
+ sync_export_course(args.course, log=(lambda text: print(text, file=sys.stderr)))
659
+ mcon, econ = moodlelib.db_connect(), edlib.db_connect()
660
+ try:
661
+ d = X.export(args.course, args.to, as_dir=args.dir, moodle_con=mcon, ed_conn=econ)
662
+ finally:
663
+ mcon.close()
664
+ econ.close()
635
665
  except LookupError as e:
636
666
  print(e, file=sys.stderr)
637
667
  return 1
@@ -870,7 +900,7 @@ class _Parser(argparse.ArgumentParser):
870
900
 
871
901
 
872
902
  def build_parser() -> argparse.ArgumentParser:
873
- from . import cli_attendance, cli_ed, cli_gmail
903
+ from . import cli_attendance, cli_ed, cli_gmail, cli_google
874
904
  ap = _Parser(prog="monash", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
875
905
  ap.add_argument("--version", action="version", version=f"monash-study-kit {__version__}")
876
906
  sub = ap.add_subparsers(dest="cmd")
@@ -897,6 +927,7 @@ def build_parser() -> argparse.ArgumentParser:
897
927
  p = cmd(sub, "sync", cmd_sync, help="同步 Ed + Moodle + 全文索引")
898
928
  p.add_argument("courses", nargs="*", help="只同步这几门 Moodle 课")
899
929
  p.add_argument("--dry-run", action="store_true", help="只列出 Moodle 会下载什么")
930
+ p.add_argument("--refresh-google", action="store_true", help="强制重新导出 Google 文件")
900
931
  cmd(sub, "courses", cmd_courses, help="选要跟踪的课")
901
932
  p = cmd(sub, "todo", cmd_todo, json_flag=True, help="本周待办")
902
933
  p.add_argument("--days", type=int, default=7)
@@ -912,6 +943,7 @@ def build_parser() -> argparse.ArgumentParser:
912
943
  p.add_argument("--to", help="保存到哪个文件夹(默认“下载”)")
913
944
  p.add_argument("--dir", action="store_true", help="写成文件夹,不打 zip")
914
945
  p.add_argument("--open", action="store_true", help="导出后在文件管理器里显示")
946
+ p.add_argument("--sync", action="store_true", help="导出前同步这门 Moodle 和 Ed 课程")
915
947
  p = cmd(sub, "media", cmd_media, help="录播:YouTube 字幕、Zoom 录像下载和转写(要装 [media])")
916
948
  p.add_argument("--limit", type=int, default=0, help="这次最多转写几个视频")
917
949
  p.add_argument("--match", help="只转写路径里含这段文字的视频")
@@ -974,6 +1006,7 @@ def build_parser() -> argparse.ArgumentParser:
974
1006
  p = cmd(m, "ls", cmd_m_ls, help="本地已同步的课件"); p.add_argument("course"); p.add_argument("week", nargs="?")
975
1007
 
976
1008
  cli_ed.add_commands(sub.add_parser("ed", help="Ed 查询").add_subparsers(dest="ecmd", required=True))
1009
+ cli_google.add_commands(sub.add_parser("google", help="Google 课程文件只读授权").add_subparsers(dest="google_cmd", required=True))
977
1010
  cli_gmail.add_commands(sub.add_parser("gmail", help="Gmail 同步、离线搜索与可选邮箱操作").add_subparsers(dest="gcmd", required=True))
978
1011
  cli_attendance.add_commands(sub.add_parser("attendance", help="考勤门户:登录、状态、读取签到记录(要装 [attendance])")
979
1012
  .add_subparsers(dest="acmd", required=True))
@@ -0,0 +1,32 @@
1
+ """Google 课程文件的独立授权命令。"""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ from . import google_export as G
9
+
10
+
11
+ def run(args):
12
+ try:
13
+ if args.google_cmd == "login":
14
+ client = Path(args.client).expanduser() if args.client else Path(__file__).with_name("google_client.json")
15
+ result = G.login(client)
16
+ elif args.google_cmd == "logout":
17
+ result = G.logout()
18
+ else:
19
+ result = G.status()
20
+ print(json.dumps(result, ensure_ascii=False, indent=2))
21
+ return 0
22
+ except (G.GoogleError, OSError, ValueError) as e:
23
+ print(str(e) if isinstance(e, G.GoogleError) else "Google 授权文件无法读取,请检查客户端 JSON", file=sys.stderr)
24
+ return 1
25
+
26
+
27
+ def add_commands(sub):
28
+ for name, help_text in (("login", "授权 Google Drive 只读导出"), ("status", "查看授权和 scope"), ("logout", "吊销授权并删除令牌")):
29
+ parser = sub.add_parser(name, help=help_text)
30
+ parser.set_defaults(fn=run)
31
+ if name == "login":
32
+ parser.add_argument("--client", metavar="JSON", help="Google 桌面应用客户端 JSON")
@@ -27,6 +27,7 @@ from .paths import ED_FILES_DIR
27
27
 
28
28
  STORED = {".pdf", ".zip", ".png", ".jpg", ".jpeg", ".gif", ".mp4", ".gz", ".docx", ".pptx", ".xlsx"}
29
29
  STATUS = {"completed": "已完成", "attempted": "做了一部分", "unattempted": "没开始"}
30
+ CHALLENGE_REASON = "Code challenge 右侧的工作区文件只能连接 Ed 工作区才读得到(会在 Ed 上创建工作区),为保持只读没有导出;题目说明已存成 .md"
30
31
 
31
32
 
32
33
  def _lessons(conn: sqlite3.Connection, course_id: int) -> list[dict]:
@@ -65,10 +66,18 @@ def plan(conn: sqlite3.Connection, course, module: str | None = None) -> dict:
65
66
  links.append((f["title"] or f["url"], f["url"]))
66
67
  elif f["type"] in ("pdf", "attachment"):
67
68
  out["missing"].append({"lesson": l["title"], "title": f["title"], "url": url})
69
+ elif f["type"] == "challenge":
70
+ out["missing"].append({"lesson": l["title"], "title": f["title"] or "Code challenge", "url": url,
71
+ "reason": CHALLENGE_REASON})
68
72
  out["lessons"].append({"module": mname, "title": l["title"], "status": l["status"], "url": url,
69
73
  "folder": folder, "files": nfiles, "links": links})
70
74
  if module and not out["lessons"]:
71
75
  raise LookupError(f"{code} 里没有模块名包含 “{module}” 的 lesson")
76
+ assets = ED_FILES_DIR / code / "assets"
77
+ if assets.is_dir():
78
+ for path in sorted(assets.iterdir()):
79
+ if path.is_file() and not path.name.endswith((".part", ".download")) and ED_FILES_DIR.resolve() in path.resolve().parents:
80
+ out["files"].append((f"{root}/assets/{path.name}", path))
72
81
  return out
73
82
 
74
83
 
@@ -86,7 +95,8 @@ def _readme(p: dict) -> str:
86
95
  md.append(f"- [{l['title']}]({l['url']}) · {STATUS.get(l['status'], l['status'] or '')} · {extra}")
87
96
  if p["missing"]:
88
97
  md += ["", "## 没下载到的文件", "", "下面这些上次同步时没下载成功(或超过大小上限),可以到 Ed 原页面看:", ""]
89
- md += [f"- {m['lesson']} · {m['title']}:{m['url']}" for m in p["missing"]]
98
+ md += [f"- {m['lesson']} · {m['title']}:{m['url']}" + (f"({m['reason']})" if m.get("reason") else "")
99
+ for m in p["missing"]]
90
100
  return "\n".join(md) + "\n"
91
101
 
92
102
 
@@ -6,6 +6,7 @@
6
6
  from __future__ import annotations
7
7
 
8
8
  import hashlib
9
+ import re
9
10
  from concurrent.futures import ThreadPoolExecutor
10
11
 
11
12
  from . import edlib
@@ -42,13 +43,39 @@ def upsert_courses(conn, courses: list[dict]) -> None:
42
43
  conn.commit()
43
44
 
44
45
 
45
- def auto_track(conn, courses: list[dict]) -> list[dict]:
46
- """第一次用的时候,默认跟踪所有 active 的课(以后可以用 monash courses 改)。"""
47
- active = [c for c in courses if c.get("status") == "active"]
48
- for c in active:
49
- conn.execute("UPDATE courses SET tracked=1 WHERE id=?", (c["id"],))
46
+ def auto_track(conn, courses: list[dict], moodle_con=None) -> list[dict]:
47
+ """新出现的 Ed 课:课号和某门已跟踪的 Moodle 课相同就跟踪(不管 Ed 上是不是已归档)。
48
+
49
+ 看过的课记在 state 表里,以后不再改,保留用户在 monash courses 里的选择。还没有跟踪任何 Moodle 课时
50
+ (第一次 setup 还没选课,或者只用 Ed)不记,等有了再匹配;这时如果一门 Ed 课都没跟踪,
51
+ 照原来的做法先跟踪所有 active 的课。"""
52
+ from . import moodlelib
53
+ own = moodle_con is None
54
+ if own:
55
+ moodle_con = moodlelib.db_connect()
56
+ try:
57
+ codes = {r[0].upper() for r in moodle_con.execute("SELECT code FROM courses WHERE tracked=1") if r[0]}
58
+ finally:
59
+ if own:
60
+ moodle_con.close()
61
+ conn.execute("CREATE TABLE IF NOT EXISTS state (key TEXT PRIMARY KEY, value TEXT)")
62
+ picked = []
63
+ if codes:
64
+ for c in courses:
65
+ key = f"auto_track:{c['id']}"
66
+ if conn.execute("SELECT 1 FROM state WHERE key=?", (key,)).fetchone():
67
+ continue
68
+ if codes.intersection(re.split(r"[/\s]+", (c.get("code") or "").upper())):
69
+ conn.execute("UPDATE courses SET tracked=1 WHERE id=?", (c["id"],))
70
+ picked.append(c)
71
+ conn.execute("INSERT INTO state VALUES (?, '1')", (key,))
72
+ if not codes and not conn.execute("SELECT 1 FROM courses WHERE tracked=1").fetchone():
73
+ for c in courses:
74
+ if c.get("status") == "active":
75
+ conn.execute("UPDATE courses SET tracked=1 WHERE id=?", (c["id"],))
76
+ picked.append(c)
50
77
  conn.commit()
51
- return active
78
+ return picked
52
79
 
53
80
 
54
81
  def store_thread(conn, cid: int, t: dict, full: dict, stamp: str, prev=None) -> tuple[int, int]:
@@ -125,9 +152,8 @@ def sync(course_ids: list[int] | None = None, full: bool = False, workers: int =
125
152
  conn = db_connect()
126
153
  courses = client.courses()
127
154
  upsert_courses(conn, courses)
155
+ auto_track(conn, courses)
128
156
  tracked = [r["id"] for r in conn.execute("SELECT id FROM courses WHERE tracked=1")]
129
- if not tracked and not course_ids:
130
- tracked = [c["id"] for c in auto_track(conn, courses)]
131
157
  course_ids = course_ids or tracked
132
158
 
133
159
  started = now_iso()
@@ -42,9 +42,12 @@ class Plan:
42
42
  notes: list = field(default_factory=list) # 写进索引的说明
43
43
  moodle: dict | None = None
44
44
  ed: dict | None = None
45
+ statistics: dict = field(default_factory=dict)
46
+ google: dict = field(default_factory=dict)
45
47
 
46
48
  def add(self, arc: str, item) -> None:
47
- self.entries.append((arc, item))
49
+ if not any(old == arc for old, _ in self.entries):
50
+ self.entries.append((arc, item))
48
51
 
49
52
 
50
53
  def scrub_url(url: str) -> str:
@@ -96,12 +99,18 @@ def plan_moodle(p: Plan, con: sqlite3.Connection) -> None:
96
99
  stats = {"id": c["id"], "name": c["fullname"], "files": 0, "bytes": 0, "links": 0, "synced_at": c["synced_at"]}
97
100
  if not c["synced_at"]:
98
101
  p.notes.append(f"Moodle:{p.code} 还没同步过(monash sync {p.code})。")
102
+ from .google_export import exported, parse_link
99
103
  for r in con.execute("SELECT path, title, section FROM files WHERE course_id=? AND removed_at IS NULL "
100
104
  "ORDER BY path", (c["id"],)):
105
+ if r["path"].startswith("ed:"):
106
+ continue
101
107
  path = FILES_DIR / r["path"]
102
108
  rel = r["path"][len(root) + 1:] if root and r["path"].startswith(root + "/") else r["path"]
103
109
  if path.is_file() and FILES_DIR.resolve() in path.resolve().parents:
104
- p.add(f"{p.code}/Moodle/{rel}", path)
110
+ arc = f"{p.code}/Moodle/{rel}"
111
+ if any(old == arc for old, _ in p.entries):
112
+ continue
113
+ p.add(arc, path)
105
114
  stats["files"] += 1
106
115
  stats["bytes"] += path.stat().st_size
107
116
  else:
@@ -110,10 +119,20 @@ def plan_moodle(p: Plan, con: sqlite3.Connection) -> None:
110
119
  by_folder: dict[str, list] = {}
111
120
  for r in con.execute("SELECT folder, section, title, url FROM links WHERE course_id=? ORDER BY folder, section, rowid",
112
121
  (c["id"],)):
113
- by_folder.setdefault(r["folder"], []).append(r)
122
+ if parse_link(r["url"]):
123
+ p.google[r["url"]] = "成功" if exported(con, c["id"], r["url"]) else "未导出"
124
+ if not exported(con, c["id"], r["url"]):
125
+ by_folder.setdefault(r["folder"], []).append(r)
114
126
  for folder, items in by_folder.items():
115
127
  p.add(f"{p.code}/Moodle/{folder}/links.md", _links_md(folder, items))
116
128
  stats["links"] += len(items)
129
+ for r in con.execute("SELECT * FROM notes WHERE course_id=? ORDER BY kind,url", (c["id"],)):
130
+ p.missing.append({"where": (r["folder"] if (r["folder"] or "").startswith("Ed Lessons") else "Moodle/" + (r["folder"] or "")), "title": r["title"], "url": r["url"], "reason": r["reason"]})
131
+ if r["kind"] == "google_failed" and p.google.get(r["url"]) != "成功":
132
+ p.google[r["url"]] = r["reason"]
133
+ for r in con.execute("SELECT source FROM files WHERE course_id=? AND removed_at IS NULL", (c["id"],)):
134
+ if parse_link(r["source"]) and exported(con, c["id"], r["source"]):
135
+ p.google[r["source"]] = "成功"
117
136
  p.moodle = stats
118
137
 
119
138
 
@@ -149,13 +168,32 @@ def plan_ed(p: Plan, conn: sqlite3.Connection) -> None:
149
168
  p.add(f"{folder}/Links.md", "\n".join(md) + "\n")
150
169
  # 按 Ed 原页面链接对应(lesson 标题可能重名)
151
170
  quiz_by_lesson = {q["url"]: q for q in edquery.quizzes(conn, ep["course_id"])}
171
+ from .lessons import asset_paths, ensure_lessons_schema
172
+ ensure_lessons_schema(conn)
173
+ assets = asset_paths(conn)
152
174
  for l in ep["lessons"]:
153
175
  q = quiz_by_lesson.get(l["url"])
154
176
  if q:
155
- p.add(f"{base}/{l['folder'][cut:]}/Quiz.md", edquery.quiz_markdown([q]))
177
+ # Quiz.md 和正文页一样在 Ed Lessons/<模块>/<lesson>/ 下,图片在 Ed Lessons/assets/
178
+ md = edquery.quiz_markdown([q])
179
+ for url, name in assets.items():
180
+ if url in md:
181
+ md = md.replace(f"]({url})", f"](../../assets/{name})")
182
+ p.add(f"{base}/{l['folder'][cut:]}/Quiz.md", md)
156
183
  for m in ep["missing"]:
157
184
  p.missing.append({"where": f"Ed Lessons/{m['lesson']}", "title": m["title"], "url": m["url"],
158
- "reason": "上次同步没下载到(下载失败或超过大小上限)"})
185
+ "reason": m.get("reason") or "上次同步没下载到(下载失败或超过大小上限)"})
186
+ from .google_export import parse_link
187
+ for r in conn.execute("SELECT url,local_path FROM lesson_files WHERE course_id=? AND type='webpage'", (ep["course_id"],)):
188
+ if parse_link(r["url"]):
189
+ ok = r["local_path"] and (edexport.ED_FILES_DIR / r["local_path"]).is_file()
190
+ if ok or r["url"] not in p.google:
191
+ p.google[r["url"]] = "成功" if ok else "未导出"
192
+ bodies = "\n".join(item.read_text(encoding="utf-8", errors="replace") for arc,item in p.entries
193
+ if isinstance(item, Path) and arc.endswith(".md") and "/Ed Lessons/" in arc)
194
+ for r in conn.execute("SELECT url,error FROM ed_assets WHERE error IS NOT NULL"):
195
+ if r["url"] in bodies:
196
+ p.missing.append({"where": "Ed Lessons/assets", "title": "正文图片", "url": r["url"], "reason": r["error"]})
159
197
  p.ed = {"id": ep["course_id"], "lessons": len(ep["lessons"]), "files": len(ep["files"]),
160
198
  "quizzes": len(quiz_by_lesson)}
161
199
 
@@ -173,6 +211,12 @@ def _index(p: Plan) -> str:
173
211
  if p.ed:
174
212
  e = p.ed
175
213
  md.append(f"- Ed Lessons/:{e['lessons']} 节 lesson,{e['files']} 个文件,{e['quizzes']} 节有 Quiz.md(只有题目,没有答案)")
214
+ if p.statistics:
215
+ s = p.statistics
216
+ md += ["", "## 统计", "", f"- links.md / Links.md:{s['link_files']} 个",
217
+ f"- Markdown 远程图片引用:{s['remote_images']} 个",
218
+ f"- Google 链接:{s['google_total']} 个,成功导出 {s['google_success']} 个,失败 {s['google_failed']} 个"]
219
+ md += [f" - {reason}:{count} 个" for reason,count in sorted(s["google_failure_reasons"].items())]
176
220
  if p.notes:
177
221
  md += ["", "## 说明", ""] + [f"- {n}" for n in p.notes]
178
222
  if p.missing:
@@ -191,6 +235,19 @@ def plan(course: str, *, moodle_con: sqlite3.Connection | None, ed_conn: sqlite3
191
235
  plan_ed(p, ed_conn)
192
236
  if not p.entries:
193
237
  raise LookupError(f"{p.code} 没有可导出的资料。\n" + "\n".join(p.notes))
238
+ reasons = {}
239
+ for reason in p.google.values():
240
+ if reason != "成功":
241
+ reasons[reason] = reasons.get(reason, 0) + 1
242
+ remote = 0
243
+ for arc,item in p.entries:
244
+ if arc.lower().endswith(".md"):
245
+ body = item.read_text(encoding="utf-8", errors="replace") if isinstance(item, Path) else item
246
+ remote += len(re.findall(r"!\[[^\]]*\]\(https?://", body))
247
+ p.statistics = {"link_files": sum(Path(arc).name in {"links.md", "Links.md"} for arc,_ in p.entries),
248
+ "remote_images": remote, "google_total": len(p.google),
249
+ "google_success": sum(reason == "成功" for reason in p.google.values()),
250
+ "google_failed": sum(reasons.values()), "google_failure_reasons": reasons}
194
251
  p.entries.insert(0, (f"{p.code}/README.md", _index(p)))
195
252
  return p
196
253
 
@@ -224,7 +281,7 @@ def write_dir(p: Plan, dest: Path) -> dict:
224
281
 
225
282
  def _stats(p: Plan, total: int) -> dict:
226
283
  return {"course": p.code, "entries": len(p.entries), "bytes": total, "moodle": p.moodle, "ed": p.ed,
227
- "missing": len(p.missing), "notes": p.notes}
284
+ "missing": len(p.missing), "notes": p.notes, "statistics": p.statistics}
228
285
 
229
286
 
230
287
  def export(course: str, to: str | Path | None = None, *, as_dir: bool = False,