Skip to content

Instantly share code, notes, and snippets.

@LasseSkogland
Created March 29, 2017 21:18
Show Gist options
  • Select an option

  • Save LasseSkogland/6ca07785fc9a03947176e077364ca69b to your computer and use it in GitHub Desktop.

Select an option

Save LasseSkogland/6ca07785fc9a03947176e077364ca69b to your computer and use it in GitHub Desktop.
Getting latest releases from github repositories
import urllib2
import json
import sys
def getJson(url):
return json.loads(urllib2.urlopen(url).read())
def download(author_repo):
print 'Gathering information'
data = getJson('https://api.github.com/repos/' + author_repo + '/releases/latest')
url = data['assets'][0]['browser_download_url']
file = data['assets'][0]['name']
print 'Downloading', file, 'from', author_repo
res = urllib2.urlopen(url)
size = res.headers['content-length']
with open(file, 'wb') as f:
fdata = res.read(1048576)
f.write(fdata)
while len(fdata) >= 1048576:
sys.stdout.write('.')
fdata = res.read(1048576)
f.write(fdata)
print 'Done!'
def search(query, sort='stars', order='desc'):
return getJson('https://api.github.com/search/repositories?q=' + query + '%20in:name&sort=' + sort + '&order=' + order)
def main(args):
query = args[0]
if query.find('/') > -1:
download(query)
else:
print 'Searching for', query
searchdata = search(query)
count = int(searchdata['total_count'])
if count == 0:
print 'Could not find repository'
return
elif count == 1:
print 'Found', query + ', downloading'
download(searchdata['full_name'])
else:
print 'Found', count, 'repositories: (showing:', len(searchdata['items']), ')'
for item in searchdata['items']:
if item['description'] != None:
print ' ', item['full_name'], '-', item['description'].encode('ascii', 'ignore')
else:
print ' ', item['full_name'], ' - No Description'
print 'Try again by using "github-release AUTHOR/' + query + '"'
return
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Usages:'
print ' github-release QUERY - search for repository'
print ' github-release AUTHOR/REPOSITORY - download this repository'
else:
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment