-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGitHubGet.py
57 lines (48 loc) · 1.86 KB
/
GitHubGet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# download an entire github repo.
#
# either copy the url to clipboard, and run script, or run following bookmarklet.
# will unzip to repo-branch (so be careful if downloading same branch name from multiple users)
#
## javascript:(function()%7Bif(document.location.href.indexOf('http')===0)document.location.href='pythonista://GitHubGet?action=run&argv='+document.location.href;%7D)();
import urllib,zipfile,sys, clipboard, functools, re, os, tempfile
def extract_git_id(git):
print git
m = re.match((r'^http(s?)://([\w-]*\.)?github\.com/(?P<user>[\w-]+)/(?P<repo>[\w-]*)'
'((/tree|/blob)/(?P<branch>[\w-]*))?'), git)
# print m.groupdict()
return m
def git_download_from_args(args):
if len(args) == 2:
url = args[1]
else:
url = clipboard.get()
git_download(url)
def dlProgress(filename, count, blockSize, totalSize):
if count*blockSize > totalSize:
percent=100
else:
percent = max(min(int(count*blockSize*100/totalSize),100),0)
sys.stdout.write("\r" + filename + "...%d%%" % percent)
sys.stdout.flush()
def git_download(url):
base='https://codeload.github.com'
archive='zip'
m=extract_git_id(url)
if m:
g=m.groupdict()
if not g['branch']:
g['branch']='master'
u= '/'.join((base,g['user'],g['repo'],archive, g['branch']))
#print u
try:
with tempfile.NamedTemporaryFile(mode='w+b',suffix='.zip') as f:
urllib.urlretrieve(u,f.name,reporthook=functools.partial(dlProgress,u))
z=zipfile.ZipFile(f)
z.extractall()
print z.namelist()
except:
print('git url did not return zip file')
else:
print('could not determine repo url from clipboard or argv')
if __name__=='__main__':
git_download_from_args(sys.argv)