Skip to content

Commit 7e0fb35

Browse files
author
Philipp Felsecker
committed
initial commit
1 parent f2700cd commit 7e0fb35

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is just a random collection of scripts I once wrote to help me in my daily life.

gh-get-latest.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
3+
import requests as req
4+
import sys
5+
from simple_term_menu import TerminalMenu
6+
import shutil
7+
import urllib.request
8+
9+
def eprint(*args, **kwargs):
10+
print(*args, file=sys.stderr, **kwargs)
11+
12+
def usage():
13+
usage_str = f"""
14+
========== Usage: ==========
15+
{sys.argv[0]} <github_repo_url>
16+
example:
17+
{sys.argv[0]} https://github.com/derailed/k9s
18+
"""
19+
20+
eprint(usage_str)
21+
sys.exit(1)
22+
23+
24+
def select_asset(owner='', repo=''):
25+
download_url = ""
26+
file_name = ""
27+
assets = {}
28+
29+
if owner == "" or repo == "":
30+
eprint(f"We need owner and repo to grab the latest release. Got owner={owner} and repo={repo}")
31+
return download_url
32+
33+
assets = grab_latest_from_gh_api(owner, repo)
34+
35+
asset_names = list(assets.keys())
36+
if len(asset_names) > 0:
37+
tmenu = TerminalMenu(asset_names, title="Select Asset to Dwonload", search_case_sensitive=False, show_search_hint=True, clear_screen=True)
38+
selection = tmenu.show()
39+
file_name = asset_names[selection]
40+
download_url = assets[file_name]
41+
42+
return file_name, download_url
43+
44+
45+
def grab_latest_from_gh_api(owner='', repo=''):
46+
github_api_url_base = "https://api.github.com/repos"
47+
github_api_url_latest_path = "releases/latest"
48+
assets = {}
49+
50+
if owner == "" or repo == "":
51+
eprint(f"We need owner and repo to grab the latest release. Got owner={owner} and repo={repo}")
52+
53+
latest_release_url = f"{github_api_url_base}/{owner}/{repo}/{github_api_url_latest_path}"
54+
55+
res = req.get(latest_release_url)
56+
if res.status_code != 200:
57+
eprint(f"Error grabbing latest version info from Github. Got {res.status_code}")
58+
return ""
59+
60+
asset_list = res.json()
61+
for asset in asset_list["assets"]:
62+
if asset['name'] and asset['name'] != "" and asset['browser_download_url'] and asset['browser_download_url'] != "":
63+
assets[asset['name']] = asset['browser_download_url']
64+
65+
return assets
66+
67+
68+
def download_latest_from_gh(file_name='', download_url=''):
69+
if file_name == "" or download_url == "":
70+
eprint(f"Need filename (got: {file_name}) and download_url (got: {download_url})")
71+
sys.exit(1)
72+
73+
print(f"Downloading {download_url}")
74+
with urllib.request.urlopen(download_url) as res, open(file_name, 'wb') as out_file:
75+
shutil.copyfileobj(res, out_file)
76+
77+
def split_gh_url(repo_url=''):
78+
split_url = repo_url.split("/")
79+
repo = split_url[-1]
80+
owner = split_url[-2]
81+
if repo == "/":
82+
repo = split_url[-2]
83+
owner = split_url[-3]
84+
85+
return owner, repo
86+
87+
def main():
88+
89+
if len(sys.argv) <= 1:
90+
usage()
91+
92+
repo_url = sys.argv[1]
93+
94+
owner, repo = split_gh_url(repo_url)
95+
96+
file_name, download_url = select_asset(owner, repo)
97+
if file_name =="" or download_url == "":
98+
eprint(f"Couldn't find a suitable download_url for latest release from Github.")
99+
sys.exit(1)
100+
101+
download_latest_from_gh(file_name, download_url)
102+
103+
104+
if __name__ == "__main__":
105+
main()

0 commit comments

Comments
 (0)