Skip to content

Commit

Permalink
Updates hackmyvm API
Browse files Browse the repository at this point in the history
  • Loading branch information
tory1103 committed Apr 21, 2022
1 parent 2d261a5 commit 8308536
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/hackmycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fire import Fire
from requests import post, get
from gdown import download
from json import loads
from exceptions import *
from prettytable.colortable import ColorTable, Themes
from my_pickledb import LoadPickleDB
Expand Down Expand Up @@ -288,7 +289,7 @@ def __list(self, update: bool = False) -> list: # Status: Working
}

if self.__database.exists("machines") and not update: machines = self.__database.get("machines")
else: machines = [[value.lower() for value in data.split()] for data in self.__api_post_data(data).splitlines()]
else: machines = [{key:value if value else "" for key, value in dictionary.items()} for dictionary in loads(self.__api_post_data(data))]

self.__database.set("machines", machines)
self.__database.save.as_json()
Expand Down Expand Up @@ -321,18 +322,18 @@ def list(self, level: str = "all", pending: bool = False, finished: bool = False
level_types: list = ["all", "easy", "medium", "hard"]
level = "skip" if pending or finished else level.lower() if level.lower() in level_types else "all"

machines_table = ColorTable(["Creation Date", "Creation time", "Machine Name", "Level", "Url"], theme=Themes.OCEAN)
machines_table = ColorTable(["Creation Date", "Creation time", "Machine Name", "Level", "Url", "Torrent"], theme=Themes.OCEAN)

for machine in self.__list(update) if not descendant else reversed(self.__list(update)):
machine_level = machine[3]
machine_level = machine["level"].lower()
# Change machine[3] in pending and finished with correct value
if level in [machine_level, "all"] or (pending and machine[3] == "pending") or (finished and machine[3] == "finished"):
machine[3] = "{0}{1}{2}".format(
if level in [machine_level, "all"] or (pending and machine["level"] == "pending") or (finished and machine["level"] == "finished"):
machine["level"] = "{0}{1}{2}".format(
"\033[92m" if machine_level == "easy" else "\033[93m" if machine_level == "medium" else "\033[91m" if machine_level == "hard" else "\033[90m",
machine[3],
machine["level"],
"\033[0m"
)
machines_table.add_row(machine)
machines_table.add_row([machine["created"].split(" ")[0], machine["created"].split(" ")[1], machine["vmname"], machine["level"], machine["url"], machine["torrent"]])

return machines_table.get_string(fields=["Creation Date", "Machine Name", "Level"], end=machines_table.rowcount if not limit else limit)

Expand Down Expand Up @@ -373,7 +374,7 @@ def download(self, machine: str, no_verify: bool = False): # Status: Working
url = f"https://downloads.hackmyvm.eu/{machine}.zip"
if machine.startswith("http"): raise DownloadParamsInconsistency("You entered an URL try to add --no-verify parameter to command")

elif no_verify: url = machine
elif no_verify: url = machine

download(get(url, allow_redirects=True).url, url.split("/")[-1], quiet=False, fuzzy=True)

Expand Down

0 comments on commit 8308536

Please sign in to comment.