Skip to content

Commit

Permalink
FIX - image reuploading
Browse files Browse the repository at this point in the history
Remove unnecessary console messages
  • Loading branch information
Audionut committed Aug 23, 2024
1 parent 50f6cdd commit 666cc07
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 141 deletions.
22 changes: 11 additions & 11 deletions src/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,9 +2221,8 @@ def create_base_from_existing_torrent(self, torrentpath, base_dir, uuid):
def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=False):
os.chdir(f"{meta['base_dir']}/tmp/{meta['uuid']}")
initial_img_host = self.config['DEFAULT'][f'img_host_{img_host_num}']
img_host = initial_img_host
console.print(f"[cyan]Starting image upload with host: {img_host}")

img_host = meta['imghost'] # Use the correctly updated image host from meta

image_list = []
newhost_list = []

Expand All @@ -2236,11 +2235,12 @@ def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_i
image_glob.remove('POSTER.png')
existing_images = meta.get('image_list', [])

# If the images are already uploaded, skip re-uploading unless in retry mode
if len(existing_images) >= total_screens and not retry_mode:
# Only skip uploading if retry_mode is False and the hosts match
if len(existing_images) >= total_screens and not retry_mode and img_host == initial_img_host:
console.print(f"[yellow]Skipping upload because images are already uploaded to {img_host}. Existing images: {len(existing_images)}, Required: {total_screens}")
return existing_images, total_screens

# Proceed with uploading images
with Progress(
TextColumn("[bold green]Uploading Screens..."),
BarColumn(),
Expand Down Expand Up @@ -2290,7 +2290,7 @@ def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_i
if response.get('status_code') != 200:
console.print("[yellow]PT Screens failed, trying next image host")
img_host_num += 1
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode)
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=True)
img_url = response['data']['image']['url']
raw_url = img_url
web_url = img_url
Expand All @@ -2307,7 +2307,7 @@ def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_i
if response.status_code != 200:
console.print("[yellow]Pixhost failed, trying next image host")
img_host_num += 1
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode)
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=True)
response = response.json()
raw_url = response['th_url'].replace('https://t', 'https://img').replace('/thumbs/', '/images/')
img_url = response['th_url']
Expand All @@ -2325,19 +2325,19 @@ def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_i
if response.get('status_code') != 200:
console.print("[yellow]Lensdump failed, trying next image host")
img_host_num += 1
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode)
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=True)
img_url = response['data']['image']['url']
raw_url = img_url
web_url = response['data']['url_viewer']
else:
console.print(f"[red]Unsupported image host: {img_host}")
img_host_num += 1
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode)
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=True)

# Update progress bar and print the result on the same line
progress.console.print(f"[cyan]Uploaded image {i+1}/{total_screens}: {raw_url}", end='\r')

# If the upload was successful, add the image details
# Add the image details to the list
image_dict = {'img_url': img_url, 'raw_url': raw_url, 'web_url': web_url}
image_list.append(image_dict)
progress.advance(upload_task)
Expand All @@ -2346,7 +2346,7 @@ def upload_screens(self, meta, screens, img_host_num, i, total_screens, custom_i
except Exception as e:
console.print(f"[yellow]Failed to upload {image} to {img_host}. Exception: {str(e)}")
img_host_num += 1
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode)
return self.upload_screens(meta, screens - i, img_host_num, i, total_screens, custom_img_list, return_dict, retry_mode=True)

time.sleep(0.5)

Expand Down
280 changes: 150 additions & 130 deletions src/trackers/MTV.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,152 +43,172 @@ async def upload(self, meta):

# Initiate the upload with retry logic
await self.upload_with_retry(meta, cookiefile, common)

async def upload_with_retry(self, meta, cookiefile, common, img_host_index=1):
approved_image_hosts = ['ptpimg', 'imgbox']

while img_host_index <= len(approved_image_hosts):
current_img_host_key = f'img_host_{img_host_index}'
current_img_host = self.config.get('DEFAULT', {}).get(current_img_host_key)
# 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)

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.")
# If retry_mode is True, switch to the next host
if retry_mode:
console.print(f"[yellow]Switching to the next image host. Current index: {img_host_index}")
img_host_index += 1
retry_mode = True # Set retry_mode to True if switching to an approved host
continue

