diff --git a/main.py b/main.py index 8d82a68..9f92c5e 100644 --- a/main.py +++ b/main.py @@ -7,15 +7,15 @@ import sys import subprocess from operator import itemgetter from urllib.parse import unquote -from tinydb.operations import set +from tinydb.operations import set, delete def get_files(url: str) -> []: if (url in ["/../", "../"]): return ([]) response = requests.get(url) - # print("connection:", response.reason) if (response.status_code != 200): - exit + print("connection:", response.reason) + sys.exit(1) soup = BeautifulSoup(response.text, 'html.parser') files = [] for element in soup.findAll("a"): @@ -45,7 +45,7 @@ def open_vlc(url: str) -> None: with open(os.devnull, 'wb') as devnull: subprocess.check_call(['vlc', url], stdout=devnull, stderr=subprocess.STDOUT) -def preview(filename: str) -> str: +def files_preview(filename: str) -> str: if (not filename.endswith("/")): return (None) files = get_files(url + filename) @@ -62,7 +62,7 @@ def files_navigator(): while True: cookies_table.update(set("last_path", get_uri(url)), query.id == "0") files = get_files(url) - terminal_menu = TerminalMenu(get(files, "name"), preview_command=preview, preview_size=0.3, show_search_hint=True, title=get_uri(url)) + terminal_menu = TerminalMenu(get(files, "name"), preview_command=files_preview, preview_size=0.3, show_search_hint=True, title=get_uri(url)) file_choose = terminal_menu.show() if (file_choose == None): sys.exit(0) @@ -77,13 +77,13 @@ def files_navigator(): def add_site(): print("add a site:") site = {} - site.update({"url without protocol (ip:port)": input("url:")}) + site.update({"url": input("url without protocol (ip:port):")}) site.update({"user": input("user(leave blank):")}) site.update({"password": input("password(leave blank):")}) site.update({"id": len(sites_table.all())}) - name = input("name[url] :") + name = input(f"name[{str(len(sites_table.all()))}] :") if (name == ""): - name = site.get("url") + name = str(len(sites_table.all())) site.update({"name": name}) sites_table.insert(site) @@ -97,6 +97,31 @@ def set_url(site): else: url = f"http://{site.get('url')}/"; +def config_preview(site_name:str): + id = int(site_name.split(": ")[0]) + site = sites_table.get(query.id == id) + str = f""" + url: {site.get('url')} + user: {site.get('user')} + password: {site.get('password')} + """ + return (str) + +def sites_deleter(): + lst = [] + for i in sites_table.all(): + lst.append(f"{str(i.get('id'))}: {i.get('name')}") + terminal_menu = TerminalMenu(lst, show_search_hint=True, preview_title="delete", preview_command=config_preview) + choose = terminal_menu.show() + if (choose == None): + return (1) + site_name = lst[choose] + id = int(site_name.split(": ")[0]) + sites_table.remove(query.id == id) + if (cookies_table.get(query.id == "0").get("last_site") == id): + cookies_table.update(set("last_site", query.id == choose.get("id"))) + cookies_table.update(set("last_path", query.id == choose.get("id"))) + def sites_navigator(): if (len(sites_table.all()) == 0): add_site() @@ -106,14 +131,18 @@ def sites_navigator(): set_url(last_site) files_navigator() while True: - terminal_menu = TerminalMenu(get(sites_table.all(), "name") + ["", "add", "edit", "remove"], skip_empty_entries=True, show_search_hint=True) + terminal_menu = TerminalMenu(get(sites_table.all(), "name") + ["", "add", "edit", "delete"], skip_empty_entries=True, show_search_hint=True) choose = terminal_menu.show() + if (choose == None): + return (1) if (choose == len(sites_table.all()) + 1): add_site(); elif (choose == len(sites_table.all()) + 2): pass elif (choose == len(sites_table.all()) + 3): - pass + sites_deleter() + sites_navigator() + return else: set_url(sites_table.all()[choose]) files_navigator()