Skip to content

Instantly share code, notes, and snippets.

@dannyvoid
Created June 3, 2018 06:45
Show Gist options
  • Save dannyvoid/81c56d051a689bbfd148950102ff7708 to your computer and use it in GitHub Desktop.
Save dannyvoid/81c56d051a689bbfd148950102ff7708 to your computer and use it in GitHub Desktop.
Function to download latest asset from a Github Repo
import requests
import urllib.request
import os
def update_from_github(github_username, github_repo, github_asset, local_directory):
github_repo_url = 'https://github.com/{}/{}/releases/latest'.format(github_username, github_repo)
github_asset_url = requests.get(github_repo_url).url.replace('tag', 'download') + '/' + github_asset
destination = os.path.join(local_directory, github_asset)
try:
print('Deleting current {}'.format(github_asset))
os.remove(destination)
print('Done!')
except OSError:
print('{} does not exist!'.format(github_asset))
pass
try:
print('Downloading latest {}'.format(github_asset))
urllib.request.urlretrieve(github_asset_url, destination)
print('Done!')
except OSError:
print('Could not download {} for some reason!'.format(github_asset))
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment