2021-05-09 12:01:50 +02:00
|
|
|
#! /usr/bin/python3
|
2021-03-21 12:52:55 +01:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import zipfile
|
|
|
|
import requests
|
|
|
|
|
|
|
|
arguments = sys.argv
|
|
|
|
arguments.remove(sys.argv[0])
|
2021-04-09 15:03:59 +02:00
|
|
|
url = "https://raw.githubusercontent.com/UntriexTv/test_directory/main/ver.json"
|
2021-03-21 12:52:55 +01:00
|
|
|
|
|
|
|
if len(arguments) == 0:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
command = arguments[0]
|
|
|
|
if command in ["u", "update"]:
|
2021-04-09 15:03:59 +02:00
|
|
|
try:
|
|
|
|
server_version = json.loads(requests.get(url).text)
|
|
|
|
except Exception as error:
|
|
|
|
print(f"CAN'T DOWNLOAD VERSION LIST. ERROR: {error}")
|
|
|
|
sys.exit()
|
2021-03-21 12:52:55 +01:00
|
|
|
if "-version" in arguments:
|
|
|
|
try:
|
|
|
|
version_download = arguments[arguments.index("-version") + 1]
|
|
|
|
except IndexError:
|
|
|
|
print("Version argument is empty.")
|
|
|
|
sys.exit()
|
|
|
|
if version_download not in list(server_version.keys()):
|
|
|
|
print("Version not found.")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
else:
|
2021-04-09 15:03:59 +02:00
|
|
|
try:
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("version.json", "r", encoding='utf-8') as f: # loading settings
|
2021-04-09 15:03:59 +02:00
|
|
|
version = json.load(f)
|
|
|
|
except:
|
2021-04-15 14:56:49 +02:00
|
|
|
version = {"id": 0, "version": "recovery"}
|
2021-03-21 12:52:55 +01:00
|
|
|
for ver, data in enumerate(server_version.values()):
|
2021-04-15 14:56:49 +02:00
|
|
|
if data["id"] > version["id"]:
|
2021-03-21 12:52:55 +01:00
|
|
|
version_download = list(server_version.keys())[ver]
|
2021-04-09 15:03:59 +02:00
|
|
|
try:
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("update.zip", "wb", encoding='utf-8') as save:
|
2021-04-09 15:03:59 +02:00
|
|
|
save.write(
|
|
|
|
bytes(requests.get(
|
|
|
|
f"https://github.com/UntriexTv/test_directory/releases/download/{version_download}/update.zip").content))
|
|
|
|
except Exception as error:
|
|
|
|
print(f"FAILED TO DOWNLOAD UPDATE. ERROR: {error}")
|
|
|
|
sys.exit()
|
2021-03-21 12:52:55 +01:00
|
|
|
with zipfile.ZipFile("update.zip", "r") as zip_ref:
|
2021-04-15 14:56:49 +02:00
|
|
|
zip_ref.extractall("")
|
2021-03-21 12:52:55 +01:00
|
|
|
os.remove("update.zip")
|
2021-04-09 15:03:59 +02:00
|
|
|
print("SUCCESS")
|
|
|
|
print(f"""Update from version {version["version"]} to {version_download} was sucesfull""")
|
2021-03-21 12:52:55 +01:00
|
|
|
|
|
|
|
if command == "clean":
|
|
|
|
if arguments[1] == "all":
|
|
|
|
open("log.txt", "w").close()
|
|
|
|
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("settings.json", "r", encoding='utf-8') as file:
|
2021-03-21 12:52:55 +01:00
|
|
|
settings = json.load(file)
|
|
|
|
|
|
|
|
for line in settings["heartbeat_table"]:
|
|
|
|
settings["heartbeat_table"][line] = []
|
|
|
|
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("settings.json", "w", encoding='utf-8') as file:
|
2021-03-21 12:52:55 +01:00
|
|
|
json.dump(settings, file, indent=2)
|
|
|
|
|
|
|
|
if arguments[1] == "log":
|
|
|
|
open("log.txt", "w").close()
|
|
|
|
|
|
|
|
if arguments[1] == "heartbeat_table":
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("settings.json", "r", encoding='utf-8') as file:
|
2021-03-21 12:52:55 +01:00
|
|
|
settings = json.load(file)
|
|
|
|
|
|
|
|
for line in settings["heartbeat_table"]:
|
|
|
|
settings["heartbeat_table"][line] = []
|
|
|
|
|
2021-04-15 14:56:49 +02:00
|
|
|
with open("settings.json", "w", encoding='utf-8') as file:
|
2021-03-21 12:52:55 +01:00
|
|
|
json.dump(settings, file, indent=2)
|
2021-04-21 19:28:26 +02:00
|
|
|
|
|
|
|
|