Skip to content

Commit

Permalink
Clean MTV host handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Audionut committed Dec 17, 2024
1 parent 46ecd9b commit 0783e7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def prompt_user_for_confirmation(self, message: str) -> bool:
sys.exit(1)

async def check_images_concurrently(self, imagelist, meta):
approved_image_hosts = ['ptpimg', 'imgbox']
approved_image_hosts = ['ptpimg', 'imgbox', 'imgbb']
invalid_host_found = False # Track if any image is on a non-approved host

# Ensure meta['image_sizes'] exists
Expand Down
41 changes: 37 additions & 4 deletions src/trackers/MTV.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import datetime
import glob
import multiprocessing
from urllib.parse import urlparse


class MTV():
Expand Down Expand Up @@ -46,9 +47,29 @@ async def upload(self, meta, disctype):

async def upload_with_retry(self, meta, cookiefile, common, img_host_index=1):
approved_image_hosts = ['ptpimg', 'imgbox', 'imgbb']
url_host_mapping = {
"i.ibb.co": "imgbb",
"ptpimg.me": "ptpimg",
"images2.imgbox.com": "imgbox",
}
images_reuploaded = False

if all(any(host in image['raw_url'] for host in approved_image_hosts) for image in meta['image_list']):
normalized_approved_hosts = set(approved_image_hosts + list(url_host_mapping.keys())) # noqa F841
for image in meta['image_list']:
raw_url = image['raw_url']
parsed_url = urlparse(raw_url)
hostname = parsed_url.netloc
mapped_host = url_host_mapping.get(hostname, hostname)
if meta['debug']:
if mapped_host in approved_image_hosts:
console.print(f"[green]URL '{raw_url}' is correctly matched to approved host '{mapped_host}'.")
else:
console.print(f"[red]URL '{raw_url}' is not recognized as part of an approved host.")

if all(
url_host_mapping.get(urlparse(image['raw_url']).netloc, urlparse(image['raw_url']).netloc) in approved_image_hosts
for image in meta['image_list']
):
console.print("[green]Images are already hosted on an approved image host. Skipping re-upload.")
image_list = meta['image_list']

Expand Down Expand Up @@ -181,6 +202,12 @@ async def handle_image_upload(self, meta, img_host_index=1, approved_image_hosts
if approved_image_hosts is None:
approved_image_hosts = ['ptpimg', 'imgbox', 'imgbb']

url_host_mapping = {
"i.ibb.co": "imgbb",
"ptpimg.me": "ptpimg",
"images2.imgbox.com": "imgbox",
}

retry_mode = False
images_reuploaded = False
new_images_key = 'mtv_images_key'
Expand Down Expand Up @@ -280,9 +307,15 @@ async def handle_image_upload(self, meta, img_host_index=1, approved_image_hosts
for image in uploaded_images:
console.print(f"[debug] Response in upload_image_task: {image['img_url']}, {image['raw_url']}, {image['web_url']}")

if not all(any(x in image['raw_url'] for x in approved_image_hosts) for image in meta.get(new_images_key, [])):
console.print("[red]Unsupported image host detected, please use one of the approved image hosts")
return meta[new_images_key], True, images_reuploaded # Trigger retry_mode if switching hosts
for image in meta.get(new_images_key, []):
raw_url = image['raw_url']
parsed_url = urlparse(raw_url)
hostname = parsed_url.netloc
mapped_host = url_host_mapping.get(hostname, hostname)

if mapped_host not in approved_image_hosts:
console.print(f"[red]Unsupported image host detected in URL '{raw_url}'. Please use one of the approved image hosts.")
return meta[new_images_key], True, images_reuploaded # Trigger retry_mode if switching hosts

return meta[new_images_key], False, images_reuploaded

Expand Down

0 comments on commit 0783e7b

Please sign in to comment.