Python Forum
Git clone all of a Github user's public repositories (download all repositories)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Git clone all of a Github user's public repositories (download all repositories)
#1
A simple script to download a user's Github repositories... only their own though... not forked ones



#! /usr/bin/python3.7
# rootVIII
# Download/clone all of a user's public repositories
# Pass the Github user's username with the -u option
# Usage: python git_clones.py -u <github username>
# Example: python git_clones.py -u rootVIII
#
from argparse import ArgumentParser
from sys import exit
from re import findall
from urllib.request import urlopen
from subprocess import call


class GitClones:
    def __init__(self, user):
        self.url = "https://github.com/%s?tab=repositories" % user
        self.git_clone = "git clone https://github.com/%s/" % user
        self.git_clone += "%s.git"
        self.user = user

    def get_repo_data(self):
        try:
            r = urlopen(self.url)
        except Exception:
            print("Unable to make request to %s's Github page" % self.user)
            exit(1)
        else:
            pattern = r"repository_nwo:%s/(.*)," % self.user
            for line in findall(pattern, r.read().decode('utf-8')):
                yield line.split(',')[0]

    def get_repositories(self):
        return set([repo for repo in self.get_repo_data()])

    def download(self, git_repos):
        for git in git_repos:
            cmd = self.git_clone % git
            try:
                call(cmd.split())
            except Exception as e:
                print(e)
                print('unable to download:%s\n ' % git)


if __name__ == "__main__":
    message = 'Usage: python git_clones.py -u <github username>'
    h = 'Github Username'
    parser = ArgumentParser(description=message)
    parser.add_argument('-u', '--user', required=True, help=h)
    d = parser.parse_args()
    clones = GitClones(d.user)
    repositories = clones.get_repositories()
    clones.download(repositories)
Reply


Messages In This Thread
Git clone all of a Github user's public repositories (download all repositories) - by rootVIII - Jun-25-2019, 12:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pong clone with classes OhNoSegFaultAgain 1 3,896 May-11-2019, 07:44 PM
Last Post: keames

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020