Skip to content

Instantly share code, notes, and snippets.

@Bhupesh-V
Last active March 11, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bhupesh-V/cb4e48138e2dc93fed0bda97cd51ad2b to your computer and use it in GitHub Desktop.
Save Bhupesh-V/cb4e48138e2dc93fed0bda97cd51ad2b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import re
import urllib.request
from pkg_resources import parse_version
HEADERS = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"cache-control": "no-cache",
}
VERSION = "0.0.5"
SCRIPT_UPSTREAM = (
"https://gist.github.com/Bhupesh-V/cb4e48138e2dc93fed0bda97cd51ad2b/raw"
)
SCRIPT_LOCATION = os.path.dirname(os.path.realpath(__file__))
SCRIPT_NAME = os.path.basename(__file__)
def get(url):
req = urllib.request.Request(url, headers=HEADERS)
try:
with urllib.request.urlopen(req) as response:
res = response.read().decode("ascii")
except urllib.error.URLError as e:
print(e.reason)
exit()
return res
def get_version(v):
version = str(re.search("(?<=VERSION\s=\s).*", v).group(0))
return version[1 : len(version) - 1]
def update():
print("Checking for updates ...", end="\r")
NEW_SCRIPT = get(SCRIPT_UPSTREAM)
NEW_SCRIPT_VERSION = get_version(NEW_SCRIPT)
if parse_version(NEW_SCRIPT_VERSION) > parse_version(VERSION):
print("Found New Version:", NEW_SCRIPT_VERSION)
old_file = os.path.join(SCRIPT_LOCATION, SCRIPT_NAME)
with open(f"""{old_file}""", "w") as new_script:
new_script.write(NEW_SCRIPT)
print("Script Successfully Updated")
else:
print("Current Version", VERSION, "is latest")
if __name__ == "__main__":
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment