Compare commits

..

No commits in common. "42c3f6b37d6f881b0bb9fc046a75c0077196027f" and "0d0692f16841d7b1d04f3faa16b54b835141e39f" have entirely different histories.

6 changed files with 120 additions and 215 deletions

View File

@ -1,55 +0,0 @@
from tinydb import TinyDB, Query
from tinydb.operations import set, delete
class Database():
def __init__(self):
self.db = TinyDB("database.json", indent=4)
self.query = Query()
self.cookies_table = self.db.table("cookies")
self.viewing_table = self.db.table("viewing")
self.sites_table = self.db.table("sites")
def get_viewing_data(self, url:str ):
return (self.viewing_table.get(self.query.url == url))
def get_sites(self):
return (self.sites_table.all())
def get_site_by_id(self, id: int):
return (self.sites_table.get(self.query.id == id))
def get_sites_table_len(self):
return (len(self.sites_table.all()))
def get_cookies_table_len(self):
return (len(self.cookies_table.all()))
def add_cookies(self, arg: dict):
self.cookies_table.insert(arg)
def get_cookies(self):
return (self.cookies_table.get(self.query.id == "0"))
def get_last_site_id(self):
cookies = self.get_cookies()
last_site_id = cookies.get("last_site")
return (last_site_id)
def get_last_site(self):
last_site_id = self.get_last_site_id()
last_site = self.get_site_by_id(last_site_id)
return (last_site)
def get_last_path(self):
cookies = self.get_cookies()
last_path = cookies.get("last_path")
return (last_path)
def set_last_site(self, value):
self.cookies_table.update(set("last_site", value), self.query.id == "0")
def set_last_path(self, value):
self.cookies_table.update(set("last_path", value), self.query.id == "0")
def add_site(self, site):
self.sites_table.insert(site)

160
main.py
View File

