From 66859928dfcd086bd4a6a8b1bf144dd5049957a7 Mon Sep 17 00:00:00 2001 From: Audionut Date: Fri, 25 Oct 2024 11:32:07 +1000 Subject: [PATCH] MTV + imgbox fixes https://github.com/Audionut/Upload-Assistant/issues/99 --- src/prep.py | 9 +++---- src/trackers/MTV.py | 66 ++++++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/prep.py b/src/prep.py index 0db305e8..f959a831 100644 --- a/src/prep.py +++ b/src/prep.py @@ -2688,7 +2688,7 @@ def exponential_backoff(retry_count, initial_timeout): loop = asyncio.get_event_loop() # Run the imgbox upload in the current event loop - image_list = loop.run_until_complete(self.imgbox_upload(os.getcwd(), image_glob, meta)) # Pass all images + image_list = loop.run_until_complete(self.imgbox_upload(os.getcwd(), image_glob, meta, return_dict)) # Pass all images # Ensure the image_list contains valid URLs before continuing if image_list and all('img_url' in img and 'raw_url' in img and 'web_url' in img for img in image_list): @@ -2839,6 +2839,7 @@ def exponential_backoff(retry_count, initial_timeout): progress.advance(upload_task) i += 1 # Increment the image counter only after success + return_dict['image_list'] = image_list break # Break retry loop after a successful upload except Exception as e: @@ -2864,7 +2865,7 @@ def exponential_backoff(retry_count, initial_timeout): return image_list, i - async def imgbox_upload(self, chdir, image_glob, meta): + async def imgbox_upload(self, chdir, image_glob, meta, return_dict): try: os.chdir(chdir) image_list = [] @@ -2872,8 +2873,6 @@ async def imgbox_upload(self, chdir, image_glob, meta): console.print(f"[debug] Starting upload of {len(image_glob)} images to imgbox...") async with pyimgbox.Gallery(thumb_width=350, square_thumbs=False) as gallery: for image in image_glob: - # console.print(f"[blue]Uploading image: {image}") - try: async for submission in gallery.add([image]): if not submission['success']: @@ -2895,11 +2894,11 @@ async def imgbox_upload(self, chdir, image_glob, meta): return [] # Return empty list in case of error # After uploading all images, validate URLs and get sizes - # console.print("[blue]Validating images and retrieving their sizes...") valid_images = await self.check_images_concurrently(image_list, meta) if valid_images: console.print(f"[yellow]Successfully uploaded and validated {len(valid_images)} images.") + return_dict['image_list'] = valid_images # Set the validated images in return_dict else: console.print("[red]Failed to validate any images.") return [] # Return empty list if no valid images diff --git a/src/trackers/MTV.py b/src/trackers/MTV.py index e9fc7c6b..c77b4990 100644 --- a/src/trackers/MTV.py +++ b/src/trackers/MTV.py @@ -83,9 +83,10 @@ def enforce_size_limit(image_list, image_sizes): else: # Proceed with the retry logic if images are not hosted on an approved image host + images_reuploaded = False while img_host_index <= len(approved_image_hosts): # Call handle_image_upload and pass the updated meta with the current image host index - image_list, retry_mode = await self.handle_image_upload(meta, img_host_index, approved_image_hosts) + image_list, retry_mode, images_reuploaded = await self.handle_image_upload(meta, img_host_index, approved_image_hosts) # If retry_mode is True, switch to the next host if retry_mode: @@ -155,7 +156,7 @@ def enforce_size_limit(image_list, image_sizes): des_tags = await self.get_tags(meta) # Edit description and other details - await self.edit_desc(meta) + await self.edit_desc(meta, images_reuploaded) group_desc = await self.edit_group_desc(meta) mtv_name = await self.edit_name(meta) @@ -219,25 +220,30 @@ 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'] - current_img_host_key = f'img_host_{img_host_index}' - current_img_host = self.config.get('DEFAULT', {}).get(current_img_host_key) + retry_mode = False + images_reuploaded = False - if not current_img_host or current_img_host not in approved_image_hosts: - console.print("[red]Your preferred image host is not supported at MTV, re-uploading to an allowed image host.") - retry_mode = True # Ensure retry_mode is set to True when switching hosts - meta['imghost'] = approved_image_hosts[0] # Switch to the first approved host - else: - meta['imghost'] = current_img_host - retry_mode = False # Start with retry_mode False unless we know we need to switch + while True: + current_img_host_key = f'img_host_{img_host_index}' + current_img_host = self.config.get('DEFAULT', {}).get(current_img_host_key) + + if not current_img_host: + console.print("[red]No more image hosts left to try.") + raise Exception("No valid image host found in the config.") + + if current_img_host not in approved_image_hosts: + console.print(f"[red]Your preferred image host '{current_img_host}' is not supported at MTV, trying next host.") + retry_mode = True # Ensure retry_mode is set to True when switching hosts + images_reuploaded = True # Mark that reuploading occurred + img_host_index += 1 # Move to the next image host in the config + continue + else: + meta['imghost'] = current_img_host + break # Exit the loop when a valid host is found from src.prep import Prep prep = Prep(screens=meta['screens'], img_host=meta['imghost'], config=self.config) - - # Screenshot and upload process - prep.screenshots(Path(meta['path']), meta['name'], meta['uuid'], meta['base_dir'], meta) return_dict = {} - - # Call upload_screens with the appropriate retry_mode prep.upload_screens( meta, screens=meta['screens'], @@ -249,35 +255,45 @@ async def handle_image_upload(self, meta, img_host_index=1, approved_image_hosts retry_mode=retry_mode # Honor the retry_mode flag passed in ) - # Update meta['image_list'] with uploaded images - meta['image_list'] = return_dict.get('image_list', []) + # Overwrite meta['image_list'] with the newly uploaded images + new_image_list = return_dict.get('image_list', []) + if new_image_list: + meta['image_list'] = new_image_list # Overwrite with new images # Ensure images are from approved hosts if not all(any(x in image['raw_url'] for x in approved_image_hosts) for image in meta['image_list']): console.print("[red]Unsupported image host detected, please use one of the approved image hosts") - return meta['image_list'], True # Trigger retry_mode if switching hosts + return meta['image_list'], True, images_reuploaded # Trigger retry_mode if switching hosts - return meta['image_list'], False # No need to retry, successful upload + return meta['image_list'], False, images_reuploaded # Return retry_mode and images_reuploaded - async def edit_desc(self, meta): + async def edit_desc(self, meta, images_reuploaded): base = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/DESCRIPTION.txt", 'r', encoding='utf-8').read() + with open(f"{meta['base_dir']}/tmp/{meta['uuid']}/[{self.tracker}]DESCRIPTION.txt", 'w', encoding='utf-8') as desc: - # adding bd_dump to description if it exits and adding empty string to mediainfo if meta['bdinfo'] is not None: mi_dump = None bd_dump = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/BD_SUMMARY_00.txt", 'r', encoding='utf-8').read() else: mi_dump = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/MEDIAINFO_CLEANPATH.txt", 'r', encoding='utf-8').read()[:-65].strip() bd_dump = None + if bd_dump: desc.write("[mediainfo]" + bd_dump + "[/mediainfo]\n\n") elif mi_dump: desc.write("[mediainfo]" + mi_dump + "[/mediainfo]\n\n") + images = meta['image_list'] if len(images) > 0: - for each in range(len(images)): - raw_url = images[each]['raw_url'] - desc.write(f"[img={raw_url}][/img]\n") + for image in images: + raw_url = image['raw_url'] + img_url = image['img_url'] + + if images_reuploaded: + desc.write(f"[url={raw_url}][img=250]{img_url}[/img][/url]\n") + else: + desc.write(f"[img={raw_url}][/img]\n") + desc.write(f"\n\n{base}") desc.close() return