You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
github_downloader/github_download.py

52 lines
1.5 KiB

#!/home/user/bin/github_download/.direnv/python-venv-3.7.4/bin/python
"""
This small program uses requests to find all assets in the *latest* release of
a github repo and lets the user select which to download and then downloads
them to the current directory.
"""
import sys, traceback
import requests
import argparse
import json
from pick import pick
def program(args):
response = requests.get('https://api.github.com/repos/{0}/{1}/releases/latest'
.format(args.github_user, args.github_repo))
response_json = json.loads(response.text)
tag = response_json.get("tag_name")
assets = response_json.get("assets")
ass_dict = {}
for ass in assets:
ass_dict[ass.get("name")] = ass.get("browser_download_url")
title = 'Choose which files to download (press SPACE to mark, ENTER to continue): '
selected = pick(list(ass_dict.keys()), title, multi_select=True, min_selection_count=1)
for file_name in selected:
name = file_name[0]
url = ass_dict.get(name)
print("Downloading : " + url)
r = requests.get(url, allow_redirects=True)
open(name, 'wb').write(r.content)
def main():
try:
parser = argparse.ArgumentParser()
parser.add_argument("github_user")
parser.add_argument("github_repo")
args = parser.parse_args()
program(args)
except KeyboardInterrupt:
print("Shutdown requested...exiting")
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)
if __name__ == "__main__":
main()