meta['imghost'] = current_img_host
retry_mode = False # No retry unless switching to another host
# If we successfully uploaded images, break out of the loop
if image_list is not None:
break

torrent_filename = "BASE"
torrent_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/BASE.torrent"
torrent = Torrent.read(torrent_path)
if image_list is None:
console.print("[red]All image hosts failed. Please check your configuration.")
return

# Proceed with the rest of the upload process
torrent_filename = "BASE"
torrent_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/BASE.torrent"
torrent = Torrent.read(torrent_path)

if torrent.piece_size > 8388608: # 8 MiB in bytes
console.print("[red]Piece size is OVER 8M and does not work on MTV. Generating a new .torrent")

# Create a new torrent with piece size explicitly set to 8 MiB
from src.prep import Prep
prep = Prep(screens=meta['screens'], img_host=meta['imghost'], config=self.config)

# Check if the piece size exceeds 8 MiB and regenerate the torrent if needed
if torrent.piece_size > 8388608: # 8 MiB in bytes
console.print("[red]Piece size is OVER 8M and does not work on MTV. Generating a new .torrent")

# Create a new torrent with piece size explicitly set to 8 MiB
new_torrent = prep.CustomTorrent(
path=Path(meta['path']),
trackers=["https://fake.tracker"],
source="L4G",
private=True,
exclude_globs=["*.*", "*sample.mkv", "!sample*.*"],
include_globs=["*.mkv", "*.mp4", "*.ts"],
creation_date=datetime.now(),
comment="Created by L4G's Upload Assistant",
created_by="L4G's Upload Assistant"
)

# Explicitly set the piece size and update metainfo
new_torrent.piece_size = 8388608 # 8 MiB in bytes
new_torrent.metainfo['info']['piece length'] = 8388608 # Ensure 'piece length' is set

# Validate and write the new torrent
new_torrent.validate_piece_size()
new_torrent.generate(callback=prep.torf_cb, interval=5)
new_torrent.write(f"{meta['base_dir']}/tmp/{meta['uuid']}/MTV.torrent", overwrite=True)

torrent_filename = "MTV"

await common.edit_torrent(meta, self.tracker, self.source_flag, torrent_filename=torrent_filename)

cat_id = await self.get_cat_id(meta)
resolution_id = await self.get_res_id(meta['resolution'])
source_id = await self.get_source_id(meta)
origin_id = await self.get_origin_id(meta)
des_tags = await self.get_tags(meta)

# Screenshot and upload process
prep.screenshots(Path(meta['path']), meta['name'], meta['uuid'], meta['base_dir'], meta)
return_dict = {}

