Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Created by: jsbain
# Created on: Aug 2014
# URL :
# 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)();
# Altered by: Mr. Coxall
# Altered on: Aug 2016
# Combined with other code, so that it will only work from sharesheet
# and gives the user different and better feedback
# Also places the files in Downloaded from Github directory
# This works not only with Github repos but also Gists.
import requests
import appex, console, time, os
import urllib, zipfile, sys, functools, re, os, tempfile
#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]
url = clipboard.get()
def git_download_from_sharesheet():
if appex.is_running_extension():
url = appex.get_url()
else: # Error handling...
* In Safari browser, navigate to a GitHub repo or Gist of interest.
* Tap 'Open in...' icon in top right of Safai window.
* Tap 'Run Pythonista Script'.
* Pick this script and tap the run button.
* When you return to Pythonista the files should be in '~/Documents/Downloaded from Github/'.''')
def dlProgress(filename, count, blockSize, totalSize):
if count*blockSize > totalSize:
percent = max(min(int(count*blockSize*100/totalSize),100),0)
sys.stdout.write("\r" + filename + "...%d%%" % percent)
def git_download(url):
base = ''
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
#console.hud_alert('Downloading Github repo ...' + u)
console.hud_alert('Starting, please wait.')
with tempfile.NamedTemporaryFile(mode='w+b',suffix='.zip') as f:
console.hud_alert('Downloading the zip.')
z = zipfile.ZipFile(f)
githubpath = os.path.expanduser('~/Documents/Downloaded from Github/')
if not os.path.exists(githubpath):
z.extractall(path = githubpath)
console.hud_alert('Extracting zip.')
print z.namelist()
print('git url did not return zip file')
console.hud_alert('Files saved in "Downloaded from Github" directory.')
console.hud_alert('All done.')
print('could not determine repo url from clipboard or argv')
if __name__=='__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment