Download a Github user's Public Repositories


/ Published in: Python
Save to your folder(s)

Clone any Github user's collection of public repositories


Copy this code and paste it in your HTML
  1. # rootVIII
  2. # Download/clone all of a user's public source repositories
  3. # Pass the Github user's username with the -u option
  4. # Usage: python git_clones.py -u <github username>
  5. # Example: python git_clones.py -u rootVIII
  6. #
  7. from argparse import ArgumentParser
  8. from sys import exit, version_info
  9. from re import findall
  10. from subprocess import call
  11. try:
  12. from urllib.request import urlopen
  13. except ImportError:
  14. from urllib2 import Request, urlopen
  15.  
  16.  
  17. class GitClones:
  18. def __init__(self, user):
  19. self.url = "https://github.com/%s" % user
  20. self.url += "?&tab=repositories&q=&type=source"
  21. self.git_clone = "git clone https://github.com/%s/%%s.git" % user
  22. self.user = user
  23.  
  24. def http_get(self):
  25. if version_info[0] != 2:
  26. req = urlopen(self.url)
  27. return req.read().decode('utf-8')
  28. req = Request(self.url)
  29. request = urlopen(req)
  30. return request.read()
  31.  
  32. def get_repo_data(self):
  33. try:
  34. response = self.http_get()
  35. except Exception:
  36. print("Unable to make request to %s's Github page" % self.user)
  37. exit(1)
  38. else:
  39. pattern = r"<a\s?href\W+%s/(.*)\"\s+" % self.user
  40. for line in findall(pattern, response):
  41. yield line.split('\"')[0]
  42.  
  43. def get_repositories(self):
  44. return [repo for repo in self.get_repo_data()]
  45.  
  46. def download(self, git_repos):
  47. for git in git_repos:
  48. cmd = self.git_clone % git
  49. try:
  50. call(cmd.split())
  51. except Exception as e:
  52. print(e)
  53. print("unable to download:%s\n" % git)
  54.  
  55.  
  56. if __name__ == "__main__":
  57. message = 'Usage: python git_clones.py -u <github username>'
  58. h = 'Github Username'
  59. parser = ArgumentParser(description=message)
  60. parser.add_argument('-u', '--user', required=True, help=h)
  61. d = parser.parse_args()
  62. clones = GitClones(d.user)
  63. repositories = clones.get_repositories()
  64. clones.download(repositories)

URL: https://github.com/rootVIII/git_clones

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.