VLC_HTTP_LAUNCHER/menu.py

128 lines
4.3 KiB
Python
Raw Normal View History

2023-05-16 16:33:36 -04:00
from database import Database
import scrapper
import utils
from utils import get
2023-05-20 13:04:18 -04:00
import platform
import urllib
import colorama
2023-05-27 18:16:46 -04:00
from terminal_menu.Menu import Menu
2023-05-20 13:04:18 -04:00
2023-05-16 16:33:36 -04:00
2023-05-16 17:25:05 -04:00
uri = ""
preview_site = {}
2023-05-17 05:08:40 -04:00
preview_database = ""
2023-05-16 16:33:36 -04:00
def get_user_choice_by_menu(options: list, title = None, preview_command=None, preview_size: float = 0.3, show_search_hint: bool = False, skip_empty_entries = False, cursor_pos: int = 0, preview_args=None):
menu = Menu(options,
title,
preview_body_function=preview_command,
preview_ratio=preview_size,
skip_empty_option=skip_empty_entries,
cursor_pos=cursor_pos,
preview_args=preview_args)
2023-05-27 18:16:46 -04:00
return (menu.show());
2023-05-20 13:04:18 -04:00
2023-05-16 16:33:36 -04:00
def add_site(database: Database) -> dict:
print("add a site:")
site = {}
site.update({"url": input("url without protocol (ip:port):")})
site.update({"user": input("user(leave blank):")})
site.update({"password": input("password(leave blank):")})
2023-05-17 05:08:40 -04:00
site.update({"id": database.get_sites_table_len()})
name = input(f"name[{str(database.get_sites_table_len())}]")
2023-05-16 16:33:36 -04:00
if (name == ""):
2023-05-17 05:08:40 -04:00
name = database.get_sites_table_len()
2023-05-16 16:33:36 -04:00
site.update({"name": name})
return (site)
def files_preview(url: str) -> str:
if (not url.endswith("/")):
2023-05-16 16:33:36 -04:00
return (None)
files = scrapper.get_files_by_url(url);
2023-05-16 16:33:36 -04:00
return ("\n".join(files))
def get_files_formated_by_viewing_data(database: Database, files: list, site: dict, path: str):
out = []
current_url = utils.get_base_url(site) + urllib.parse.quote(path);
for file in files:
if file == "..":
out.append(file);
continue
url = current_url + urllib.parse.quote(file)
viewing_data = database.get_viewing_data(url);
if (viewing_data == None) or (viewing_data.get("finished") == False):
out.append(colorama.Style.BRIGHT + file)
else:
out.append(colorama.Fore.LIGHTBLACK_EX + file);
return (out);
def get_files_path(current_url: str, files: list):
out = []
for file in files:
out.append(current_url + file);
return (out);
2023-05-16 16:33:36 -04:00
def files(site:dict, path:str, database:Database, pos:int):
current_url = scrapper.get_url(site, path);
files = scrapper.get_files(site, path)
files_displayed = get_files_formated_by_viewing_data(database, files, site, path)
preview_args = get_files_path(current_url, files)
choose = get_user_choice_by_menu(files_displayed,
2023-05-16 16:33:36 -04:00
preview_command=files_preview,
preview_args=preview_args,
2023-05-16 16:33:36 -04:00
preview_size=0.3,
show_search_hint=True,
2023-05-29 13:23:39 -04:00
title=f"Index of {path}",
cursor_pos = pos)
2023-05-17 05:08:40 -04:00
if (choose == None):
2023-05-29 13:23:39 -04:00
database.set_last_cursor_pos(0)
2023-05-17 05:08:40 -04:00
return (None)
2023-05-29 13:23:39 -04:00
database.set_last_cursor_pos(choose)
2023-05-16 16:33:36 -04:00
return (files[choose])
def config_preview(site_name:str):
id = int(site_name.split(": ")[0])
2023-05-17 05:08:40 -04:00
site = preview_database.get_site_by_id(id)
if (site == None):
return
2023-05-16 16:33:36 -04:00
str = f"""
url: {site.get('url')}
user: {site.get('user')}
password: {site.get('password')}
"""
return (str)
2023-05-17 05:08:40 -04:00
def site_deleter(sites, database: Database):
global preview_database
preview_database = database
2023-05-20 13:04:18 -04:00
choose = get_user_choice_by_menu(sites,
2023-05-16 16:33:36 -04:00
show_search_hint=True,
preview_title="delete",
preview_command=config_preview)
return (choose)
2023-05-17 05:08:40 -04:00
def param_editor(lst: list):
2023-05-20 13:04:18 -04:00
choose = get_user_choice_by_menu(lst)
2023-05-17 05:08:40 -04:00
return (choose)
def site_editor(lst: list, database: Database):
global preview_database
preview_database = database
2023-05-20 13:04:18 -04:00
choose = get_user_choice_by_menu(lst + ["", "quit"],
2023-05-16 16:33:36 -04:00
skip_empty_entries=True,
show_search_hint=True,
2023-05-20 13:04:18 -04:00
title="Edit")
2023-05-16 16:33:36 -04:00
if (choose == len(lst) + 1):
return (None)
return (choose)
2023-05-17 05:08:40 -04:00
def sites(sites: list):
nb_site = len(sites)
2023-05-20 13:04:18 -04:00
choose = get_user_choice_by_menu(get(sites, "name") + ["", "add", "edit", "delete"],
2023-05-17 05:08:40 -04:00
skip_empty_entries=True,
show_search_hint=True)
return (choose)