Update cmd_wdir.py

This commit is contained in:
m5rcel { Marcel }
2025-09-09 17:22:34 +02:00
committed by GitHub
parent 036ebf99ea
commit 0c6c414cdf

View File

@@ -1,7 +1,8 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse from urllib.parse import urljoin
from colorama import Fore, Style from colorama import Fore, Style
import re
class WdirCommand: class WdirCommand:
def __init__(self, url): def __init__(self, url):
@@ -12,7 +13,7 @@ class WdirCommand:
print(Fore.RED + "Usage: wdir <url>" + Style.RESET_ALL) print(Fore.RED + "Usage: wdir <url>" + Style.RESET_ALL)
return return
# Ensure URL has scheme # Ensure scheme
if not self.url.startswith("http://") and not self.url.startswith("https://"): if not self.url.startswith("http://") and not self.url.startswith("https://"):
self.url = "http://" + self.url self.url = "http://" + self.url
@@ -37,22 +38,34 @@ class WdirCommand:
if href.startswith("?") or href.startswith("#") or href == "../": if href.startswith("?") or href.startswith("#") or href == "../":
continue continue
full_url = urljoin(self.url, href)
filename = href.split("/")[-1] filename = href.split("/")[-1]
full_url = urljoin(self.url, href)
# Determine file type # Guess type
if "." in filename: if "." in filename:
ext = filename.split(".")[-1].lower() ext = filename.split(".")[-1].lower()
ftype = f".{ext} file" ftype = f".{ext} file"
else: else:
ftype = "Directory" ftype = "Directory"
files.append((filename, ftype, full_url)) # Try to extract file size and modified date from raw HTML
row_text = link.parent.get_text(" ", strip=True)
size_match = re.search(r"(\d+(?:\.\d+)?\s*(?:KB|MB|GB|B))", row_text, re.I)
date_match = re.search(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2})", row_text)
size = size_match.group(1) if size_match else "?"
modified = date_match.group(1) if date_match else "?"
files.append((filename, ftype, size, modified, full_url))
if not files: if not files:
print(Fore.YELLOW + "No files or directories found (maybe directory listing is disabled)." + Style.RESET_ALL) print(Fore.YELLOW + "No files or directories found (maybe directory listing is disabled)." + Style.RESET_ALL)
return return
print(Fore.GREEN + "\nFiles found:" + Style.RESET_ALL) print(Fore.GREEN + "\nFiles found:" + Style.RESET_ALL)
for fname, ftype, furl in files: print(Fore.MAGENTA + f"{'Name':<30}{'Type':<15}{'Size':<12}{'Modified':<20}" + Style.RESET_ALL)
print(f" {Fore.CYAN}{fname:<30}{Style.RESET_ALL} {Fore.WHITE}{ftype}{Style.RESET_ALL}") print(Fore.MAGENTA + "-" * 77 + Style.RESET_ALL)
for fname, ftype, size, modified, furl in files:
print(f"{Fore.CYAN}{fname:<30}{Style.RESET_ALL} "
f"{Fore.WHITE}{ftype:<15}{size:<12}{modified:<20}{Style.RESET_ALL}")