from tinydb import TinyDB, Query from bs4 import BeautifulSoup import requests from simple_term_menu import TerminalMenu import os import sys import subprocess from operator import itemgetter from urllib.parse import unquote from tinydb.operations import set, delete def get_files(url: str) -> []: if (url in ["/../", "../"]): return ([]) response = requests.get(url) if (response.status_code != 200): print("connection:", response.reason) sys.exit(1) soup = BeautifulSoup(response.text, 'html.parser') files = [] for element in soup.findAll("a"): file = {} file.update({"name": unquote(element['href'])}) file.update({"link": element["href"]}) files.append(file) return (files) def get_uri(url: str) -> []: if (url in ["/../", "../"]): return ([]) response = requests.get(url) # print("connection:", response.reason) if (response.status_code != 200): exit soup = BeautifulSoup(response.text, 'html.parser') return(soup.find("h1").text[9:]) def get(files: [], key: str): names = [] for file in files: names.append(file.get(key)) return (names) def open_vlc(url: str) -> None: with open(os.devnull, 'wb') as devnull: subprocess.check_call(['vlc', url], stdout=devnull, stderr=subprocess.STDOUT) def files_preview(filename: str) -> str: if (not filename.endswith("/")): return (None) files = get_files(url + filename) return ("\n".join(get(files, "name"))) def files_navigator(): global url path = cookies_table.get(query.id == "0").get("last_path") if (path): url = url + path else: url = url + "/" 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=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) elif (file_choose == 0 and get_uri(url) == "/"): return file = get(files, "link")[file_choose] if (not file.endswith("/")): open_vlc(url + file) else: url = url + file def add_site(): 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):")}) site.update({"id": len(sites_table.all())}) name = input(f"name[{str(len(sites_table.all()))}] :") if (name == ""): name = str(len(sites_table.all())) site.update({"name": name}) sites_table.insert(site) def set_url(site): global url cookies_table.update(set("last_site", site.get("id")), query.id == "0") if (site.get("user") and site.get("password")): url = f"http://{site.get('user')}:{site.get('password')}@{site.get('url')}/" 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() last_site_id = cookies_table.get(query.id == "0").get("last_site") last_site = sites_table.get(query.id == last_site_id) if (last_site != None): set_url(last_site) files_navigator() while 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): sites_deleter() sites_navigator() return else: set_url(sites_table.all()[choose]) files_navigator() db = TinyDB("./database.json", indent=4) query = Query() sites_table = db.table("sites") cookies_table = db.table("cookies") if (len(cookies_table.all()) == 0): cookies_table.insert({"last_path": "", "last_site": "", "id": "0"}) sites_navigator();