github-folder-downloader 0.1.2__py3-none-any.whl

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.
File without changes
@@ -0,0 +1,125 @@
1
+ import argparse
2
+ import os
3
+ import sys
4
+ import requests
5
+ from urllib.parse import urlparse
6
+
7
+ API_BASE = "https://api.github.com"
8
+ # Timeout for all HTTP requests (connect, read) in seconds.
9
+ REQUEST_TIMEOUT = (10, 60)
10
+
11
+ def parse_repo_url(repo_url):
12
+ """
13
+ Parses a GitHub repository URL that may include branch and subfolder information.
14
+ Expected formats:
15
+ - https://github.com/owner/repo
16
+ - https://github.com/owner/repo/tree/branch
17
+ - https://github.com/owner/repo/tree/branch/subpath
18
+ Returns a tuple (owner, repo, branch, subpath) where branch defaults to "main" and subpath defaults to "" if not provided.
19
+ """
20
+ parsed = urlparse(repo_url)
21
+ path_parts = parsed.path.strip("/").split("/")
22
+ if len(path_parts) < 2:
23
+ sys.exit("Invalid repository URL. Expected format: https://github.com/owner/repo")
24
+ owner, repo = path_parts[0], path_parts[1]
25
+ if repo.endswith(".git"):
26
+ repo = repo[:-4]
27
+ branch = "main"
28
+ subpath = ""
29
+ # If the URL contains 'tree', extract branch and subfolder from it.
30
+ if len(path_parts) >= 4 and path_parts[2] == "tree":
31
+ branch = path_parts[3]
32
+ if len(path_parts) > 4:
33
+ subpath = "/".join(path_parts[4:])
34
+ return owner, repo, branch, subpath
35
+
36
+ def get_repo_contents(owner, repo, path="", branch="main"):
37
+ """
38
+ Recursively retrieves repository contents using the GitHub API.
39
+ """
40
+ url = f"{API_BASE}/repos/{owner}/{repo}/contents/{path}"
41
+ params = {"ref": branch}
42
+ response = requests.get(url, params=params, timeout=REQUEST_TIMEOUT)
43
+ if response.status_code != 200:
44
+ sys.exit(f"Error fetching contents from GitHub API: {response.status_code} {response.text}")
45
+ return response.json()
46
+
47
+ def is_within_directory(base_dir, target_path):
48
+ """
49
+ Returns True if target_path is inside base_dir after resolving
50
+ symlinks and '..' components.
51
+ """
52
+ base = os.path.realpath(base_dir)
53
+ target = os.path.realpath(target_path)
54
+ return target == base or target.startswith(base + os.sep)
55
+
56
+ def download_file(raw_url, local_path):
57
+ """
58
+ Downloads a single file from the given raw URL to local_path.
59
+ """
60
+ if not raw_url or urlparse(raw_url).scheme != "https":
61
+ print(f"Skipping file with unsafe or missing download URL: {raw_url}", file=sys.stderr)
62
+ return
63
+ response = requests.get(raw_url, timeout=REQUEST_TIMEOUT)
64
+ if response.status_code != 200:
65
+ print(f"Failed to download file: {raw_url}", file=sys.stderr)
66
+ return
67
+ os.makedirs(os.path.dirname(local_path), exist_ok=True)
68
+ with open(local_path, "wb") as f:
69
+ f.write(response.content)
70
+ print(f"Downloaded: {local_path}")
71
+
72
+ def process_contents(contents, owner, repo, output_dir, branch):
73
+ """
74
+ Processes the contents returned from get_repo_contents.
75
+ """
76
+ for item in contents:
77
+ item_type = item.get("type")
78
+ item_path = item.get("path")
79
+ if item_type == "dir":
80
+ # Recursively process directory
81
+ sub_contents = get_repo_contents(owner, repo, item_path, branch)
82
+ process_contents(sub_contents, owner, repo, output_dir, branch)
83
+ elif item_type == "file":
84
+ # Get the raw file URL.
85
+ raw_url = item.get("download_url")
86
+ local_file_path = os.path.join(output_dir, item_path)
87
+ if not is_within_directory(output_dir, local_file_path):
88
+ # Reject path traversal attempts (e.g. paths containing "..").
89
+ print(f"Skipping file outside output directory: {item_path}", file=sys.stderr)
90
+ continue
91
+ download_file(raw_url, local_file_path)
92
+ else:
93
+ print(f"Skipping unknown type: {item.get('type')} at {item_path}")
94
+
95
+ def main():
96
+ parser = argparse.ArgumentParser(
97
+ description="Download folders from GitHub repositories without zipping."
98
+ )
99
+ parser.add_argument(
100
+ "repo_url",
101
+ help="The GitHub repository URL (e.g., https://github.com/owner/repo)"
102
+ )
103
+ parser.add_argument(
104
+ "output_dir",
105
+ help="The output directory to save the downloaded files."
106
+ )
107
+ parser.add_argument(
108
+ "--branch", default="main",
109
+ help="The branch to download from (default: main)."
110
+ )
111
+ parser.add_argument(
112
+ "--subpath", default="",
113
+ help="The repository sub-path to download (default: root)."
114
+ )
115
+ args = parser.parse_args()
116
+
117
+ owner, repo, branch, subpath = parse_repo_url(args.repo_url)
118
+ print(f"Processing repository '{owner}/{repo}' on branch '{branch}' with subfolder '{subpath}'...")
119
+
120
+ contents = get_repo_contents(owner, repo, path=subpath, branch=branch)
121
+ process_contents(contents, owner, repo, args.output_dir, branch)
122
+ print("Download complete.")
123
+
124
+ if __name__ == "__main__":
125
+ main()
@@ -0,0 +1,72 @@
1
+ Metadata-Version: 2.4
2
+ Name: github-folder-downloader
3
+ Version: 0.1.2
4
+ Summary: A command line tool to download folders from GitHub repositories without zipping.
5
+ Home-page: https://github.com/HiroshiOkada/github-folder-downloader
6
+ Author: Hiroshi Okada
7
+ Author-email: okadahiroshi@miobox.jp
8
+ Maintainer: toycode
9
+ Maintainer-email: okadahiroshi@miobox.jp
10
+ Project-URL: PyPI, https://pypi.org/user/toycode
11
+ Project-URL: Source, https://github.com/HiroshiOkada/github-folder-downloader
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Requires-Python: >=3.8
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: requests>=2.32.4
19
+ Dynamic: author
20
+ Dynamic: author-email
21
+ Dynamic: classifier
22
+ Dynamic: description
23
+ Dynamic: description-content-type
24
+ Dynamic: home-page
25
+ Dynamic: license-file
26
+ Dynamic: maintainer
27
+ Dynamic: maintainer-email
28
+ Dynamic: project-url
29
+ Dynamic: requires-dist
30
+ Dynamic: requires-python
31
+ Dynamic: summary
32
+
33
+ # GitHub Folder Downloader
34
+
35
+ GitHub Folder Downloader is a command line tool to download folders from GitHub repositories without zipping. It supports specifying branch and subpath.
36
+
37
+ ## Features
38
+
39
+ - Download folders from GitHub repositories.
40
+ - Specify branch and subpath.
41
+ - Command line interface for ease of use.
42
+
43
+ ## Installation
44
+
45
+ You can install the package from PyPI using pip:
46
+
47
+ pip install github-folder-downloader
48
+
49
+ ## Usage
50
+
51
+ Basic usage:
52
+
53
+ github-folder-downloader <repo_url> <output_dir>
54
+
55
+ Optional arguments:
56
+
57
+ --branch BRANCH The branch to download from (default: main)
58
+ --subpath SUBPATH The repository sub-path to download (default: root)
59
+
60
+ For more information, use:
61
+
62
+ github-folder-downloader --help
63
+
64
+ ## Maintainer
65
+
66
+ - PyPi Username: toycode
67
+ - Full Name: Hiroshi Okada
68
+ - Public Email: okadahiroshi@miobox.jp
69
+
70
+ ## License
71
+
72
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,8 @@
1
+ github_folder_downloader/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ github_folder_downloader/cli.py,sha256=JzKYKf41Se11FU1Cb1ZU-cZCZWduB8YAvgiudoS2V_s,4824
3
+ github_folder_downloader-0.1.2.dist-info/licenses/LICENSE,sha256=8lYKL-L4VaQJqtDUFdEWwNil0nA8T616M6oHn5q0tEk,1063
4
+ github_folder_downloader-0.1.2.dist-info/METADATA,sha256=pRCLN2fUcPl-SszQ5sDGGwwOe2uVLrIfA1apfMlb9-s,1968
5
+ github_folder_downloader-0.1.2.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ github_folder_downloader-0.1.2.dist-info/entry_points.txt,sha256=5ATmijb7h-bYf98AOplcsJ3r8cGRZa5EyTsmCnS__Tg,79
7
+ github_folder_downloader-0.1.2.dist-info/top_level.txt,sha256=d6tFojjfhcRVt0GIgzur18JdwoSn_NRVjboIx8y2Bcc,25
8
+ github_folder_downloader-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ github-folder-downloader = github_folder_downloader.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hiroshi
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ github_folder_downloader