# Only enable retry_mode if switching to an approved image host
prep.upload_screens(
meta,
screens=meta['screens'],
img_host_num=img_host_index,
i=0,
total_screens=meta['screens'],
custom_img_list=[],
return_dict=return_dict,
retry_mode=retry_mode
new_torrent = prep.CustomTorrent(
path=Path(meta['path']),
trackers=["https://fake.tracker"],
source="L4G",
private=True,
exclude_globs=["*.*", "*sample.mkv", "!sample*.*"],
include_globs=["*.mkv", "*.mp4", "*.ts"],
creation_date=datetime.now(),
comment="Created by L4G's Upload Assistant",
created_by="L4G's Upload Assistant"
)

# Explicitly set the piece size and update metainfo
new_torrent.piece_size = 8388608 # 8 MiB in bytes
new_torrent.metainfo['info']['piece length'] = 8388608 # Ensure 'piece length' is set

# Validate and write the new torrent
new_torrent.validate_piece_size()
new_torrent.generate(callback=prep.torf_cb, interval=5)
new_torrent.write(f"{meta['base_dir']}/tmp/{meta['uuid']}/MTV.torrent", overwrite=True)

torrent_filename = "MTV"

await common.edit_torrent(meta, self.tracker, self.source_flag, torrent_filename=torrent_filename)

cat_id = await self.get_cat_id(meta)
resolution_id = await self.get_res_id(meta['resolution'])
source_id = await self.get_source_id(meta)
origin_id = await self.get_origin_id(meta)
des_tags = await self.get_tags(meta)

# Edit description and other details
await self.edit_desc(meta)
group_desc = await self.edit_group_desc(meta)
mtv_name = await self.edit_name(meta)

anon = 1 if meta['anon'] != 0 or bool(str2bool(str(self.config['TRACKERS'][self.tracker].get('anon', "False")))) else 0

desc_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/[{self.tracker}]DESCRIPTION.txt"
desc = open(desc_path, 'r').read()

torrent_file_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/[{self.tracker}]{meta['clean_name']}.torrent"
with open(torrent_file_path, 'rb') as f:
tfile = f.read()

files = {
'file_input': (f"{meta['name']}.torrent", tfile)
}

# Update meta['image_list'] with uploaded images
meta['image_list'] = return_dict.get('image_list', [])

# 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")
img_host_index += 1
continue

# Edit description and other details
await self.edit_desc(meta)
group_desc = await self.edit_group_desc(meta)
mtv_name = await self.edit_name(meta)

anon = 1 if meta['anon'] != 0 or bool(str2bool(str(self.config['TRACKERS'][self.tracker].get('anon', "False")))) else 0

desc_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/[{self.tracker}]DESCRIPTION.txt"
desc = open(desc_path, 'r').read()

torrent_file_path = f"{meta['base_dir']}/tmp/{meta['uuid']}/[{self.tracker}]{meta['clean_name']}.torrent"
with open(torrent_file_path, 'rb') as f:
tfile = f.read()

files = {
'file_input': (f"{meta['name']}.torrent", tfile)
}

data = {
'image': '',
'title': mtv_name,
'category': cat_id,
'Resolution': resolution_id,
'source': source_id,
'origin': origin_id,
'taglist': des_tags,
'desc': desc,
'groupDesc': group_desc,
'ignoredupes': '1',
'genre_tags': '---',
'autocomplete_toggle': 'on',
'fontfont': '-1',
'fontsize': '-1',
'auth': await self.get_auth(cookiefile),
'anonymous': anon,
'submit': 'true',
}
data = {
'image': '',
'title': mtv_name,
'category': cat_id,
'Resolution': resolution_id,
'source': source_id,
'origin': origin_id,
'taglist': des_tags,
'desc': desc,
'groupDesc': group_desc,
'ignoredupes': '1',
'genre_tags': '---',
'autocomplete_toggle': 'on',
'fontfont': '-1',
'fontsize': '-1',
'auth': await self.get_auth(cookiefile),
'anonymous': anon,
'submit': 'true',
}

if not meta['debug']:
with requests.Session() as session:
with open(cookiefile, 'rb') as cf:
session.cookies.update(pickle.load(cf))
response = session.post(url=self.upload_url, data=data, files=files)
try:
if "torrents.php" in response.url:
console.print(response.url)
if not meta['debug']:
with requests.Session() as session:
with open(cookiefile, 'rb') as cf:
session.cookies.update(pickle.load(cf))
response = session.post(url=self.upload_url, data=data, files=files)
try:
if "torrents.php" in response.url:
console.print(response.url)
else:
if "authkey.php" in response.url:
console.print(f"[red]No DL link in response, It may have uploaded, check manually.")
else:
if "authkey.php" in response.url:
console.print(f"[red]No DL link in response, It may have uploaded, check manually.")
else:
console.print(f"[red]Upload Failed. It doesn't look like you are logged in.")
except:
console.print(f"[red]It may have uploaded, check manually.")
print(traceback.print_exc())
else:
console.print(f"[cyan]Request Data:")
console.print(data)
return

console.print("[red]All image hosts failed. Please check your configuration.")
console.print(f"[red]Upload Failed. It doesn't look like you are logged in.")
except:
console.print(f"[red]It may have uploaded, check manually.")
print(traceback.print_exc())
else:
console.print(f"[cyan]Request Data:")
console.print(data)
return

async def handle_image_upload(self, meta, img_host_index=1, approved_image_hosts=None):
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)

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

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'],
img_host_num=img_host_index,
i=0,
total_screens=meta['screens'],
custom_img_list=[], # This remains to handle any custom logic in the original function
return_dict=return_dict,
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', [])

# 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'], False # No need to retry, successful upload

async def edit_desc(self, meta):
base = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/DESCRIPTION.txt", 'r').read()
Expand Down

0 comments on commit 666cc07

Please sign in to comment.