-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
59 lines (43 loc) · 1.9 KB
/
backup.py
File metadata and controls
59 lines (43 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# Copyright 2025 Theodore Podewil
# GPL-3.0-or-later
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
# system imports
import sys
import json
import subprocess
from datetime import date
# First Party Imports
# Third Party Imports
import requests
def run(*args):
"""Main Entry Point"""
if len(args) != 2:
print(
"Program takes two arguments.\nUsage:\n./backup <username> <#ofreposlimit>"
)
sys.exit()
repos_url = f"https://api.github.com/users/{args[0]}/repos"
repo_limit = int(args[1])
try:
params = {"per_page": repo_limit}
response = requests.get(repos_url, timeout=10, params=params)
except requests.RequestException:
sys.exit("Bad Request.")
file_path = f"{date.today()}.json"
converted_response = response.json()
with open(file_path, "w", encoding="utf-8") as json_file:
json.dump(converted_response, json_file, indent=4)
for repo in converted_response:
try:
subprocess.run(
["git", "clone", repo["html_url"], repo["name"], "--mirror"], check=True
)
except subprocess.CalledProcessError as e:
print("Error cloning repository: %s" % (e))
if __name__ == "__main__":
run(*sys.argv[1:])
else:
raise ImportError("Run this file directly, don't import it.")