From 84512ade361daf66913887d9e220dda64ce60d4b Mon Sep 17 00:00:00 2001 From: BarsTiger Date: Thu, 5 May 2022 18:39:36 +0300 Subject: [PATCH] versions and updates for apps better api requests system --- horsy.py | 28 ++++++++++++++++-------- horsygui.py | 40 ++++++++++++++++----------------- modules/change_email.py | 7 +++--- modules/change_password.py | 8 +++---- modules/download.py | 3 ++- modules/gui_manager.py | 45 +++++++++++++++++++++++++------------- modules/liker.py | 4 ++-- modules/login.py | 8 +++---- modules/manager.py | 16 ++++++++++---- modules/package_edit.py | 10 ++++----- modules/request.py | 29 ++++++++++++++++++++++++ modules/search.py | 3 ++- modules/source.py | 4 ++-- modules/updates.py | 36 ++++++++++++++++++++++++------ modules/uploader.py | 14 ++++++------ 15 files changed, 170 insertions(+), 85 deletions(-) create mode 100644 modules/request.py diff --git a/horsy.py b/horsy.py index 2d9a273..33105cf 100644 --- a/horsy.py +++ b/horsy.py @@ -2,7 +2,7 @@ import argparse import os import subprocess import sys -import requests +from modules.request import request import modules.tui as tui from modules.console import cls from modules.virustotal import add_to_cfg @@ -10,9 +10,9 @@ import modules.vars as horsy_vars # Getting the arguments parser = argparse.ArgumentParser(description='horsy - the best package manager') -parser.add_argument('option', help='options for horsy (install/i | uninstall/un | source/s | list/l | ' +parser.add_argument('option', help='options for horsy (install/i | uninstall/un | updates/u | source/s | list/l | ' 'upload | search | info | like | dislike)', - choices=['install', 'i', 'uninstall', 'un', 'source', 's', 'list', 'l', 'upload', + choices=['install', 'i', 'uninstall', 'un', 'updates', 'u', 'source', 's', 'list', 'l', 'upload', 'search', 'info', 'like', 'dislike'], nargs='?') parser.add_argument('app', help='app to do function with', nargs='?') @@ -43,7 +43,7 @@ except: print('Horsy may be not installed correctly. Please reinstall it or stop other horsy instances. ' 'If you installed it just now, please restart PC.') -if int(requests.get('https://github.com/horsy-ml/horsy/raw/master/web_vars/version').text) > version: +if int(request.get('https://github.com/horsy-ml/horsy/raw/master/web_vars/version').text) > version: print('New version available!') print('If you see this message again, or horsy doesn\'t launch itself for long time, please type ' 'horsy_updater in your terminal to update it manually.') @@ -51,7 +51,7 @@ if int(requests.get('https://github.com/horsy-ml/horsy/raw/master/web_vars/versi print('Updating...') print('Please wait, if process seems closed, its OK, just wait a bit.') with open(os.path.join(horsy_vars.horsypath) + '/horsy_updater.exe', 'wb') as f: - f.write(requests.get('https://github.com/horsy-ml/horsy/raw/master/bin/horsy_updater.exe').content) + f.write(request.get('https://github.com/horsy-ml/horsy/raw/master/bin/horsy_updater.exe').content) subprocess.Popen('horsy_updater.exe horsy', shell=True, close_fds=True) sys.exit(0) @@ -87,12 +87,13 @@ if args.vt_key: # Checking if arguments are empty to use in-app CLI if not args.option: log_logo() - option = ['install', 'uninstall', 'source', 'list', 'upload', 'search', 'info'][ - tui.menu(['install app', 'uninstall app', 'get source', 'list of installed apps', - 'upload your app', 'search for app', 'get information about app'])] + option = ['install', 'uninstall', 'updates', 'source', 'list', 'upload', 'search', 'info'][ + tui.menu(['install app', 'uninstall app', 'see updates', 'get source', + 'list of installed apps', 'upload your app', 'search for app', + 'get information about app'])] isNoArgs = True -if not args.app and option not in ['list', 'upload', 'update']: +if not args.app and option not in ['list', 'l', 'upload', 'updates', 'u']: log_logo() print('\n') app = tui.get(f'Select app to {option}') @@ -108,6 +109,15 @@ match option: case 'uninstall' | 'un': from modules.manager import uninstall uninstall(app) + case 'updates' | 'u': + from modules.updates import check + update_list = check() + if update_list: + print('Use following commands to update apps: \n') + for needs_update in update_list: + print(f'horsy i {needs_update}') + else: + print('No updates available') case 'source' | 's': from modules.source import get_source get_source(app) diff --git a/horsygui.py b/horsygui.py index 445bd3d..fb1b797 100644 --- a/horsygui.py +++ b/horsygui.py @@ -2,13 +2,12 @@ import os import sys import math import threading -import time import webbrowser import modules.vars as horsy_vars +from modules.request import request from PyQt5 import QtWidgets import ctypes import modules.gui as gui -import requests import subprocess @@ -46,16 +45,19 @@ def refresh_gui(): installed_apps() -def installed_apps(): - from modules.manager import apps_list +def fill_installed(apps: list): ui.installed_table.clear() - apps = apps_list(True) ui.installed_table.setColumnCount(4) ui.installed_table.setRowCount(math.ceil(len(apps) / 4)) for i in range(len(apps)): ui.installed_table.setItem(i // 4, i % 4, QtWidgets.QTableWidgetItem(str(apps[i]))) +def installed_apps(): + from modules.manager import apps_list + fill_installed(apps_list(True)) + + def update_app(): try: app_name = ui.installed_table.currentItem().text().replace('!', '') @@ -63,6 +65,7 @@ def update_app(): return else: from modules.gui_manager import install + ui.installed_table.currentItem().setText(app_name) install(app_name) except: return @@ -109,10 +112,9 @@ def install_app(): def check_updates(): from modules.updates import check - needupdate = check() + needupdate = check(True) try: - for module in needupdate: - print(module) + fill_installed(needupdate) except: gui.cpopup("Error", str(needupdate)) @@ -195,27 +197,20 @@ def login_logout_gui(): def get_users_apps(): def get_threaded(): - apps = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/users/public" - f"/{ui.username_box.text()}").json() try: - if apps['message'] == 'Too many requests': - while apps['message'] == 'Too many requests': - time.sleep(0.5) - apps = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/users/public" - f"/{ui.username_box.text()}").json() + apps = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/users/public" + f"/{ui.username_box.text()}").json() except: - pass + print("Error getting user apps") try: apps = apps['packages'] ui.manage_packages_table.clear() ui.manage_packages_table.setColumnCount(4) ui.manage_packages_table.setRowCount(math.ceil(len(apps) / 4)) - print(math.ceil(len(apps) / 4)) - print(apps) for i in range(len(apps)): ui.manage_packages_table.setItem(i // 4, i % 4, QtWidgets.QTableWidgetItem(str(apps[i]))) except: - pass + print("Error getting user apps") threading.Thread(target=get_threaded).start() @@ -252,6 +247,9 @@ if __name__ == "__main__": if not os.path.isfile(horsy_vars.horsypath + 'config.cfg'): with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: f.write('{}') + if not os.path.isfile(horsy_vars.horsypath + 'apps/versions.json'): + with open(horsy_vars.horsypath + 'apps/versions.json', 'w+') as f: + f.write('{}') # Checking version try: @@ -260,14 +258,14 @@ if __name__ == "__main__": gui.popup('Error', 'Horsy may be not installed correctly. Please reinstall it or stop another instances if ' 'running. If you installed it just now, please restart PC.') version = int(f.read()) - if int(requests.get('https://github.com/horsy-ml/horsy/raw/master/web_vars/version').text) > version: + if int(request.get('https://github.com/horsy-ml/horsy/raw/master/web_vars/version').text) > version: gui.popup('Update', 'New version available! \nWe appreciate your safety, so you need to update horsy.' '\nPress OK and updater will download the latest version.\n' 'If you see this message again, or horsy doesn\'t launch \n' 'itself for long time, please type horsy_updater in your terminal.') try: with open(os.path.join(horsy_vars.horsypath) + '/horsy_updater.exe', 'wb') as f: - f.write(requests.get('https://github.com/horsy-ml/horsy/raw/master/bin/horsy_updater.exe').content) + f.write(request.get('https://github.com/horsy-ml/horsy/raw/master/bin/horsy_updater.exe').content) except: gui.popup('Error', 'Could not download updater. \nMaybe installation folder is not writable ' '(only for admins).\n Please reinstall horsy or update it manually. \n' diff --git a/modules/change_email.py b/modules/change_email.py index 6afd1e0..f9781f4 100644 --- a/modules/change_email.py +++ b/modules/change_email.py @@ -1,4 +1,4 @@ -import requests +from modules.request import request import modules.gui as gui from modules.auth import get_auth import modules.vars as horsy_vars @@ -21,11 +21,12 @@ def change(email): try: def change_in_new_thread(): try: - r_code = handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', - json={'auth': auth, 'email': email}).status_code) + r_code = handle(request.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': auth, 'email': email}).status_code) gui.cpopup("Changing email", r_code[0]) except: gui.cpopup('Error', 'Unexpected error.') + threading.Thread(target=change_in_new_thread).start() gui.popup('Started', 'Check your email for confirmation') except: diff --git a/modules/change_password.py b/modules/change_password.py index 04a3439..5f7a4b5 100644 --- a/modules/change_password.py +++ b/modules/change_password.py @@ -1,4 +1,4 @@ -import requests +from modules.request import request import modules.gui as gui from modules.auth import get_auth import modules.vars as horsy_vars @@ -27,8 +27,8 @@ def change(oldpass, newpass): try: gui.cpopup("Changing password", - handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', - json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()), - 'password': newpass}).status_code)[0]) + handle(request.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()), + 'password': newpass}).status_code)[0]) except: gui.popup('Error', 'Unexpected error.') diff --git a/modules/download.py b/modules/download.py index 0b08353..a7dbeb3 100644 --- a/modules/download.py +++ b/modules/download.py @@ -7,6 +7,7 @@ import signal from functools import partial from threading import Event from urllib.request import urlopen +from urllib.parse import unquote from rich.progress import ( BarColumn, @@ -59,7 +60,7 @@ def dl(urls, dest_dir: str): with progress: with ThreadPoolExecutor(max_workers=len(urls)) as pool: for url in urls: - filename = url.split("/")[-1] + filename = unquote(url.split("/")[-1]) dest_path = os.path.join(dest_dir, filename) task_id = progress.add_task("download", filename=filename, start=False) pool.submit(copy_url, task_id, url, dest_path) diff --git a/modules/gui_manager.py b/modules/gui_manager.py index 9973f1e..75c6c71 100644 --- a/modules/gui_manager.py +++ b/modules/gui_manager.py @@ -1,6 +1,7 @@ import json import threading from modules.http_status import handle +from modules.request import request import requests import modules.vars as horsy_vars import os @@ -9,10 +10,11 @@ from modules.virustotal import get_key, scan_file, get_report from horsygui import UiDownloadWindow, download_ui from modules.gui import cpopup from PyQt5 import QtGui +from urllib.parse import unquote def install(package): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") r_code = handle(r.status_code) r = r.text r = json.loads(r) @@ -23,7 +25,7 @@ def install(package): try: UiDownloadWindow.show() download_ui.logs_box.clear() - download_ui.logs_box.append(f"Downloading {r['url'].split('/')[-1]}") + download_ui.logs_box.append(f"Downloading {unquote(r['url'].split('/')[-1])}") def install_this(): if not os.path.exists('{1}apps/{0}'.format(r['name'], horsy_vars.horsypath)): @@ -35,7 +37,8 @@ def install(package): file_r = requests.get(r['url'], stream=True) chunk_size = int(int(file_r.headers['Content-Length']) / 100) percent = 0 - with open('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath), "wb") as f: + with open('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), horsy_vars.horsypath), + "wb") as f: for chunk in file_r.iter_content(chunk_size=chunk_size): if chunk: percent += 1 @@ -47,15 +50,15 @@ def install(package): threads.append(threading.Thread(target=dl_main_file)) if r['download']: - download_ui.logs_box.append(f"Downloading {r['download'].split('/')[-1]}") + download_ui.logs_box.append(f"Downloading {unquote(r['download'].split('/')[-1])}") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) def dl_dep_file(): global success file_r = requests.get(r['download'], stream=True) chunk_size = int(int(file_r.headers['Content-Length']) / 100) - with open('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1], horsy_vars.horsypath), - "wb") as f: + with open('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]), + horsy_vars.horsypath), "wb") as f: for chunk in file_r.iter_content(chunk_size=chunk_size): if chunk: f.write(chunk) @@ -78,9 +81,9 @@ def install(package): download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) if r['url'].split('.')[-1] == 'zip': - download_ui.logs_box.append(f"Extracting {r['url'].split('/')[-1]}") + download_ui.logs_box.append(f"Extracting {unquote(r['url'].split('/')[-1])}") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) - unzip('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath), + unzip('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), horsy_vars.horsypath), '{1}apps/{0}'.format(r['name'], horsy_vars.horsypath)) download_ui.logs_box.append("") @@ -95,12 +98,15 @@ def install(package): download_ui.logs_box.append("If you want to disable scan, type horsy --vt disable in terminal") download_ui.logs_box.append("Starting virustotal scan for program") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) - scan_file('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], horsy_vars.horsypath)) - analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], r['url'].split('/')[-1], + scan_file('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), + horsy_vars.horsypath)) + analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], unquote(r['url'].split('/')[-1]), horsy_vars.horsypath)) - download_ui.logs_box.append(f"Scan finished for program \nYou can see report for program by opening: " + download_ui.logs_box.append(f"Scan finished for program \nYou can see report for program by " + f"opening: " f"{analysis['link']} \n" - f"{analysis['detect']['malicious']} antivirus flagged this file as malicious") + f"{analysis['detect']['malicious']} antivirus flagged this file as " + f"malicious") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) except: pass @@ -110,11 +116,11 @@ def install(package): download_ui.logs_box.append("") download_ui.logs_box.append("Starting virustotal scan for dependency") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) - scan_file('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1], + scan_file('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]), horsy_vars.horsypath)) download_ui.logs_box.append(f"Scan finished for dependency") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) - analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], r['download'].split('/')[-1], + analysis = get_report('{2}apps/{0}/{1}'.format(r['name'], unquote(r['download'].split('/')[-1]), horsy_vars.horsypath)) download_ui.logs_box.append(f"You can see report for dependency by opening: {analysis['link']}") download_ui.logs_box.append(f"{analysis['detect']['malicious']} " @@ -139,7 +145,7 @@ def install(package): download_ui.logs_box.append("Generating launch script") with open('{1}apps/{0}.bat'.format(r['name'], horsy_vars.horsypath), 'w') as f: f.write(f"@ECHO off\n") - f.write(f"""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')} %*\n""") + f.write(f""""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')}" %*\n""") download_ui.logs_box.append("") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) @@ -149,8 +155,17 @@ def install(package): download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) threading.Thread(target=os.system, args=('{2}apps/{0}/{1}'.format(r['name'], r['install'], horsy_vars.horsypath),)).start() + + # Update versions file + with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f: + versions = json.load(f) + with open(horsy_vars.horsypath + 'apps/versions.json', 'w') as f: + versions[r['name']] = r['version'] + f.write(json.dumps(versions)) + download_ui.logs_box.append(f"All done!\nYou can run your app by entering {r['name']} in terminal") download_ui.logs_box.moveCursor(QtGui.QTextCursor.End) + threading.Thread(target=install_this).start() except: diff --git a/modules/liker.py b/modules/liker.py index ec92642..092b6d5 100644 --- a/modules/liker.py +++ b/modules/liker.py @@ -1,4 +1,4 @@ -import requests +from modules.request import request import modules.vars as horsy_vars from modules.auth import get_auth @@ -17,6 +17,6 @@ def send(package, type, is_gui=False, login_ui=None, Ui_LoginWindow=None): "rate": type, "packageName": package } - r = requests.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/rate", json=body).json() + r = request.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/rate", json=body).json() print(r["message"]) return r["message"] diff --git a/modules/login.py b/modules/login.py index ca72875..c7140e0 100644 --- a/modules/login.py +++ b/modules/login.py @@ -1,4 +1,4 @@ -import requests +from modules.request import request from PyQt5 import QtWidgets import modules.gui as gui from modules.auth import get_auth, del_auth @@ -14,9 +14,9 @@ def loginload(): with open(horsy_vars.horsypath + 'config.cfg') as f: config = json.load(f) if config['auth'] is not None: - return (lambda x: (x if x != "Forbidden" else "Invalid login"))\ - (requests.get(horsy_vars.protocol + horsy_vars.server_url + '/users/login', - json={'auth': config['auth']}).json()['message']) + return (lambda x: (x if x != "Forbidden" else "Invalid login")) \ + (request.get(horsy_vars.protocol + horsy_vars.server_url + '/users/login', + json={'auth': config['auth']}).json()['message']) except: pass diff --git a/modules/manager.py b/modules/manager.py index d7c52d3..a7a3b05 100644 --- a/modules/manager.py +++ b/modules/manager.py @@ -1,7 +1,7 @@ import json import threading from rich import print -import requests +from modules.request import request import modules.vars as horsy_vars import os import zipfile @@ -11,7 +11,7 @@ from modules.download import dl def install(package): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") r_code = handle(r.status_code) r = r.text r = json.loads(r) @@ -78,7 +78,15 @@ def install(package): with open('{1}apps\{0}.bat'.format(r['name'], horsy_vars.horsypath), 'w+') as f: f.write(f"@ECHO off\n") - f.write(f"""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')} %*\n""") + f.write(f""""{r['run'].replace('$appdir$', f'%horsypath%/apps/{r["name"]}')}" %*\n""") + + # Update versions file + with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f: + versions = json.load(f) + with open(horsy_vars.horsypath + 'apps/versions.json', 'w') as f: + versions[r['name']] = r['version'] + f.write(json.dumps(versions)) + print(f"Versions file updated") # Done message print(f"[green][OK] All done![/]") @@ -114,4 +122,4 @@ def apps_list(is_gui=False): elif file.endswith(".bat"): apps.append(file.split('.')[0]) if is_gui: - return apps + return sorted(apps) diff --git a/modules/package_edit.py b/modules/package_edit.py index 040cc6c..c062fcd 100644 --- a/modules/package_edit.py +++ b/modules/package_edit.py @@ -1,7 +1,7 @@ from modules.auth import get_auth from horsygui import login_ui, UiLoginWindow as Ui_LoginWindow import modules.gui as gui -import requests +from modules.request import request import modules.vars as horsy_vars from modules.http_status import handle import json @@ -11,7 +11,7 @@ def edit(package, UiPackageWindow): package_ui = gui.Ui_PackageWindow() package_ui.setupUi(UiPackageWindow) - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") try: r_code = handle(r.status_code) if r_code[1] not in [200, 201]: @@ -44,15 +44,15 @@ def edit(package, UiPackageWindow): } gui.cpopup("Updating", - handle(requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", - json=body).status_code)[0]) + handle(request.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", + json=body).status_code)[0]) package_ui.update_button.clicked.connect(send) def push_version(package): gui.cpopup("Pushing version", - handle(requests.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/push-version", json={ + handle(request.post(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/push-version", json={ 'auth': get_auth(True, login_ui, Ui_LoginWindow), 'name': package }).status_code)[0]) diff --git a/modules/request.py b/modules/request.py new file mode 100644 index 0000000..1bfc3ab --- /dev/null +++ b/modules/request.py @@ -0,0 +1,29 @@ +import time +import requests + + +class request: + @staticmethod + def _request(url, method, json=None): + while True: + r = requests.request(method, url, json=json) + if r.status_code != 429: + return r + else: + time.sleep(0.5) + + @staticmethod + def get(url, json=None): + return request._request(url, 'GET', json=json) + + @staticmethod + def post(url, json=None): + return request._request(url, 'POST', json=json) + + @staticmethod + def put(url, json): + return request._request(url, 'PUT', json=json) + + @staticmethod + def delete(url): + return request._request(url, 'DELETE') diff --git a/modules/search.py b/modules/search.py index b15f4fb..c853977 100644 --- a/modules/search.py +++ b/modules/search.py @@ -1,6 +1,7 @@ import textwrap from algoliasearch.search_client import SearchClient import os +from modules.request import request import requests import modules.vars as horsy_vars import json @@ -27,7 +28,7 @@ def search(query, is_gui=False): def info(package, download_ui=None, UiDownloadWindow=None): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") r_code = handle(r.status_code) r = r.text r = json.loads(r) diff --git a/modules/source.py b/modules/source.py index 5b2381e..9696393 100644 --- a/modules/source.py +++ b/modules/source.py @@ -1,4 +1,4 @@ -import requests +from modules.request import request import json import webbrowser import modules.vars as horsy_vars @@ -7,7 +7,7 @@ from modules.http_status import handle def get_source(package): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") r_code = handle(r.status_code) if r_code[1] not in [200, 201]: return r_code[0] diff --git a/modules/updates.py b/modules/updates.py index c5ce230..cea21a8 100644 --- a/modules/updates.py +++ b/modules/updates.py @@ -1,22 +1,44 @@ import json -import requests +from modules.request import request import modules.vars as horsy_vars from modules.http_status import handle from modules.manager import apps_list from rich import print -def check(): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/" - f"{','.join(apps_list(True))}") +def check(gui=False): + r = request.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/" + f"{','.join(apps_list(True))}") r_code = handle(r.status_code) if r_code[1] not in [200, 201]: return r_code[0] r = r.text r = json.loads(r) - print(r) - update_this = list() + with open(horsy_vars.horsypath + 'apps/versions.json', 'r') as f: + versions = json.load(f) - return update_this + need_update = list() + for app in r: + try: + if versions[app] < r[app]['version']: + need_update.append(app) + except (TypeError, KeyError): + if r[app]['version'] > 0: + need_update.append(app) + except Exception as e: + print(f"[red]Unexpected error![/]") + print(e) + need_update.sort() + + if not gui: + return need_update + + apps = list() + for app in apps_list(True): + if app in need_update: + apps.append("!" + app) + else: + apps.append(app) + return sorted(apps) diff --git a/modules/uploader.py b/modules/uploader.py index 876e063..4a34305 100644 --- a/modules/uploader.py +++ b/modules/uploader.py @@ -1,6 +1,6 @@ import json import time -import requests +from modules.request import request from rich import print from modules.auth import get_auth, del_auth, get_auth_without_login import re @@ -83,7 +83,7 @@ def upload(is_gui=False, ui=None): print('[red]Please, specify runtime[/red]') run = None - request = { + request_body = { 'auth': auth, 'name': project_name, 'description': description, @@ -127,7 +127,7 @@ def upload(is_gui=False, ui=None): if run == '': return 'Please, specify runtime' - request = { + request_body = { 'auth': auth, 'name': project_name, 'description': description, @@ -141,7 +141,7 @@ def upload(is_gui=False, ui=None): r = None while r is None: try: - r = requests.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request) + r = request.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request_body) r_code = handle(r.status_code) r = r.text r = json.loads(r) @@ -150,15 +150,15 @@ def upload(is_gui=False, ui=None): print('[red]Invalid credentials[/red]') print('Deleting auth from config') del_auth() - request['auth'] = get_auth() + request_body['auth'] = get_auth() print(r) r = None elif r_code[1] in [200, 201]: print('[green]Success, your project is created. You can install it by running[/] ' - '[i]horsy i {0}[/]'.format(request['name'])) + '[i]horsy i {0}[/]'.format(request_body['name'])) return 'Success, your project is created. You can install it by running horsy i {0}'.format( - request['name']) + request_body['name']) return r_code[0] except: