This commit is contained in:
Yuvi9587
2025-07-27 06:32:15 -07:00
parent 9db89cfad0
commit e3dd0e70b6
9 changed files with 508 additions and 254 deletions

View File

@@ -120,7 +120,7 @@ def download_from_api(
selected_cookie_file=None,
app_base_dir=None,
manga_filename_style_for_sort_check=None,
processed_post_ids=None # --- ADD THIS ARGUMENT ---
processed_post_ids=None
):
headers = {
'User-Agent': 'Mozilla/5.0',
@@ -139,9 +139,19 @@ def download_from_api(
parsed_input_url_for_domain = urlparse(api_url_input)
api_domain = parsed_input_url_for_domain.netloc
if not any(d in api_domain.lower() for d in ['kemono.su', 'kemono.party', 'coomer.su', 'coomer.party']):
logger(f"⚠️ Unrecognized domain '{api_domain}' from input URL. Defaulting to kemono.su for API calls.")
api_domain = "kemono.su"
fallback_api_domain = None
# --- START: MODIFIED DOMAIN LOGIC WITH FALLBACK ---
if 'kemono.cr' in api_domain.lower():
fallback_api_domain = 'kemono.su'
elif 'coomer.st' in api_domain.lower():
fallback_api_domain = 'coomer.su'
elif not any(d in api_domain.lower() for d in ['kemono.su', 'kemono.party', 'kemono.cr', 'coomer.su', 'coomer.party', 'coomer.st']):
logger(f"⚠️ Unrecognized domain '{api_domain}'. Defaulting to kemono.cr with fallback to kemono.su.")
api_domain = "kemono.cr"
fallback_api_domain = "kemono.su"
# --- END: MODIFIED DOMAIN LOGIC WITH FALLBACK ---
cookies_for_api = None
if use_cookie and app_base_dir:
cookies_for_api = prepare_cookies_for_request(use_cookie, cookie_text, selected_cookie_file, app_base_dir, logger, target_domain=api_domain)
@@ -178,7 +188,6 @@ def download_from_api(
logger("⚠️ Page range (start/end page) is ignored when a specific post URL is provided (searching all pages for the post).")
is_manga_mode_fetch_all_and_sort_oldest_first = manga_mode and (manga_filename_style_for_sort_check != STYLE_DATE_POST_TITLE) and not target_post_id
api_base_url = f"https://{api_domain}/api/v1/{service}/user/{user_id}"
page_size = 50
if is_manga_mode_fetch_all_and_sort_oldest_first:
logger(f" Manga Mode (Style: {manga_filename_style_for_sort_check if manga_filename_style_for_sort_check else 'Default'} - Oldest First Sort Active): Fetching all posts to sort by date...")
@@ -191,6 +200,12 @@ def download_from_api(
logger(f" Manga Mode: Starting fetch from page 1 (offset 0).")
if end_page:
logger(f" Manga Mode: Will fetch up to page {end_page}.")
# --- START: MANGA MODE FALLBACK LOGIC ---
is_first_page_attempt_manga = True
api_base_url_manga = f"https://{api_domain}/api/v1/{service}/user/{user_id}"
# --- END: MANGA MODE FALLBACK LOGIC ---
while True:
if pause_event and pause_event.is_set():
logger(" Manga mode post fetching paused...")
@@ -208,7 +223,10 @@ def download_from_api(
logger(f" Manga Mode: Reached specified end page ({end_page}). Stopping post fetch.")
break
try:
posts_batch_manga = fetch_posts_paginated(api_base_url, headers, current_offset_manga, logger, cancellation_event, pause_event, cookies_dict=cookies_for_api)
# --- START: MANGA MODE FALLBACK EXECUTION ---
posts_batch_manga = fetch_posts_paginated(api_base_url_manga, headers, current_offset_manga, logger, cancellation_event, pause_event, cookies_dict=cookies_for_api)
is_first_page_attempt_manga = False # Success, no need to fallback
# --- END: MANGA MODE FALLBACK EXECUTION ---
if not isinstance(posts_batch_manga, list):
logger(f"❌ API Error (Manga Mode): Expected list of posts, got {type(posts_batch_manga)}.")
break
@@ -220,9 +238,21 @@ def download_from_api(
logger(f" Manga Mode: No posts found within the specified page range ({start_page or 1}-{end_page}).")
break
all_posts_for_manga_mode.extend(posts_batch_manga)
logger(f"MANGA_FETCH_PROGRESS:{len(all_posts_for_manga_mode)}:{current_page_num_manga}")
current_offset_manga += page_size
time.sleep(0.6)
except RuntimeError as e:
# --- START: MANGA MODE FALLBACK HANDLING ---
if is_first_page_attempt_manga and fallback_api_domain:
logger(f" ⚠️ Initial API fetch (Manga Mode) from '{api_domain}' failed: {e}")
logger(f" ↪️ Falling back to old domain: '{fallback_api_domain}'")
api_domain = fallback_api_domain
api_base_url_manga = f"https://{api_domain}/api/v1/{service}/user/{user_id}"
is_first_page_attempt_manga = False
continue # Retry the same offset with the new domain
# --- END: MANGA MODE FALLBACK HANDLING ---
if "cancelled by user" in str(e).lower():
logger(f" Manga mode pagination stopped due to cancellation: {e}")
else:
@@ -232,7 +262,12 @@ def download_from_api(
logger(f"❌ Unexpected error during manga mode fetch: {e}")
traceback.print_exc()
break
if cancellation_event and cancellation_event.is_set(): return
if all_posts_for_manga_mode:
logger(f"MANGA_FETCH_COMPLETE:{len(all_posts_for_manga_mode)}")
if all_posts_for_manga_mode:
if processed_post_ids:
original_count = len(all_posts_for_manga_mode)
@@ -278,6 +313,12 @@ def download_from_api(
current_offset = (start_page - 1) * page_size
current_page_num = start_page
logger(f" Starting from page {current_page_num} (calculated offset {current_offset}).")
# --- START: STANDARD PAGINATION FALLBACK LOGIC ---
is_first_page_attempt = True
api_base_url = f"https://{api_domain}/api/v1/{service}/user/{user_id}"
# --- END: STANDARD PAGINATION FALLBACK LOGIC ---
while True:
if pause_event and pause_event.is_set():
logger(" Post fetching loop paused...")
@@ -296,11 +337,23 @@ def download_from_api(
logger(f"✅ Reached specified end page ({end_page}) for creator feed. Stopping.")
break
try:
# --- START: STANDARD PAGINATION FALLBACK EXECUTION ---
posts_batch = fetch_posts_paginated(api_base_url, headers, current_offset, logger, cancellation_event, pause_event, cookies_dict=cookies_for_api)
is_first_page_attempt = False # Success, no more fallbacks needed
# --- END: STANDARD PAGINATION FALLBACK EXECUTION ---
if not isinstance(posts_batch, list):
logger(f"❌ API Error: Expected list of posts, got {type(posts_batch)} at page {current_page_num} (offset {current_offset}).")
break
except RuntimeError as e:
# --- START: STANDARD PAGINATION FALLBACK HANDLING ---
if is_first_page_attempt and fallback_api_domain:
logger(f" ⚠️ Initial API fetch from '{api_domain}' failed: {e}")
logger(f" ↪️ Falling back to old domain: '{fallback_api_domain}'")
api_domain = fallback_api_domain
api_base_url = f"https://{api_domain}/api/v1/{service}/user/{user_id}"
is_first_page_attempt = False
continue # Retry the same offset with the new domain
# --- END: STANDARD PAGINATION FALLBACK HANDLING ---
if "cancelled by user" in str(e).lower():
logger(f" Pagination stopped due to cancellation: {e}")
else:
@@ -340,4 +393,4 @@ def download_from_api(
current_page_num += 1
time.sleep(0.6)
if target_post_id and not processed_target_post_flag and not (cancellation_event and cancellation_event.is_set()):
logger(f"❌ Target post {target_post_id} could not be found after checking all relevant pages (final check after loop).")
logger(f"❌ Target post {target_post_id} could not be found after checking all relevant pages (final check after loop).")