Last active
April 17, 2021 05:12
-
-
Save frgomes/6e54407930b8439a4dd67de2c4f231fa to your computer and use it in GitHub Desktop.
Python - github_clone_user - clones all repositories of type=sources
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
#!/usr/bin/env python3 | |
import json | |
import requests | |
import argparse | |
import os | |
import sys | |
from git import Repo | |
## | |
## Credits: https://github.com/anantshri/github_cloner | |
## License: GPL v3 | |
## | |
## $ pip install requests GitPython | |
## $ chmod 755 gitclone_user.py | |
## $ ./github_clone_user.py --user frgomes --type sources | |
## | |
def main(): | |
global outp | |
desc="""This program is used to clone github repositories of a user / organization""" | |
epilog="""Copyright (C) Anant Shrivastava http://anantshri.info and Contributors: Akash Shende and Viktor Ahlstrom""" | |
parser = argparse.ArgumentParser(description=desc,epilog=epilog) | |
parser.add_argument("--user",help="User name",dest='target',required=True) | |
parser.add_argument("--type",help="Repository type",dest='kind',required=False) | |
parser.add_argument("--outdir",help="Output Directory",dest='out',required=False) | |
parser.add_argument("--use-ssh",help="Use ssh instead of https.",dest='useSSH',required=False,action='store_true') | |
x=parser.parse_args() | |
target=x.target | |
output=x.out | |
kind=x.kind | |
if not output: output = os.path.curdir | |
if not kind: kind="all" | |
page = 1 | |
while True: | |
url="https://api.github.com/users/" + target + "/repos?sort=full_name&page=" + str(page) + "&per_page=100" | |
r=requests.get(url) | |
js_data=json.loads(r.content) | |
if len(js_data) == 0: | |
break | |
else: | |
## print('count: ' + str(page) + ' : ' + str(len(js_data))) | |
if "message" in js_data and "API rate limit exceeded" in js_data["message"]: | |
print("Rate limit reached") | |
break | |
else: | |
for y in js_data: | |
if x.useSSH: git_url=y["ssh_url"] | |
else: git_url=y["clone_url"] | |
out_name=os.path.join(output, y["name"]) | |
skip = True | |
if kind == "sources": | |
skip = not y["has_issues"] | |
else: | |
skip = False | |
##XXX elif kind == "private": | |
##XXX skip = not y["private"] | |
##XXX print(y["private"], git_url) ## it's not possible to filter by this field | |
if not skip: | |
if os.path.isdir(out_name): | |
print(git_url + ": Directory already existing - let me pull the fresh updates for you") | |
repo=Repo(out_name); | |
repo.remotes.origin.pull() | |
else: | |
print(git_url) | |
Repo.clone_from(git_url, out_name) | |
page += 1 | |
if __name__ == "__main__": | |
main() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment