53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from tinydb import TinyDB, Query
|
|
from bs4 import BeautifulSoup
|
|
import requests
|
|
import os
|
|
|
|
db = TinyDB("./database.json", indent=4)
|
|
query = Query()
|
|
|
|
sites = db.table("sites")
|
|
nb_sites = len(sites.all());
|
|
site_choose = 0;
|
|
if (nb_sites == 0):
|
|
print("add a site:")
|
|
site = {}
|
|
site.update({"url": input("url:")})
|
|
site.update({"user": input("user(leave blank):")})
|
|
site.update({"password": input("password(leave blank):")})
|
|
sites.insert(site)
|
|
elif (nb_sites != 1):
|
|
for i, site in enumerate(sites.all()):
|
|
print(f"{i}: {site.get('url')}")
|
|
site_choose = -1
|
|
while (site_choose == -1):
|
|
choice = input("what site do you want ?")
|
|
if ((not choice.isdigit()) or (int(choice) >= nb_sites)):
|
|
print(f"error: invalid choice, please retry")
|
|
else:
|
|
site_choose = int(choice)
|
|
site = sites.all()[site_choose]
|
|
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')}/";
|
|
while True:
|
|
response = requests.get(url)
|
|
print("connection:", response.reason)
|
|
if (response.status_code != 200):
|
|
exit
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
files = soup.findAll("a")
|
|
for i, element in enumerate(files):
|
|
print(f"{i}: {element.text}")
|
|
file_choose = -1
|
|
while (file_choose == -1):
|
|
choice = input("what file or folder do you want ?")
|
|
if ((not choice.isdigit()) or (int(choice) >= len(files))):
|
|
print(f"error: invalid choice, please retry")
|
|
else:
|
|
file_choose = int(choice)
|
|
url = url + files[file_choose].text
|
|
if (not files[file_choose].text.endswith("/")):
|
|
os.system(f"vlc {url}")
|