add: possibility to delete a site

This commit is contained in:
starnakin 2023-05-15 21:12:10 +02:00
parent da2724c2b3
commit 41f50a05c3

49
main.py
View File

@ -7,15 +7,15 @@ import sys
import subprocess import subprocess
from operator import itemgetter from operator import itemgetter
from urllib.parse import unquote from urllib.parse import unquote
from tinydb.operations import set from tinydb.operations import set, delete
def get_files(url: str) -> []: def get_files(url: str) -> []:
if (url in ["/../", "../"]): if (url in ["/../", "../"]):
return ([]) return ([])
response = requests.get(url) response = requests.get(url)
# print("connection:", response.reason)
if (response.status_code != 200): if (response.status_code != 200):
exit print("connection:", response.reason)
sys.exit(1)
soup = BeautifulSoup(response.text, 'html.parser') soup = BeautifulSoup(response.text, 'html.parser')
files = [] files = []
for element in soup.findAll("a"): for element in soup.findAll("a"):
@ -45,7 +45,7 @@ def open_vlc(url: str) -> None:
with open(os.devnull, 'wb') as devnull: with open(os.devnull, 'wb') as devnull:
subprocess.check_call(['vlc', url], stdout=devnull, stderr=subprocess.STDOUT) 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("/")): if (not filename.endswith("/")):
return (None) return (None)
files = get_files(url + filename) files = get_files(url + filename)
@ -62,7 +62,7 @@ def files_navigator():
while True: while True:
cookies_table.update(set("last_path", get_uri(url)), query.id == "0") cookies_table.update(set("last_path", get_uri(url)), query.id == "0")
files = get_files(url) 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() file_choose = terminal_menu.show()
if (file_choose == None): if (file_choose == None):
sys.exit(0) sys.exit(0)
@ -77,13 +77,13 @@ def files_navigator():
def add_site(): def add_site():
print("add a site:") print("add a site:")
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({"user": input("user(leave blank):")})
site.update({"password": input("password(leave blank):")}) site.update({"password": input("password(leave blank):")})
site.update({"id": len(sites_table.all())}) site.update({"id": len(sites_table.all())})
name = input("name[url] :") name = input(f"name[{str(len(sites_table.all()))}] :")
if (name == ""): if (name == ""):
name = site.get("url") name = str(len(sites_table.all()))
site.update({"name": name}) site.update({"name": name})
sites_table.insert(site) sites_table.insert(site)
@ -97,6 +97,31 @@ def set_url(site):
else: else:
url = f"http://{site.get('url')}/"; 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(): def sites_navigator():
if (len(sites_table.all()) == 0): if (len(sites_table.all()) == 0):
add_site() add_site()
@ -106,14 +131,18 @@ def sites_navigator():
set_url(last_site) set_url(last_site)
files_navigator() files_navigator()
while True: 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() choose = terminal_menu.show()
if (choose == None):
return (1)
if (choose == len(sites_table.all()) + 1): if (choose == len(sites_table.all()) + 1):
add_site(); add_site();
elif (choose == len(sites_table.all()) + 2): elif (choose == len(sites_table.all()) + 2):
pass pass
elif (choose == len(sites_table.all()) + 3): elif (choose == len(sites_table.all()) + 3):
pass sites_deleter()
sites_navigator()
return
else: else:
set_url(sites_table.all()[choose]) set_url(sites_table.all()[choose])
files_navigator() files_navigator()