@ -1,34 +1,112 @@
from urllib.parse import unquote, quote
from scrapper import get_uri, get_files
import player
from database import Database
from utils import get_url
import menu
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 files_navigator(site: dict):
if site.get("id") == database.get_last_site_id():
path = database.get_last_path()
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 ([])
try:
response = requests.get(url)
if (response.status_code != 200):
print("connection:", response.reason)
sys.exit(1)
soup = BeautifulSoup(response.text, 'html.parser')
return(soup.find("h1").text[9:])
except:
return ("")
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:
path = '/'
url = url + "/"
while True:
database.set_last_path(path)
file = menu.files(get_url(site) + path)
if (file == None):
return
elif (file == "../" and path == "/"):
cookies_table.update(set("last_path", get_uri(url)), query.id == "0")
try:
files = get_files(url)
except:
print("Connection: ERROR")
return (1)
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("/")):
player.play(get_url(site) + path + file, database)
open_vlc(url + file)
else:
path = path + file
url = url + file
def add_site():
site = menu.add_site()
database.add_site(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 = database.get_site_by_id(id)
site = sites_table.get(query.id == id)
str = f"""
url: {site.get('url')}
user: {site.get('user')}
@ -47,13 +125,13 @@ def sites_deleter():
site_name = lst[choose]
id = int(site_name.split(": ")[0])
sites_table.remove(query.id == id)
if (database.get_last_site() == id):
database.set_last_path("")
database.set_last_site("")
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_editor(site_id: int):
def sites_editor():
lst = []
for i in database.get_sites():
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()
@ -61,7 +139,7 @@ def sites_editor(site_id: int):
return (1)
site_name = lst[choose]
id = int(site_name.split(": ")[0])
site = database.get_site_by_id(id)
site = sites_table.get(query.id == id)
lst = [f"name: {site.get('name')}", f"url: {site.get('url')}", f"user: {site.get('user')}", f"password: {site.get('password')}"]
terminal_menu = TerminalMenu(lst)
choose = terminal_menu.show()
@ -79,33 +157,35 @@ def sites_editor(site_id: int):
sites_table.update(set("password", input("password: ")), query.id == id)
def sites_navigator():
nb_site = database.get_sites_table_len()
if (nb_site == 0):
if (len(sites_table.all()) == 0):
add_site()
nb_site = 1
last_site = database.get_last_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):
files_navigator(last_site)
set_url(last_site)
files_navigator()
while True:
nb_site = database.get_sites_table_len()
terminal_menu = TerminalMenu(get(database.get_sites(), "name") + ["", "add", "edit", "delete"], 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 == nb_site + 1):
if (choose == len(sites_table.all()) + 1):
add_site();
elif (choose == nb_site + 2):
elif (choose == len(sites_table.all()) + 2):
sites_editor()
elif (choose == nb_site + 3):
elif (choose == len(sites_table.all()) + 3):
sites_deleter()
sites_navigator()
return
else:
set_url(database.get_sites()[choose])
set_url(sites_table.all()[choose])
files_navigator()
database = Database()
db = TinyDB("./database.json", indent=4)
query = Query()
if (database.get_cookies_table_len() == 0):
database.add_cookies({"last_path": "", "last_site": "", "id": "0"})
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();

66
menu.py
View File

@ -1,66 +0,0 @@
from simple_term_menu import TerminalMenu
from database import Database
from scrapper import get_files, get_uri
url = ""
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):")})
site.update({"id": str(database.get_sites_table_len())})
name = input(f"name[{str(len(sites_table.all()))}] :")
if (name == ""):
name = str(database.get_sites_table_len())
site.update({"name": name})
return (site)
def files_preview(filename: str) -> str:
if (not filename.endswith("/")):
return (None)
files = get_files(url + filename)
return ("\n".join(files))
def files(base_url:str):
global url
url = base_url
files = get_files(base_url)
terminal_menu = TerminalMenu(files,
preview_command=files_preview,
preview_size=0.3,
show_search_hint=True,
title=f"Index of {get_uri(url)}")
choose = terminal_menu.show()
return (files[choose])
def config_preview(site_name:str):
id = int(site_name.split(": ")[0])
site = database.get_site_by_id(id)
str = f"""
url: {site.get('url')}
user: {site.get('user')}
password: {site.get('password')}
"""
return (str)
def site_deleter(sites: list):
terminal_menu = TerminalMenu(sites,
show_search_hint=True,
preview_title="delete",
preview_command=config_preview)
choose = terminal_menu.show()
return (choose)
def site_editor(lst: list):
terminal_menu = TerminalMenu(lst + ["", "quit"],
skip_empty_entries=True,
show_search_hint=True,
preview_title="Edit",
preview_command=config_preview)
choose = terminal_menu.show()
if (choose == len(lst) + 1):
return (None)
return (choose)

View File

@ -1,19 +0,0 @@
from simple_term_menu import TerminalMenu
import vlc
from database import Database
def play(url: str, database: Database):
start_pos = 0
viewing_data = database.get_viewing_data(url)
if (viewing_data != None):
menu = TerminalMenu([f"go back to {str(viewing_data.get('last_pos'))}", "restart from 0:00"])
response = menu.show()
if (response == None):
return (1);
elif (response == 0):
start_pos = viewing_data.get("last_pos")
vlc_instance = vlc.Instance()
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(url)
player.set_media(media)
player.play()

View File

@ -1,29 +0,0 @@
from bs4 import BeautifulSoup
from urllib.parse import quote, unquote
import requests
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"):
files.append(unquote(element["href"]))
return (files)
def get_uri(url: str) -> []:
if (url in ["/../", "../"]):
return ([])
try:
response = requests.get(url)
if (response.status_code != 200):
print("connection:", response.reason)
sys.exit(1)
soup = BeautifulSoup(response.text, 'html.parser')
return(soup.find("h1").text[9:])
except:
return ("")

View File

@ -1,6 +0,0 @@
def get_url(site: dict):
if (site.get("user") == "" and site.get("password") == ""):
return (f"http://{site.get('url')}")
else:
return (f"http://{site.get('user')}:{site.get('password')}@{site.get('url')}")