Python Forum
Git clone all of a Github user's public repositories (download all repositories) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Git clone all of a Github user's public repositories (download all repositories) (/thread-19336.html)

Pages: 1 2 3 4


Git clone all of a Github user's public repositories (download all repositories) - rootVIII - Jun-25-2019

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)



RE: Git clone all of a Github user's public repositories (download all repositories) - metulburr - Jun-25-2019

Im just posting to stop the unread notifications every time you modify the OP code


RE: Git clone all of a Github user's public repositories (download all repositories) - Skaperen - Jul-03-2019

Edited 11 times ?? someone posted buggy code to begin with?


RE: Git clone all of a Github user's public repositories (download all repositories) - rootVIII - Jul-04-2019

No not buggy... just kept realizing that certain lines could be combined/shortened (after the fact)


RE: Git clone all of a Github user's public repositories (download all repositories) - Axel_Erfurt - Jul-04-2019

The script does nothing on Linux Mint 19 (python 3.6.8)


RE: Git clone all of a Github user's public repositories (download all repositories) - rootVIII - Jul-04-2019

Which user's repositories are you trying to clone? It will only clone the repositories actually made by the user.. not forked ones. Linux Mint should have nothing to do with it... I run this on Ubuntu, CentOS, and Kali with no issues.

Actually nothing in the code is OS specific whatsoever... It should run even on Windows to... haven't tried it but system should not be a factor

Screenshots on my Kali Desktop


RE: Git clone all of a Github user's public repositories (download all repositories) - Skaperen - Jul-04-2019

the code looked OK to me. i was thinking of using on AWS' repositories there.


RE: Git clone all of a Github user's public repositories (download all repositories) - Axel_Erfurt - Jul-04-2019

(Jul-04-2019, 04:19 PM)rootVIII Wrote: Which user's repositories are you trying to clone?

I tried it with my own.(and others)


RE: Git clone all of a Github user's public repositories (download all repositories) - rootVIII - Jul-04-2019

Hmmmm yeah just tried to get all your repos and nothing... hold on looking into it now. I suspect the regex

Yup it's the regex... should have a fix in a little bit... sorry for that

Okay just got all your repos... had to fix that regex. Also this one should run with Python 2 or 3...

# 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, version_info
from re import findall
from subprocess import call
try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import Request, urlopen


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 http_get(self):
        if version_info[0] != 2:
            req = urlopen(self.url)
            return req.read().decode('utf-8')
        req = Request(self.url)
        request = urlopen(req)
        return request.read()

    def get_repo_data(self):
        try:
            response = self.http_get()
        except Exception:
            print("Unable to make request to %s's Github page" % self.user)
            exit(1)
        else:
            pattern = r"<a\s?href\W+%s/(.*)\"\s+" % self.user
            for line in findall(pattern, response):
                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)



RE: Git clone all of a Github user's public repositories (download all repositories) - Axel_Erfurt - Jul-04-2019

No problem.

On github I use Axel-Erfurt as username