Skip to content

Instantly share code, notes, and snippets.

@007revad
Forked from jarrodnorwell/gitpybackup.py
Created October 7, 2023 09:13
Show Gist options
  • Save 007revad/4462c2e84479f68f1766a656b3dd0686 to your computer and use it in GitHub Desktop.
Save 007revad/4462c2e84479f68f1766a656b3dd0686 to your computer and use it in GitHub Desktop.
Backup `repo_count` of public repositories for username`
#
# main.py
# GitPyBackup
#
# Created by Jarrod Norwell on 07/10/2023.
#
"""
Because the user specified only has 40~ repositories I've made this as basic as possible,
just change the username and repo_count to get started, it'll download all repos into the cwd
Required: pip install gitpython requests
"""
from git import Repo
from requests import get
from os import getcwd, sep
def all_repositories_for_user(username: str, repo_count: int = 30) -> list[tuple[str, str]]:
def is_private(repo: dict) -> bool: # used to remove private repos
return repo["private"] == False
response = get(f"https://api.github.com/users/{username}/repos?per_page={repo_count}", headers={
"Accept" : "application/vnd.github+json",
"X-GitHub-Api-Version" : "2022-11-28"
}).json()
repos = []
[repos.append((repo["clone_url"], repo["name"])) for repo in filter(is_private, response)]
return repos
if __name__ == "__main__":
repos = all_repositories_for_user(username="official-antique", repo_count=5) # change these
[Repo.clone_from(url=repo[0], to_path=getcwd() + sep + repo[1]) for repo in repos]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment