Second wave of initial code changes.
This commit is contained in:
68
util.py
68
util.py
@ -1,5 +1,9 @@
|
||||
#!/usr/bin/python3
|
||||
# coding=utf-8
|
||||
#
|
||||
# Bragi - A Mumble music bot
|
||||
# Forked from botamusique by azlux (https://github.com/azlux/botamusque)
|
||||
#
|
||||
|
||||
import hashlib
|
||||
import html
|
||||
@ -102,64 +106,8 @@ def get_user_ban():
|
||||
return res
|
||||
|
||||
|
||||
def new_release_version(target):
|
||||
if target == "testing":
|
||||
r = requests.get("https://packages.azlux.fr/botamusique/testing-version")
|
||||
else:
|
||||
r = requests.get("https://packages.azlux.fr/botamusique/version")
|
||||
v = r.text
|
||||
return v.rstrip()
|
||||
|
||||
|
||||
def fetch_changelog():
|
||||
r = requests.get("https://packages.azlux.fr/botamusique/changelog")
|
||||
c = r.text
|
||||
return c
|
||||
|
||||
|
||||
def check_update(current_version):
|
||||
global log
|
||||
log.debug("update: checking for updates...")
|
||||
new_version = new_release_version(var.config.get('bot', 'target_version'))
|
||||
if version.parse(new_version) > version.parse(current_version):
|
||||
changelog = fetch_changelog()
|
||||
log.info(f"update: new version {new_version} found, current installed version {current_version}.")
|
||||
log.info(f"update: changelog: {changelog}")
|
||||
changelog = changelog.replace("\n", "<br>")
|
||||
return new_version, changelog
|
||||
else:
|
||||
log.debug("update: no new version found.")
|
||||
return None, None
|
||||
|
||||
|
||||
def update(current_version):
|
||||
global log
|
||||
|
||||
target = var.config.get('bot', 'target_version')
|
||||
new_version = new_release_version(target)
|
||||
msg = ""
|
||||
if target == "git":
|
||||
msg = "git install, I do nothing<br/>"
|
||||
|
||||
elif (target == "stable" and version.parse(new_version) > version.parse(current_version)) or \
|
||||
(target == "testing" and version.parse(new_version) != version.parse(current_version)):
|
||||
log.info('update: new version, start updating...')
|
||||
tp = sp.check_output(['/usr/bin/env', 'bash', 'update.sh', target]).decode()
|
||||
log.debug(tp)
|
||||
log.info('update: update pip libraries dependencies')
|
||||
sp.check_output([var.config.get('bot', 'pip3_path'), 'install', '--upgrade', '-r', 'requirements.txt']).decode()
|
||||
msg = "New version installed, please restart the bot.<br/>"
|
||||
|
||||
log.info(f'update: starting update {YT_PKG_NAME} via pip3')
|
||||
tp = sp.check_output([var.config.get('bot', 'pip3_path'), 'install', '--upgrade', YT_PKG_NAME]).decode()
|
||||
if f"Collecting {YT_PKG_NAME}" in tp.splitlines():
|
||||
msg += "Update done: " + tp.split('Successfully installed')[1]
|
||||
else:
|
||||
msg += YT_PKG_NAME.capitalize() + " is up-to-date"
|
||||
|
||||
reload(youtube_dl)
|
||||
msg += "<br/>" + YT_PKG_NAME.capitalize() + " reloaded"
|
||||
return msg
|
||||
|
||||
|
||||
def pipe_no_wait():
|
||||
@ -311,7 +259,7 @@ def get_url_from_input(string):
|
||||
else:
|
||||
return ""
|
||||
|
||||
match = re.search("(http|https)://(\S*)?/(\S*)", string, flags=re.IGNORECASE)
|
||||
match = re.search(r"(http|https)://(\S*)?/(\S*)", string, flags=re.IGNORECASE)
|
||||
if match:
|
||||
url = match[1].lower() + "://" + match[2].lower() + "/" + match[3]
|
||||
# https://github.com/mumble-voip/mumble/issues/4999
|
||||
@ -376,7 +324,7 @@ def get_media_duration(path):
|
||||
|
||||
|
||||
def parse_time(human):
|
||||
match = re.search("(?:(\d\d):)?(?:(\d\d):)?(\d+(?:\.\d*)?)", human, flags=re.IGNORECASE)
|
||||
match = re.search(r"(?:(\d\d):)?(?:(\d\d):)?(\d+(?:\.\d*)?)", human, flags=re.IGNORECASE)
|
||||
if match:
|
||||
if match[1] is None and match[2] is None:
|
||||
return float(match[3])
|
||||
@ -399,7 +347,7 @@ def format_time(seconds):
|
||||
def parse_file_size(human):
|
||||
units = {"B": 1, "KB": 1024, "MB": 1024 * 1024, "GB": 1024 * 1024 * 1024, "TB": 1024 * 1024 * 1024 * 1024,
|
||||
"K": 1024, "M": 1024 * 1024, "G": 1024 * 1024 * 1024, "T": 1024 * 1024 * 1024 * 1024}
|
||||
match = re.search("(\d+(?:\.\d*)?)\s*([A-Za-z]+)", human, flags=re.IGNORECASE)
|
||||
match = re.search(r"(\d+(?:\.\d*)?)\s*([A-Za-z]+)", human, flags=re.IGNORECASE)
|
||||
if match:
|
||||
num = float(match[1])
|
||||
unit = match[2].upper()
|
||||
@ -428,7 +376,7 @@ def get_supported_language():
|
||||
lang_files = os.listdir(os.path.join(root_dir, 'lang'))
|
||||
lang_list = []
|
||||
for lang_file in lang_files:
|
||||
match = re.search("([a-z]{2}_[A-Z]{2})\.json", lang_file)
|
||||
match = re.search(r"([a-z]{2}_[A-Z]{2})\.json", lang_file)
|
||||
if match:
|
||||
lang_list.append(match[1])
|
||||
|
||||
|
Reference in New Issue
Block a user