Compare commits
2 Commits
0d0692f168
...
42c3f6b37d
Author | SHA1 | Date | |
---|---|---|---|
42c3f6b37d | |||
1838ac4850 |
55
database.py
Normal file
55
database.py
Normal file
@ -0,0 +1,55 @@
|
||||
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
160
main.py
@ -1,112 +1,34 @@
|
||||
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
|
||||
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
|
||||
|
||||
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
|
||||
def files_navigator(site: dict):
|
||||
if site.get("id") == database.get_last_site_id():
|
||||
path = database.get_last_path()
|
||||
else:
|
||||
url = url + "/"
|
||||
path = '/'
|
||||
while True:
|
||||
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) == "/"):
|
||||
database.set_last_path(path)
|
||||
file = menu.files(get_url(site) + path)
|
||||
if (file == None):
|
||||
return
|
||||
elif (file == "../" and path == "/"):
|
||||
return
|
||||
file = get(files, "link")[file_choose]
|
||||
if (not file.endswith("/")):
|
||||
open_vlc(url + file)
|
||||
player.play(get_url(site) + path + file, database)
|
||||
else:
|
||||
url = url + file
|
||||
path = path + 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')}/";
|
||||
site = menu.add_site()
|
||||
database.add_site(site)
|
||||
|
||||
def config_preview(site_name:str):
|
||||
id = int(site_name.split(": ")[0])
|
||||
site = sites_table.get(query.id == id)
|
||||
site = database.get_site_by_id(id)
|
||||
str = f"""
|
||||
url: {site.get('url')}
|
||||
user: {site.get('user')}
|
||||
@ -125,13 +47,13 @@ def sites_deleter():
|
||||
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"))
|
||||
if (database.get_last_site() == id):
|
||||
database.set_last_path("")
|
||||
database.set_last_site("")
|
||||
|
||||
def sites_editor():
|
||||
def sites_editor(site_id: int):
|
||||
lst = []
|
||||
for i in sites_table.all():
|
||||
for i in database.get_sites():
|
||||
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()
|
||||
@ -139,7 +61,7 @@ def sites_editor():
|
||||
return (1)
|
||||
site_name = lst[choose]
|
||||
id = int(site_name.split(": ")[0])
|
||||
site = sites_table.get(query.id == id)
|
||||
site = database.get_site_by_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()
|
||||
@ -157,35 +79,33 @@ def sites_editor():
|
||||
sites_table.update(set("password", input("password: ")), query.id == id)
|
||||
|
||||
def sites_navigator():
|
||||
if (len(sites_table.all()) == 0):
|
||||
nb_site = database.get_sites_table_len()
|
||||
if (nb_site == 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)
|
||||
nb_site = 1
|
||||
last_site = database.get_last_site()
|
||||
if (last_site != None):
|
||||
set_url(last_site)
|
||||
files_navigator()
|
||||
files_navigator(last_site)
|
||||
while True:
|
||||
terminal_menu = TerminalMenu(get(sites_table.all(), "name") + ["", "add", "edit", "delete"], skip_empty_entries=True, show_search_hint=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)
|
||||
choose = terminal_menu.show()
|
||||
if (choose == None):
|
||||
return (1)
|
||||
if (choose == len(sites_table.all()) + 1):
|
||||
if (choose == nb_site + 1):
|
||||
add_site();
|
||||
elif (choose == len(sites_table.all()) + 2):
|
||||
elif (choose == nb_site + 2):
|
||||
sites_editor()
|
||||
elif (choose == len(sites_table.all()) + 3):
|
||||
elif (choose == nb_site + 3):
|
||||
sites_deleter()
|
||||
sites_navigator()
|
||||
return
|
||||
else:
|
||||
set_url(sites_table.all()[choose])
|
||||
set_url(database.get_sites()[choose])
|
||||
files_navigator()
|
||||
|
||||
db = TinyDB("./database.json", indent=4)
|
||||
query = Query()
|
||||
database = Database()
|
||||
|
||||
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"})
|
||||
if (database.get_cookies_table_len() == 0):
|
||||
database.add_cookies({"last_path": "", "last_site": "", "id": "0"})
|
||||
sites_navigator();
|
||||
|
66
menu.py
Normal file
66
menu.py
Normal file
@ -0,0 +1,66 @@
|
||||
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)
|
19
player.py
Normal file
19
player.py
Normal file
@ -0,0 +1,19 @@
|
||||
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()
|
29
scrapper.py
Normal file
29
scrapper.py
Normal file
@ -0,0 +1,29 @@
|
||||
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 ("")
|
Loading…
Reference in New Issue
Block a user