Created
March 25, 2025 22:02
-
-
Save amoilanen/a803541932e4751328ad157ca419daae to your computer and use it in GitHub Desktop.
Python script to clone all the Github repos of a given user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import requests | |
import subprocess | |
import os | |
def get_repos(username, api_key): | |
url = f"https://api.github.com/users/{username}/repos" | |
headers = {"Authorization": f"token {api_key}"} | |
repos = [] | |
while url: | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
print(f"Error fetching repositories: {response.json()}") | |
return [] | |
data = response.json() | |
repos.extend([repo['clone_url'] for repo in data]) | |
url = response.links.get('next', {}).get('url') # Handle pagination | |
return repos | |
def clone_repos(repos, output_dir): | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
for repo in repos: | |
repo_name = repo.split('/')[-1].replace('.git', '') | |
repo_path = os.path.join(output_dir, repo_name) | |
if os.path.exists(repo_path): | |
print(f"Skipping {repo_name}, already cloned.") | |
else: | |
print(f"Cloning {repo_name}...") | |
subprocess.run(["git", "clone", repo, repo_path]) | |
def main(): | |
parser = argparse.ArgumentParser(description="Clone all repositories of a GitHub user.") | |
parser.add_argument("--username", help="GitHub username") | |
parser.add_argument("--api-key", help="GitHub API token") | |
parser.add_argument("--output", default="./repos", help="Directory to clone repositories into") | |
args = parser.parse_args() | |
repos = get_repos(args.username, args.api_key) | |
if repos: | |
clone_repos(repos, args.output) | |
else: | |
print("No repositories found or failed to fetch repositories.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment