Fix update bug and get domain.

This commit is contained in:
Lovi 2024-12-12 09:36:35 +01:00
parent eb95a4e5ac
commit 0ab1ab8798
3 changed files with 58 additions and 40 deletions

View File

@ -38,12 +38,12 @@ def title_search(title_search: str) -> int:
# Find new domain if prev dont work
max_timeout = config_manager.get_int("REQUESTS", "timeout")
#domain_to_use, _ = search_domain(SITE_NAME, f"https://{SITE_NAME}")
domain_to_use, _ = search_domain(SITE_NAME, f"https://{SITE_NAME}")
# Send request to search for title
try:
response = client.get(
url=f"https://{SITE_NAME}.{DOMAIN_NOW}/?story={title_search.replace(' ', '+')}&do=search&subaction=search&titleonly=3",
url=f"https://{SITE_NAME}.{domain_to_use}/?story={title_search.replace(' ', '+')}&do=search&subaction=search&titleonly=3",
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7',

View File

@ -69,7 +69,6 @@ def search_domain(site_name: str, base_url: str):
Parameters:
- site_name (str): The name of the site to search the domain for.
- base_url (str): The base URL to construct complete URLs.
- follow_redirects (bool): To follow redirect url or not.
Returns:
tuple: The found domain and the complete URL.
@ -80,47 +79,67 @@ def search_domain(site_name: str, base_url: str):
domain = str(config_manager.get_dict("SITE", site_name)['domain'])
try:
# Test the current domain
response_follow = httpx.get(f"{base_url}.{domain}", headers={'user-agent': get_headers()}, timeout=max_timeout, follow_redirects=True)
response_follow.raise_for_status()
with httpx.Client(
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7',
'User-Agent': get_headers()
},
follow_redirects=True,
timeout=max_timeout
) as client:
response_follow = client.get(f"{base_url}.{domain}")
response_follow.raise_for_status()
except Exception as e:
query = base_url.split("/")[-1]
first_url = google_search(query)
console.print(f"[green]First url from google seach[white]: [red]{first_url}")
# Perform a Google search with multiple results
search_results = list(search(query, num_results=5))
#console.print(f"[green]Google search results[white]: {search_results}")
if first_url:
final_url = get_final_redirect_url(first_url, max_timeout)
if final_url != None:
console.print(f"\n[bold yellow]Suggestion:[/bold yellow] [white](Experimental)\n"
f"[cyan]New final URL[white]: [green]{final_url}")
def extract_domain(url):
parsed_url = urlparse(url)
domain = parsed_url.netloc
return domain.split(".")[-1]
new_domain_extract = extract_domain(str(final_url))
if msg.ask(f"[red]Do you want to auto update config.json - '[green]{site_name}[red]' with domain: [green]{new_domain_extract}", choices=["y", "n"], default="y").lower() == "y":
# Update domain in config.json
config_manager.config['SITE'][site_name]['domain'] = new_domain_extract
config_manager.write_config()
# Return config domain
#console.print(f"[cyan]Return domain: [red]{new_domain_extract} \n")
return new_domain_extract, f"{base_url}.{new_domain_extract}"
# Iterate through search results
for first_url in search_results:
console.print(f"[green]Checking url[white]: [red]{first_url}")
else:
console.print("[bold red]\nManually change the domain in the JSON file.[/bold red]")
raise
# Check if the base URL matches the Google search result
parsed_first_url = urlparse(first_url)
else:
console.print("[bold red]No valid URL to follow redirects.[/bold red]")
# Compare base url from google search with base url from config.json
if parsed_first_url.netloc.split(".")[0] == base_url:
console.print(f"[red]URL does not match base URL. Skipping.[/red]")
continue
try:
final_url = get_final_redirect_url(first_url, max_timeout)
if final_url is not None:
#console.print(f"\n[cyan]New final URL[white]: [green]{final_url}")
def extract_domain(url):
parsed_url = urlparse(url)
domain = parsed_url.netloc
return domain.split(".")[-1]
new_domain_extract = extract_domain(str(final_url))
if msg.ask(f"[cyan]\nDo you want to auto site[white]: [red]{site_name}[cyan] with domain[white]: [red]{new_domain_extract}", choices=["y", "n"], default="y").lower() == "y":
# Update domain in config.json
config_manager.config['SITE'][site_name]['domain'] = new_domain_extract
config_manager.write_config()
# Return config domain
return new_domain_extract, f"{base_url}.{new_domain_extract}"
except Exception as redirect_error:
console.print(f"[red]Error following redirect for {first_url}: {redirect_error}")
continue
# If no matching URL is found
console.print("[bold red]No valid URL found matching the base URL.[/bold red]")
raise Exception("No matching domain found")
# Ensure the URL is in string format before parsing
parsed_url = urlparse(str(response_follow.url))
@ -128,10 +147,9 @@ def search_domain(site_name: str, base_url: str):
tld = parse_domain.split('.')[-1]
if tld is not None:
# Update domain in config.json
config_manager.config['SITE'][site_name]['domain'] = tld
config_manager.write_config()
# Return config domain
return tld, f"{base_url}.{tld}"
return tld, f"{base_url}.{tld}"

View File

@ -56,7 +56,7 @@ def update():
percentual_stars = 0
# Check installed version
if __version__ != last_version:
if str(__version__).replace('v', '') != str(last_version).replace('v', '') :
console.print(f"[red]New version available: [yellow]{last_version}")
else:
console.print(f"[red]Everything is up to date")