canvas-download 1.0.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.
@@ -0,0 +1,3 @@
1
+ dist/
2
+ login.json
3
+ config.json
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 suncloudsmoon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ Metadata-Version: 2.4
2
+ Name: canvas-download
3
+ Version: 1.0.0
4
+ Summary: Automatically downloads Canvas modules/files into a local directory.
5
+ Project-URL: Homepage, https://github.com/suncloudsmoon/canvas-download
6
+ Project-URL: Issues, https://github.com/suncloudsmoon/canvas-download/issues
7
+ Author-email: suncloudsmoon <farsaturn@outlook.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Keywords: canvas,canvas-grab,download
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Requires-Python: >=3.10
14
+ Requires-Dist: canvasapi>=3.3.0
15
+ Requires-Dist: tqdm>=4.67.1
16
+ Description-Content-Type: text/markdown
17
+
18
+ # canvas-download
19
+ Canvas Download automatically downloads Canvas modules/files into a local directory for offline access. A file called config.json will be created during the first run of the utility and no files will be downloaded in this run. config.json will define whether to download "files" or "modules" for a particular course. To use this command line utility, create the following files as listed below in the same directory as you want the canvas files to be downloaded to.
20
+
21
+ ## login.json
22
+ {
23
+ "API_URL": "",
24
+ "API_KEY": ""
25
+ }
@@ -0,0 +1,8 @@
1
+ # canvas-download
2
+ Canvas Download automatically downloads Canvas modules/files into a local directory for offline access. A file called config.json will be created during the first run of the utility and no files will be downloaded in this run. config.json will define whether to download "files" or "modules" for a particular course. To use this command line utility, create the following files as listed below in the same directory as you want the canvas files to be downloaded to.
3
+
4
+ ## login.json
5
+ {
6
+ "API_URL": "",
7
+ "API_KEY": ""
8
+ }
@@ -0,0 +1,31 @@
1
+ [build-system]
2
+ requires = ["hatchling >= 1.27"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ dependencies = [
7
+ "canvasapi >= 3.3.0",
8
+ "tqdm >= 4.67.1"
9
+ ]
10
+ name = "canvas-download"
11
+ version = "1.0.0"
12
+ authors = [
13
+ {name="suncloudsmoon", email="farsaturn@outlook.com"}
14
+ ]
15
+ description = "Automatically downloads Canvas modules/files into a local directory."
16
+ keywords = ["canvas", "download", "canvas-grab"]
17
+ readme="README.md"
18
+ requires-python = ">= 3.10"
19
+ classifiers = [
20
+ "Programming Language :: Python :: 3",
21
+ "Operating System :: OS Independent",
22
+ ]
23
+ license = "MIT"
24
+ license-files = ["LICEN[CS]E*"]
25
+
26
+ [project.urls]
27
+ Homepage = "https://github.com/suncloudsmoon/canvas-download"
28
+ Issues = "https://github.com/suncloudsmoon/canvas-download/issues"
29
+
30
+ [project.scripts]
31
+ canvas-download = "canvas_download:main"
@@ -0,0 +1 @@
1
+ from .canvas_download import main
@@ -0,0 +1,83 @@
1
+ """
2
+ Create a file called login.json with the following:
3
+ {
4
+ "API_URL": "",
5
+ "API_KEY": ""
6
+ }
7
+ """
8
+
9
+ import json
10
+ import re
11
+ from datetime import datetime, timezone
12
+ from pathlib import Path
13
+
14
+ from canvasapi import Canvas
15
+ from tqdm import tqdm
16
+
17
+
18
+ def get_valid_filename(filename: str):
19
+ return re.sub(r'[<>:"\|\\/\?\*]', " ", filename).strip()
20
+
21
+ def main():
22
+ contents = Path("login.json").read_text(encoding="utf-8")
23
+ login = json.loads(contents)
24
+ canvas = Canvas(login["API_URL"], login["API_KEY"])
25
+
26
+ courses = canvas.get_courses()
27
+ current_courses = [
28
+ course
29
+ for course in courses
30
+ if hasattr(course, "end_at_date")
31
+ and course.end_at_date > datetime.now(tz=timezone.utc)
32
+ ]
33
+
34
+ if not current_courses:
35
+ raise LookupError("no enrolled courses found")
36
+
37
+ config_path = Path("config.json")
38
+ names_courses = {
39
+ re.search(r"\w\S+-\S+\w", course.name).group(0).replace("-", " "): course
40
+ for course in current_courses
41
+ }
42
+ if not config_path.exists():
43
+ config = {k: "modules" for k in names_courses}
44
+ config_path.write_text(json.dumps(config, indent=4), encoding="utf-8")
45
+ print("Welcome to canvas sync...")
46
+ print("config.json has been created; change settings if needed")
47
+ else:
48
+ contents = config_path.read_text(encoding="utf-8")
49
+ config = json.loads(contents)
50
+ # config validation
51
+ if not all(name in config for name in names_courses) or not all(
52
+ val.lower() == "modules" or val.lower() == "files" for val in config.values()
53
+ ):
54
+ raise ValueError("invalid config")
55
+
56
+ for name, course in tqdm(names_courses.items(), desc="Fetching courses"):
57
+ course_path = Path(get_valid_filename(name))
58
+ user_choice = config[name].lower()
59
+ if user_choice == "modules":
60
+ for module in course.get_modules():
61
+ module_path = course_path / get_valid_filename(module.name)
62
+ module_path.mkdir(parents=True, exist_ok=True)
63
+ for item in module.get_module_items():
64
+ if item.type == "File":
65
+ file_path = module_path / get_valid_filename(item.title)
66
+ if not file_path.exists():
67
+ file = course.get_file(item.content_id)
68
+ file.download(str(file_path))
69
+ elif user_choice == "files":
70
+ for folder in course.get_folders():
71
+ full_path = Path(folder.full_name)
72
+ folder_path = (
73
+ course_path / full_path.relative_to(full_path.parts[0])
74
+ if full_path.parts[0] == "course files"
75
+ else None
76
+ )
77
+ folder_path.mkdir(parents=True, exist_ok=True)
78
+ for file in folder.get_files():
79
+ if file.locked:
80
+ continue
81
+ file_path = folder_path / get_valid_filename(file.display_name)
82
+ if not file_path.exists():
83
+ file.download(str(file_path))