<ScriptableScraper>
	<details>
		<name>IMDb+</name>
		<author>KJ-Cox</author>
		<description>TMDb Advanced Movie Title Renaming Script (Collections + Smart Titles)</description>
		<id>314159265</id>
		<version major="5" minor="0" point="1" />
		<published month="04" day="10" year="2026" />
		<type>MovieDetailsFetcher|MovieCoverFetcher|MovieBackdropFetcher</type>
		<language>en</language>
	</details>

<!-- ========================================================= -->
<!-- SEARCH -->
<!-- ========================================================= -->

<action name="search">

	<!-- Skip files with '#CDUniverse#' prefix to allow IMDb+ to coexist with CDUniverse scraper-script -->
	<parse name="skip_cduniverse" input="${search.filename_noext}" regex="^\#CDUniverse\#" />
	<if test="${skip_cduniverse[0]}=">		<!-- Regular Expressions -->

		<!-- API key -->
		<set name="tmdb_api_key" value="10ab68e4cfcedb66e9e911cd7cfa0546" />

		<!-- ### OPTIONS ### -->                                    <!--       TRUE     |         FALSE         -->
		<set name="global_options_special_edition" value="true" /> <!-- Modify title   | Keep title as-is      -->

		<!-- ========================================================= -->
		<!-- OPTIONS XML LOADER -->
		<!-- ========================================================= -->

		<retrieve name="options_xml" file="C:\ProgramData\Team MediaPortal\MediaPortal\IMDb+\Options IMDb+ Scraper.xml" encoding="UTF-8" />
		
		<if test="${options_xml}!=">
			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_special_edition']" />
			<if test="${xml_option.count}=1"><set name="global_options_special_edition" value="${xml_option[0].@value}" /></if>
		</if>

		<!-- ========================================================= -->
		<!-- 1. IMDb ID SEARCH (PRIMARY) -->
		<!-- ========================================================= -->

		<if test="${search.imdb_id}!=">
			
			<set name="search_url" value="https://api.themoviedb.org/3/find/${search.imdb_id}?api_key=${tmdb_api_key}&amp;external_source=imdb_id" />
			<log LogLevel="Info" Message="TMDb SEARCH (IMDB): ${search_url}" />

			<if test="${search_url}!=">
				<retrieve name="tmdb_find" url="${search_url}" encoding="UTF-8" />
			</if>

			<set name="rx_tmdb_block"><![CDATA["movie_results":\[\{(.+?)\}\]]]></set>
			<parse name="tmdb_block" input="${tmdb_find}" regex="${rx_tmdb_block}" />

			<if test="${global_options_debug_imdb_sources}=true">
				<log LogLevel="Debug" Message="TMDb RESPONSE: ${tmdb_find}" />
			</if>
		</if>

		<!-- ========================================================= -->
		<!-- 3. TITLE SEARCH (FALLBACK) -->
		<!-- ========================================================= -->

		<if test="${tmdb_block}=">

			<!-- CLEAN SEARCH TITLE -->
			<set name="search_title_clean" value="${search.title}" />

			<!-- Remove brackets that are NOT just a year -->
			<replace name="search_title_clean" input="${search_title_clean}" pattern="\((?!\d{4}\))[^)]*\)" with="" />
			
			<!-- If year exists, strip anything after it -->
			<if test="${search.year}!=">
				<replace name="search_title_clean" input="${search_title_clean}" pattern="\s*\b${search.year}\b.*$" with="" />
			</if>

			<!-- REMOVE SPECIAL EDITION WORDS (safe, order matters) -->
			<replace name="search_title_clean" input="${search_title_clean}" pattern="(?i)\b(Extended Edition|Collector(?:'s)? Edition|Special Edition)\b" with="" />
			<replace name="search_title_clean" input="${search_title_clean}" pattern="(?i)\b(Extended|Unrated|Director(?:'s)? Cut|Alternate Ending|Ultimate Edition|Final Cut|Remastered)\b" with="" />

			<!-- CLEAN STRAY 'Edition' -->
			<replace name="search_title_clean" input="${search_title_clean}" pattern="(?i)\bEdition\b" with="" />

			<!-- REMOVE COMMON JUNK -->
			<replace name="search_title_clean" input="${search_title_clean}" pattern="(?i)\b(2160p|1080p|720p|480p|BluRay|BRRip|DVDRip|WEBRip|WEB-DL|HDR|HEVC|x264|x265|AVC|REMUX|10bit|8bit|DDP?5\.1|DD5\.1|DTS(?:-HD)?|MA|AAC)\b" with="" />

			<!-- CLEAN SPACING -->
			<replace name="search_title_clean" input="${search_title_clean}" pattern="\s+" with=" " />
			<replace name="search_title_clean" input="${search_title_clean}" pattern="^\s+|\s+$" with="" />

			<!-- Build query -->
			<set name="search_title_encoded" value="${search_title_clean}" />
			<replace name="search_title_encoded" input="${search_title_encoded}" pattern=" " with="%20" />

			<set name="search_url" value="https://api.themoviedb.org/3/search/movie?api_key=${tmdb_api_key}&amp;query=${search_title_encoded}" />

			<!-- Add year if exists -->
			<if test="${search.year}!=">
				<set name="search_url" value="${search_url}&amp;year=${search.year}" />
			</if>

			<log LogLevel="Info" Message="TMDb SEARCH (TITLE): ${search_title_clean} (${search.year})" />

			<if test="${search_url}!=">
				<retrieve name="tmdb_find" url="${search_url}" encoding="UTF-8" />
			</if>

			<!-- First result only -->
			<set name="rx_tmdb_block"><![CDATA["results":\[\{(.+?)\}\]]]></set>
			<parse name="tmdb_block" input="${tmdb_find}" regex="${rx_tmdb_block}" />

			<if test="${global_options_debug_imdb_sources}=true">
				<log LogLevel="Debug" Message="TMDb RESPONSE: ${tmdb_find}" />
			</if>
		</if>

		<!-- ========================================================= -->
		<!-- 4. EXTRACT DATA -->
		<!-- ========================================================= -->

		<if test="${tmdb_block}!=">

			<!-- ID -->
			<parse name="tmdb_id" input="${tmdb_block}" regex="&quot;id&quot;:[0-9]+" />
			<replace name="tmdb_id" input="${tmdb_id}" pattern="&quot;id&quot;:" with="" />

			<!-- TITLE -->
			<parse name="tmdb_title" input="${tmdb_block}" regex="&quot;title&quot;:&quot;[^&quot;]+&quot;" />
			<replace name="tmdb_title" input="${tmdb_title}" pattern="&quot;title&quot;:&quot;" with="" />
			<replace name="tmdb_title" input="${tmdb_title}" pattern="&quot;" with="" />

			<!-- TITLE SPECIAL EDITION -->
			<if test="${global_options_special_edition}=true">
				<!-- REGEX (simple + safe) -->
				<set name="rx_special_edition"><![CDATA[(?i)\b(3D|Extended|Unrated|Director(?:'s)? Cut|Alternate Ending|Special|Collector(?:'s)?|Ultimate|Final Cut|Remastered)\b]]></set>

				<!-- (3D|[Ee]xtended|[Uu]nrated|[Dd]irector'?s Cut|...) -->

				<!-- DETECT FROM FILENAME -->
				<parse name="special_file" input="${search.filename_noext}" regex="${rx_special_edition}" />

				<!-- FALLBACK: FOLDER -->
				<parse name="special_folder" input="${search.foldername}" regex="${rx_special_edition}" />

				<!-- SELECT -->
				<set name="special_final" value="" />

				<if test="${special_file[0][0]}!=">
					<set name="special_final" value="${special_file[0][0]}" />
				</if>

				<if test="${special_final}=">
					<if test="${special_folder[0][0]}!=">
						<set name="special_final" value="${special_folder[0][0]}" />
					</if>
				</if>

				<!-- APPLY -->
				<if test="${special_final}!=">
					<!-- NORMALIZE -->
					<set name="special_clean" value="${special_final}" />

					<replace name="special_clean" input="${special_clean}" pattern="Extended Edition" with="Extended" />
					<replace name="special_clean" input="${special_clean}" pattern="Collector" with="Collector's" />
					<replace name="special_clean" input="${special_clean}" pattern="Special Edition" with="Special" />

					<!-- Using unused 'details_url' to pass special edition strings onto details node -->
					<set name="movie[0].details_url" value="###${special_clean}###" />
				</if>
			</if>

			<!-- YEAR -->
			<parse name="tmdb_year" input="${tmdb_block}" regex="&quot;release_date&quot;:&quot;[0-9\-]+&quot;" />
			<replace name="tmdb_year" input="${tmdb_year}" pattern="&quot;release_date&quot;:&quot;" with="" />
			<replace name="tmdb_year" input="${tmdb_year}" pattern="-[0-9]{2}-[0-9]{2}&quot;" with="" />
			<replace name="tmdb_year_clean" input="${tmdb_year}" pattern="[^\d]" with="" />

			<!-- IMDb ID (ONLY available in /find, so optional) -->
			<if test="${search.imdb_id}!=">
				<set name="imdb_id_clean" value="${search.imdb_id}" />
			</if>

			<log LogLevel="Info" Message="TMDb SEARCH: ${search_title_clean} (${search.year})" />
			<log LogLevel="Info" Message="TMDb RESULT: ${tmdb_title} (${tmdb_year_clean})" />
			<log LogLevel="Debug" Message="Special Edition: ${special_clean}" />

			<!-- Assign -->
			<set name="movie[0].site_id" value="${tmdb_id}" />
			<set name="movie[0].title" value="${tmdb_title}" />
			<set name="movie[0].year" value="${tmdb_year_clean}" />
			<set name="movie[0].release_date" value="${tmdb_year_clean}" />

			<if test="${imdb_id_clean}!=">
				<set name="movie[0].imdb_id" value="${imdb_id_clean}" />
			</if>
		</if>
	</if>
	<if test="${skip_cduniverse[0]}!=">
		<log LogLevel="Info" Message="# Skipping CDUniverse file = ${search.filename}" />
	</if>
</action>

<!-- ========================================================= -->
<!-- DETAILS -->
<!-- ========================================================= -->

<action name="get_details">
    <set name="tmdb_api_key" value="10ab68e4cfcedb66e9e911cd7cfa0546" />

	<set name="current_tmdb_id" value="${movie.site_id}" />

	<if test="${current_tmdb_id}!=">

		<!-- ### OPTIONS ### -->                                        <!--       TRUE         |         FALSE         -->
		<set name="global_options_original_title" value="false" />      <!-- Original Title     | Always English Title  -->
		<set name="global_options_long_summary" value="true" />         <!-- Long summary       | Short summary         -->
		<set name="global_options_special_edition" value="true" />      <!-- Modify title       | Keep title as-is      -->
		<set name="global_options_one_writer_director" value="false" /> <!-- Only use one name  | Get all the names     -->		
		<set name="global_options_rename_titles" value="true" />        <!-- Rename titles      | Keep title as-is      -->	
		<set name="global_options_remove_first_roman" value="false" />  <!-- Remove 'I' numeral | Rename title as-is    -->
		<set name="global_options_refresh_all_fields" value="true" />   <!-- Update All Fields  | Update A Few + Empty  -->		
		<set name="global_options_debug_imdb_sources" value="false" />  <!-- Log HTML sources   | Not needed normally   -->

		<!--
			There is now expanded support for other English based rating systems.
				01 = USA
				09 = Australia
		-->
		<set name="global_options_english_movie_rating" value="01" />

		<!-- ========================================================= -->
		<!-- OPTIONS XML LOADER -->
		<!-- ========================================================= -->

		<retrieve name="options_xml" file="C:\ProgramData\Team MediaPortal\MediaPortal\IMDb+\Options IMDb+ Scraper.xml" encoding="UTF-8" />

		<if test="${options_xml}!=">
			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_original_title']" />
			<if test="${xml_option.count}=1"><set name="global_options_original_title" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_long_summary']" />
			<if test="${xml_option.count}=1"><set name="global_options_long_summary" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_special_edition']" />
			<if test="${xml_option.count}=1"><set name="global_options_special_edition" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_one_writer_director']" />
			<if test="${xml_option.count}=1"><set name="global_options_one_writer_director" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_rename_titles']" />
			<if test="${xml_option.count}=1"><set name="global_options_rename_titles" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_remove_first_roman']" />
			<if test="${xml_option.count}=1"><set name="global_options_remove_first_roman" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_refresh_all_fields']" />
			<if test="${xml_option.count}=1"><set name="global_options_refresh_all_fields" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_debug_imdb_sources']" />
			<if test="${xml_option.count}=1"><set name="global_options_debug_imdb_sources" value="${xml_option[0].@value}" /></if>

			<parse name="xml_option" input="${options_xml}" xpath="//set[@name='global_options_english_movie_rating']" />
			<if test="${xml_option.count}=1"><set name="global_options_english_movie_rating" value="${xml_option[0].@value}" /></if>

			<log LogLevel="Info" Message="Options XML loaded successfully" />
		</if>

		<if test="${options_xml}=">
			<log LogLevel="Debug" Message="No Options XML found, using defaults" />
		</if>

		<if test="${global_options_rename_titles}=true">
			<!-- LOAD DEFAULT -->
			<retrieve name="rename_default" file="C:\ProgramData\Team MediaPortal\MediaPortal\IMDb+\Rename dBase IMDb+ Scraper (Base).xml" encoding="UTF-8" />

			<!-- LOAD CUSTOM -->
			<retrieve name="rename_custom" file="C:\ProgramData\Team MediaPortal\MediaPortal\IMDb+\Rename dBase IMDb+ Scraper (Custom).xml" encoding="UTF-8" />
		</if>

		<!-- Can also use language=en-AU -->
		<set name="details_url" value="https://api.themoviedb.org/3/movie/${current_tmdb_id}?api_key=${tmdb_api_key}&amp;language=en-US&amp;append_to_response=credits,alternative_titles" />
		<log LogLevel="Info" Message="TMDb DETAILS URL: ${details_url}" />

		<if test="${details_url}!=">
			<retrieve name="details_page" url="${details_url}" encoding="UTF-8" />
		</if>

		<if test="${global_options_debug_imdb_sources}=true">
			<log LogLevel="Debug" Message="TMDb DETAILS RESPONSE: ${details_page}" />
		</if>
		
		<set name="release_url" value="https://api.themoviedb.org/3/movie/${current_tmdb_id}/release_dates?api_key=${tmdb_api_key}" />
		<retrieve name="release_page" url="${release_url}" encoding="UTF-8" />
		
		<if test="${global_options_debug_imdb_sources}=true">
			<log LogLevel="Debug" Message="TMDb RELEASE RESPONSE: ${release_page}" />
		</if>

		<!-- ========================================================= -->
		<!-- IMDb ID (FIX) -->
		<!-- ========================================================= -->

		<set name="rx_imdb"><![CDATA["imdb_id":"(tt[0-9]+)"]]></set>
		<parse name="imdb_id" input="${details_page}" regex="${rx_imdb}" />
		<set name="movie.imdb_id" value="${imdb_id[0][0]}" />

		<!-- ========================================================= -->
		<!-- CERTIFICATION -->
		<!-- ========================================================= -->

		<set name="target_country" value="US" />

		<if test="${global_options_english_movie_rating}=9">
			<set name="target_country" value="AU" />
		</if>

		<!-- PRIMARY COUNTRY BLOCK -->
		<set name="rx_country_block"><![CDATA["iso_3166_1":"${target_country}".+?"release_dates":\[(.*?)\]\s*\}]]></set>
		<parse name="country_block" input="${release_page}" regex="${rx_country_block}" />

		<!-- FALLBACK COUNTRY (US) -->
		<set name="rx_country_block_us"><![CDATA["iso_3166_1":"US".+?"release_dates":\[(.*?)\]\s*\}]]></set>
		<parse name="country_block_us" input="${release_page}" regex="${rx_country_block_us}" />

		<if test="${movie.certification}="><set name="update_certification" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_certification" value="true" /></if>

		<if test="${update_certification}=true">
			<set name="rx_cert"><![CDATA["certification":"([^"]*)"]]></set>

			<!-- ========================= -->
			<!-- PRIMARY COUNTRY PARSE -->
			<!-- ========================= -->
			<parse name="cert_all" input="${country_block}" regex="${rx_cert}" />

			<set name="cert_clean" value="" />

			<!-- DEPTH 5 -->
			<if test="${cert_all[0][0]}!=">
				<set name="cert_clean" value="${cert_all[0][0]}" />
			</if>
			<if test="${cert_clean}=">
				<set name="cert_clean" value="${cert_all[1][0]}" />
			</if>
			<if test="${cert_clean}=">
				<set name="cert_clean" value="${cert_all[2][0]}" />
			</if>
			<if test="${cert_clean}=">
				<set name="cert_clean" value="${cert_all[3][0]}" />
			</if>
			<if test="${cert_clean}=">
				<set name="cert_clean" value="${cert_all[4][0]}" />
			</if>

			<!-- ========================= -->
			<!-- FALLBACK TO US IF AU EMPTY -->
			<!-- ========================= -->
			<if test="${cert_clean}=">
				<if test="${target_country}=AU">
					<log LogLevel="Debug" Message="FALLBACK: AU empty, using US certification" />

					<parse name="cert_all_us" input="${country_block_us}" regex="${rx_cert}" />

					<if test="${cert_all_us[0][0]}!=">
						<set name="cert_clean" value="${cert_all_us[0][0]}" />
					</if>
					<if test="${cert_clean}=">
						<set name="cert_clean" value="${cert_all_us[1][0]}" />
					</if>
					<if test="${cert_clean}=">
						<set name="cert_clean" value="${cert_all_us[2][0]}" />
					</if>
					<if test="${cert_clean}=">
						<set name="cert_clean" value="${cert_all_us[3][0]}" />
					</if>
					<if test="${cert_clean}=">
						<set name="cert_clean" value="${cert_all_us[4][0]}" />
					</if>
				</if>
			</if>

			<!-- CLEAN (ORDER MATTERS) -->
			<replace name="cert_clean" input="${cert_clean}" pattern="Unrated|Not Rated" with="NR" />
			<replace name="cert_clean" input="${cert_clean}" pattern=" " with="" />

			<set name="movie.certification" value="${cert_clean}" />

			<log LogLevel="Info" Message="CERTIFICATION (${target_country}): ${cert_clean}" />
			
			<log LogLevel="Debug" Message="COUNTRY BLOCK: ${country_block}" />
			<log LogLevel="Debug" Message="US BLOCK: ${country_block_us}" />
		</if>

		<!-- ========================================================= -->
		<!-- RELEASE DATE (TMDb release_dates) -->
		<!-- ========================================================= -->

		<!-- MOVIE PRIMARY (stable TMDb field) -->
		<set name="rx_release_movie"><![CDATA["release_date":"([^"]+)"]]></set>
		<parse name="release_movie" input="${details_page}" regex="${rx_release_movie}" />

		<!-- SELECT -->
		<set name="release_final" value="" />

		<!-- PRIMARY -->
		<if test="${release_movie[0][0]}!=">
			<set name="release_final" value="${release_movie[0][0]}" />
		</if>

		<!-- FALLBACK (US only) -->
		<if test="${release_final}=">
			<set name="rx_release_date"><![CDATA["release_date":"([^"]+)"[^}]*"type":3]]></set>
			<parse name="release_value" input="${country_block}" regex="${rx_release_date}" />

			<if test="${release_value[0][0]}!=">
				<set name="release_final" value="${release_value[0][0]}" />
			</if>
		</if>

		<!-- CLEAN -->
		<replace name="release_clean" input="${release_final}" pattern="T.*" with="" />
		<set name="movie.release_date" value="${release_clean}" />

		<log LogLevel="Debug" Message="RELEASE BLOCK: ${country_block}" />
		<log LogLevel="Info" Message="RELEASE DATE: ${release_final}" />

		<!-- ========================================================= -->
		<!-- BASIC INFO + RENAME SYSTEM -->
		<!-- ========================================================= -->

		<!-- COLLECTION (TMDb) -->
		<set name="rx_collection"><![CDATA["belongs_to_collection":\{[^}]+?"name":"([^"]+)"]]></set>
		<parse name="collection" input="${details_page}" regex="${rx_collection}" />

		<set name="collection_tmdb" value="" />
		<if test="${collection[0][0]}!=">
			<set name="collection_tmdb" value="${collection[0][0]}" />
			<set name="collection_final" value="|${collection_tmdb}|" />
			<log LogLevel="Debug" Message="TMDb Collection Found: ${collection_tmdb}" />
		</if>

		<!-- TITLE BASE -->
		<set name="title_base" value="" />

		<if test="${global_options_original_title}=true">
			<set name="rx_original_title"><![CDATA["original_title":"([^"]+)"]]></set>
			<parse name="title_original" input="${details_page}" regex="${rx_original_title}" />
			<set name="title_base" value="${title_original[0][0]}" />
		</if>

		<if test="${global_options_original_title}!=true">
			<set name="rx_title"><![CDATA["title":"([^"]+)"]]></set>
			<parse name="title_en" input="${details_page}" regex="${rx_title}" />
			<set name="title_base" value="${title_en[0][0]}" />
		</if>

		<set name="title_final" value="${title_base}" />
		<set name="sortby_final" value="" />

		<!-- ========================================================= -->
		<!-- RENAME SYSTEM (CUSTOM → DEFAULT → AUTO) -->
		<!-- ========================================================= -->

		<if test="${global_options_rename_titles}=true">

			<set name="rx_rename"><![CDATA[<rename\s+id="${movie.imdb_id}"(?:\s+title="([^"]*)")?(?:\s+foreign="([^"]*)")?(?:\s+sortby="([^"]*)")?(?:\s+collection="([^"]*)")?\s*/>]]></set>

			<!-- RESET FLAGS -->
			<set name="xml_has_title" value="false" />
			<set name="xml_has_sortby" value="false" />
			<set name="xml_has_collection" value="false" />

			<!-- PARSE -->
			<parse name="rename_custom_match" input="${rename_custom}" regex="${rx_rename}" />
			<parse name="rename_default_match" input="${rename_default}" regex="${rx_rename}" />

			<!-- TITLE -->
			<if test="${rename_custom_match[0][0]}!=">
				<set name="title_final" value="${rename_custom_match[0][0]:htmldecode}" />
				<set name="xml_has_title" value="true" />
			</if>
			<if test="${xml_has_title}!=true">
				<if test="${rename_default_match[0][0]}!=">
					<set name="title_final" value="${rename_default_match[0][0]:htmldecode}" />
					<set name="xml_has_title" value="true" />
				</if>
			</if>

			<!-- SORTBY -->
			<if test="${rename_custom_match[0][2]}!=">
				<set name="sortby_final" value="${rename_custom_match[0][2]:htmldecode}" />
				<set name="xml_has_sortby" value="true" />
			</if>
			<if test="${xml_has_sortby}!=true">
				<if test="${rename_default_match[0][2]}!=">
					<set name="sortby_final" value="${rename_default_match[0][2]:htmldecode}" />
					<set name="xml_has_sortby" value="true" />
				</if>
			</if>

			<!-- COLLECTION -->
			<if test="${rename_custom_match[0][3]}!=">
				<set name="collection_final" value="|${rename_custom_match[0][3]:htmldecode}|" />
				<set name="xml_has_collection" value="true" />
			</if>
			<if test="${xml_has_collection}!=true">
				<if test="${rename_default_match[0][3]}!=">
					<set name="collection_final" value="|${rename_default_match[0][3]:htmldecode}|" />
					<set name="xml_has_collection" value="true" />
				</if>
			</if>

			<!-- REMOVE LEADING "THE" ONLY IF NO COLLECTION -->
			<if test="${xml_has_title}!=true">
				<if test="${collection_clean}=">
					<replace name="title_final" input="${title_final}" pattern="(?i)^the\s+" with="" />
				</if>
			</if>

			<!-- ===================================================== -->
			<!-- AUTO MODE -->
			<!-- ===================================================== -->

			<log LogLevel="Debug" Message="AUTO MODE: Processing" />

			<!-- COLLECTION ID -->
			<set name="rx_collection_id"><![CDATA["belongs_to_collection":\{.*?"id":([0-9]+)]]></set>
			<parse name="collection_id" input="${details_page}" regex="${rx_collection_id}" />

			<if test="${collection_id[0][0]}!=">

				<set name="collection_id_val" value="${collection_id[0][0]}" />

				<!-- LOAD COLLECTION -->
				<set name="collection_url" value="https://api.themoviedb.org/3/collection/${collection_id_val}?api_key=${tmdb_api_key}&amp;language=en-US" />
				<retrieve name="collection_page" url="${collection_url}" encoding="UTF-8" />

				<!-- PARTS -->
				<set name="rx_parts"><![CDATA["id":([0-9]+).*?"title":"([^"]+)".*?"release_date":"([^"]*)"]]></set>
				<parse name="parts" input="${collection_page}" regex="${rx_parts}" />

				<!-- INDEX -->
				<set name="movie_index" value="1" />

				<replace name="current_date" input="${release_final}" pattern="-" with="" />
				<if test="${current_date}=">
					<set name="current_date" value="99999999" />
				</if>

				<!-- ========================================================= -->
				<!-- INDEX CALCULATION DEBUG -->
				<!-- ========================================================= -->

				<log LogLevel="Debug" Message="CURRENT MOVIE DATE: ${current_date}" />

				<loop name="currPart" on="parts" limit="99">
					<replace name="date_clean" input="${currPart[2]}" pattern="-" with="" />

					<if test="${date_clean}=">
						<set name="date_clean" value="99999999" />
					</if>

					<log LogLevel="Debug" Message="COMPARE PART: ${currPart[1]} DATE: ${date_clean}" />

					<if test="${date_clean}&lt;${current_date}">
						<add name="movie_index" value1="${movie_index}" value2="1"/>
					</if>
				</loop>

				<log LogLevel="Info" Message="FINAL MOVIE INDEX: ${movie_index}" />

				<set name="found_index" value="${movie_index}" />

				<!-- SORT INDEX -->
				<if test="${found_index}&lt;10">
					<set name="sort_index" value="0${found_index}" />
				</if>
				<if test="${found_index}>=10">
					<set name="sort_index" value="${found_index}" />
				</if>

				<!-- CUSTOM SORT INDEX -->
				<if test="${xml_has_sortby}=true">
					<if test="${xml_has_title}!=true">
						<parse name="xml_index" input="${sortby_final}" regex="([0-9]+)$" />
						<set name="found_index" value="${xml_index}" />
					</if>
				</if>

				<!-- ========================================================= -->
				<!-- COLLECTION DEBUGGING -->
				<!-- ========================================================= -->

				<log LogLevel="Debug" Message="COLLECTION RAW: ${collection_tmdb}" />

				<replace name="collection_clean" input="${collection_tmdb}" pattern="\s+Collection$" with="" />
				<replace name="collection_clean" input="${collection_clean}" pattern="^The\s+" with="" />
				<replace name="collection_clean" input="${collection_clean}" pattern="(?i)\s*Trilogy$" with="" />

				<log LogLevel="Debug" Message="COLLECTION CLEAN: ${collection_clean}" />

				<!-- BASE -->
				<set name="collection_match" value="${collection_clean}" />
				<replace name="collection_match" input="${collection_match}" pattern="^\s+|\s+$" with="" />

				<parse name="collection_base" input="${collection_match}" regex="^(.+?)(?:\s*\(|$)" />
				<replace name="collection_base_clean" input="${collection_base[0][0]}" pattern="^\s+|\s+$" with="" />

				<!-- ========================================================= -->
				<!-- COLLECTION REGEX ESCAPE DEBUG -->
				<!-- ========================================================= -->

				<log LogLevel="Debug" Message="COLLECTION BASE CLEAN: ${collection_base_clean}" />

				<set name="collection_regex" value="${collection_base_clean}" />

				<!-- Escape regex-sensitive characters (hex-safe for MP engine) -->
				<replace name="collection_regex" input="${collection_regex}" pattern="\[" with="\x5B" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\]" with="\x5D" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\(" with="\x28" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\)" with="\x29" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\." with="\x2E" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\+" with="\x2B" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\*" with="\x2A" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\?" with="\x3F" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\^" with="\x5E" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\$" with="\x24" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\|" with="\x7C" />
				<replace name="collection_regex" input="${collection_regex}" pattern="&apos;" with="\x27" />
				<replace name="collection_regex" input="${collection_regex}" pattern="&amp;" with="\x26" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\{" with="\x7B" />
				<replace name="collection_regex" input="${collection_regex}" pattern="\}" with="\x7D" />

				<log LogLevel="Debug" Message="COLLECTION REGEX FINAL: ${collection_regex}" />

				<!-- APPLY -->
				<set name="auto_collection" value="|${collection_clean}|" />
				<set name="auto_sortby" value="${collection_clean} ${sort_index}" />

				<!-- ROMAN 1–30 -->
				<set name="roman" value="" />
				<if test="${found_index}=1"><set name="roman" value="I" /></if>
				<if test="${found_index}=2"><set name="roman" value="II" /></if>
				<if test="${found_index}=3"><set name="roman" value="III" /></if>
				<if test="${found_index}=4"><set name="roman" value="IV" /></if>
				<if test="${found_index}=5"><set name="roman" value="V" /></if>
				<if test="${found_index}=6"><set name="roman" value="VI" /></if>
				<if test="${found_index}=7"><set name="roman" value="VII" /></if>
				<if test="${found_index}=8"><set name="roman" value="VIII" /></if>
				<if test="${found_index}=9"><set name="roman" value="IX" /></if>
				<if test="${found_index}=10"><set name="roman" value="X" /></if>
				<if test="${found_index}=11"><set name="roman" value="XI" /></if>
				<if test="${found_index}=12"><set name="roman" value="XII" /></if>
				<if test="${found_index}=13"><set name="roman" value="XIII" /></if>
				<if test="${found_index}=14"><set name="roman" value="XIV" /></if>
				<if test="${found_index}=15"><set name="roman" value="XV" /></if>
				<if test="${found_index}=16"><set name="roman" value="XVI" /></if>
				<if test="${found_index}=17"><set name="roman" value="XVII" /></if>
				<if test="${found_index}=18"><set name="roman" value="XVIII" /></if>
				<if test="${found_index}=19"><set name="roman" value="XIX" /></if>
				<if test="${found_index}=20"><set name="roman" value="XX" /></if>
				<if test="${found_index}=21"><set name="roman" value="XXI" /></if>
				<if test="${found_index}=22"><set name="roman" value="XXII" /></if>
				<if test="${found_index}=23"><set name="roman" value="XXIII" /></if>
				<if test="${found_index}=24"><set name="roman" value="XXIV" /></if>
				<if test="${found_index}=25"><set name="roman" value="XXV" /></if>
				<if test="${found_index}=26"><set name="roman" value="XXVI" /></if>
				<if test="${found_index}=27"><set name="roman" value="XXVII" /></if>
				<if test="${found_index}=28"><set name="roman" value="XXVIII" /></if>
				<if test="${found_index}=29"><set name="roman" value="XXIX" /></if>
				<if test="${found_index}=30"><set name="roman" value="XXX" /></if>

				<!-- ========================================================= -->
				<!-- ALT TITLE (WITH SUBTITLE DETECTION) -->
				<!-- ========================================================= -->

				<!-- Extract alternative titles block -->
				<set name="rx_alt_block"><![CDATA["alternative_titles":\{"titles":\[(.*?)\]\}]]></set>
				<parse name="alt_block" input="${details_page}" regex="${rx_alt_block}" />

				<log LogLevel="Debug" Message="ALT BLOCK RAW: ${alt_block[0][0]}" />

				<!-- Look specifically for US 'with subtitle' -->
				<set name="rx_alt_with_sub"><![CDATA[\{[^}]*"iso_3166_1":"US"[^}]*"title":"([^"]+)"[^}]*"type":"with subtitle"[^}]*\}]]></set>
				<parse name="alt_with_sub" input="${alt_block[0][0]}" regex="${rx_alt_with_sub}" />

				<log LogLevel="Debug" Message="ALT WITH SUB MATCH: ${alt_with_sub}" />

				<if test="${alt_with_sub[0][0]}!=">
					<set name="alt_subtitle" value="${alt_with_sub[0][0]:htmldecode}" />
					<replace name="alt_subtitle" input="${alt_subtitle}" pattern="\." with=" " />
					<log LogLevel="Info" Message="ALT SUBTITLE USED: ${alt_subtitle}" />
				</if>

				<if test="${alt_subtitle}=">
					<set name="alt_subtitle" value="${title_base}" />
					<log LogLevel="Debug" Message="ALT SUBTITLE FALLBACK TO BASE TITLE: ${title_base}" />
				</if>

				<!-- TITLE CLEANING -->
				<log LogLevel="Debug" Message="=== TITLE CLEAN START ===" />
				<log LogLevel="Debug" Message="ORIGINAL: ${alt_subtitle}" />
				<log LogLevel="Debug" Message="COLLECTION: ${collection_clean}" />
				<log LogLevel="Debug" Message="COLLECTION REGEX: ${collection_regex}" />
				<log LogLevel="Debug" Message="INDEX: ${found_index}" />

				<set name="title_tmp" value="${alt_subtitle}" />

				<!-- NORMALIZE -->
				<replace name="title_tmp" input="${title_tmp}" pattern="²" with=" 2" />
				<replace name="title_tmp" input="${title_tmp}" pattern="³" with=" 3" />
				<replace name="title_tmp" input="${title_tmp}" pattern="⁴" with=" 4" />

				<!-- NORMALIZE PUNCTUATION -->
				<replace name="title_tmp" input="${title_tmp}" pattern="[?!]$" with="" />

				<!-- REMOVE COMMON PREFIXES -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)\s*\bEpisode\s+[IVXLC0-9]+\s*-\s*" with=" " />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)\bthe movie\b:?\s*" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^\s*(?:${collection_regex})?\s*(?:[0-9]+|[IVXLC]+)\s*:\s*Movie\b:?\s*" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^the\s+" with="" />

				<!-- REMOVE COLLECTION PREFIX -->
 				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^${collection_regex}(?:'s)?(?:\s*[:\-]\s+|\s+)" with="" />

				<!-- REMOVE DUPLICATE ROMAN SUBTITLE -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^(I|II|III|IV|V|VI|VII|VIII|IX|X)$" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^(${roman})$" with="" />

				<!-- FIX FRANCHISE REINTRO / DUPES -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^${collection_regex}s?\b:\s*" with="" />
				<if test="(?i)${title_tmp}=${collection_clean}">
					<set name="title_tmp" value="" />
				</if>

				<!-- REMOVE LABELS -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^\s*Presents:\s*" with="" />

				<!-- CLEAN LEADING INDEX (SAFE) -->
				<replace name="title_tmp" input="${title_tmp}" pattern="^\s*(?:${found_index}|[0-9]+|[IVXLC]+)\s*[:\-]\s*" with="" />

				<!-- BASIC CLEAN -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^the\s+(?=.+\s)" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^\s*and\s+the\s+" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="^[-–—]\s*" with="" />

				<!-- PART CLEAN -->
				<if test="${found_index}=2">
					<replace name="title_tmp" input="${title_tmp}" pattern="(?i)\bPart\s+(II|Two|2)\b" with="" />
				</if>
				<if test="${found_index}=3">
					<replace name="title_tmp" input="${title_tmp}" pattern="(?i)\bPart\s+(III|Three|3)\b" with="" />
				</if>

				<!-- REMOVE 3D -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)\b3D\b:?\s*" with="" />

				<!-- REMOVE PURE NUMBER TITLE -->
				<parse name="title_num" input="${title_tmp}" regex="(\d+)$" />
				<if test="${title_tmp}=${title_num[0][0]}">
					<set name="title_tmp" value="" />
				</if>

				<!-- REMOVE DUPLICATE COLLECTION -->
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^${collection_regex}$" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i):\s*${collection_regex}(\s+[0-9IVXLC]+)?" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)([:\-\s]+)?${collection_regex}(\s+[0-9IVXLC]+)?$" with="" />

				<!-- FINAL CLEAN -->
				<replace name="title_tmp" input="${title_tmp}" pattern="^[^a-zA-Z0-9]+$" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="^:\s*" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="[:\-\s]+$" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="\s{2,}" with=" " />
				<replace name="title_tmp" input="${title_tmp}" pattern="^\s+|\s+$" with="" />
				<replace name="title_tmp" input="${title_tmp}" pattern="(?i)^the$" with="" />

				<!-- FINAL GUARD -->
				<if test="${title_tmp}=${found_index}">
					<set name="title_tmp" value="" />
				</if>

				<log LogLevel="Debug" Message="FINAL: ${title_tmp}" />
				<log LogLevel="Debug" Message="=== TITLE CLEAN END ===" />

				<!-- BUILD TITLE -->
				<if test="${found_index}>1">
					<set name="auto_title" value="${collection_clean} ${roman}" />
					<if test="${title_tmp}!=">
						<set name="auto_title" value="${collection_clean} ${roman}: ${title_tmp}" />
					</if>
				</if>

				<if test="${found_index}=1">
					<if test="${title_tmp}!=">
						<set name="auto_title" value="${collection_clean} I: ${title_tmp}" />
					</if>
					<if test="${title_tmp}=">
						<if test="${global_options_remove_first_roman}=true">
							<set name="auto_title" value="${collection_clean}" />
						</if>
						<if test="${global_options_remove_first_roman}!=true">
							<set name="auto_title" value="${collection_clean} I" />
						</if>
					</if>
				</if>

				<log LogLevel="Info" Message="AUTO TITLE: ${auto_title}" />
				<log LogLevel="Debug" Message="AUTO SORTBY: ${auto_sortby}" />
				<log LogLevel="Debug" Message="AUTO COLLECTION: ${auto_collection}" />
			</if>
		</if>

		<!-- ========================================================= -->
		<!-- SPECIAL EDITION -->
		<!-- ========================================================= -->

		<set name="special_final" value="" />

		<if test="${global_options_special_edition}=true">

			<!-- EXTRACT SPECIAL EDITION FROM DETAILS_URL -->
			<set name="rx_special_pass"><![CDATA[###([^#]+)###]]></set>
			<parse name="special_pass" input="${movie.details_url}" regex="${rx_special_pass}" />

			<if test="${special_pass[0][0]}!=">
				<set name="special_final" value="${special_pass[0][0]}" />
			</if>
		</if>

		<!-- ===================================== -->
		<!-- FINAL MERGE: XML → AUTO FALLBACK -->
		<!-- ===================================== -->

		<!-- TITLE -->
		<if test="${xml_has_title}!=true">
			<if test="${auto_title}!=">
				<set name="title_final" value="${auto_title}" />
			</if>
		</if>

		<!-- SORTBY -->
		<if test="${xml_has_sortby}!=true">
			<if test="${auto_sortby}!=">
				<set name="sortby_final" value="${auto_sortby}" />
			</if>
		</if>

		<!-- COLLECTION -->
		<if test="${xml_has_collection}!=true">
			<if test="${auto_collection}!=">
				<set name="collection_final" value="${auto_collection}" />
			</if>
		</if>

		<!-- ========================================================= -->
		<!-- FINAL APPLY -->
		<!-- ========================================================= -->

		<if test="${special_final}!=">
			<set name="title_combined" value="${title_final} (${special_final})" />
		</if>

		<if test="${special_final}=">
			<set name="title_combined" value="${title_final}" />
		</if>
		
		<if test="${title_combined}!=">
			<set name="movie.title" value="${title_combined}" />
		</if>

		<if test="${sortby_final}!=">
			<replace name="sortby_final" input="${sortby_final}" pattern="^(The|A|An)\s+" with="" />
			<set name="movie.sortby" value="${sortby_final}" />
		</if>

		<if test="${collection_final}!=">
			<set name="movie.collections" value="${collection_final}" />
		</if>

		<log LogLevel="Info" Message="FINAL MERGE TITLE: ${title_combined}" />
		<log LogLevel="Info" Message="FINAL MERGE SORTBY: ${sortby_final}" />
		<log LogLevel="Info" Message="FINAL MERGE COLLECTION: ${collection_final}" />

		<!-- ========================================================= -->
		<!-- PLOT -->
		<!-- ========================================================= -->

		<if test="${movie.summary}="><set name="update_summary" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_summary" value="true" /></if>
		<if test="${update_summary}=true">
			<!-- GET OVERVIEW -->
			<set name="rx_overview"><![CDATA["overview":"((?:\\.|[^"\\])*)"]]></set>
			<parse name="overview" input="${details_page}" regex="${rx_overview}" />

			<!-- CLEAN -->
			<set name="summary_clean" value="${overview[0][0]:htmldecode}" />

			<!-- SELECT -->
			<if test="${global_options_long_summary}=true">
				<!-- Full summary -->
				<set name="movie.summary" value="${summary_clean}" />
			</if>

			<!-- Short summary -->
			<if test="${global_options_long_summary}!=true">
				<!-- Get first 300 chars -->
				<set name="rx_summary_short"><![CDATA[(^.{0,300})]]></set>
				<parse name="summary_short" input="${summary_clean}" regex="${rx_summary_short}" />

				<!-- Default to parsed -->
				<set name="summary_final" value="${summary_short[0][0]}" />

				<!-- If original is longer than 300 → trim last word -->
				<set name="rx_overflow"><![CDATA[^.{300}.+]]></set>
				<parse name="is_long" input="${summary_clean}" regex="${rx_overflow}" />

				<if test="${is_long[0]}!=">
					<replace name="summary_final" input="${summary_final}" pattern="\s+\S*$" with="" />
				</if>

				<set name="movie.summary" value="${summary_final}" />
			</if>
			
			<log LogLevel="Debug" Message="OVERVIEW RAW: ${overview}" />
			<log LogLevel="Info" Message="SUMMARY CLEAN: ${summary_clean}" />
		</if>

		<if test="${movie.year}="><set name="update_year" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_year" value="true" /></if>
		<if test="${update_year}=true">
			<!-- YEAR -->
			<set name="rx_year"><![CDATA["release_date":"(\d{4})]]></set>
			<parse name="year" input="${details_page}" regex="${rx_year}" />
			<set name="movie.year" value="${year[0][0]}" />
		</if>

		<if test="${movie.score}="><set name="update_score" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_score" value="true" /></if>
		<if test="${update_score}=true">
			<!-- RATING -->
			<set name="rx_rating"><![CDATA["vote_average":([0-9.]+)]]></set>
			<parse name="rating" input="${details_page}" regex="${rx_rating}" />
			<set name="movie.score" value="${rating[0][0]}" />
		</if>

		<if test="${movie.popularity}="><set name="update_popularity" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_popularity" value="true" /></if>
		<if test="${update_popularity}=true">
			<!-- POPULARITY -->
			<set name="rx_popularity"><![CDATA["vote_count":([0-9\.]+)]]></set>
			<parse name="popularity" input="${details_page}" regex="${rx_popularity}" />

			<replace name="popularity_int" input="${popularity[0][0]}" pattern="\..*" with="" />
			<set name="movie.popularity" value="${popularity_int}" />
		</if>

		<!-- ========================================================= -->
		<!-- CREW BLOCK -->
		<!-- ========================================================= -->

		<set name="rx_crew_block"><![CDATA["credits":\{.*?"crew":\[(.*?)\]\s*\}]]></set>
		<parse name="crew_block" input="${details_page}" regex="${rx_crew_block}" />

		<if test="${movie.directors}="><set name="update_directors" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_directors" value="true" /></if>
		<if test="${update_directors}=true">
			<!-- DIRECTORS -->
			<set name="rx_director"><![CDATA["name":"([^"]+)"[^}]*"job":"Director"]]></set>
			<parse name="directors" input="${crew_block}" regex="${rx_director}" />

			<set name="directors_tmp" value="|" />

			<!-- ONE ONLY -->
			<if test="${global_options_one_writer_director}=true">
				<loop name="currDirector" on="directors" limit="1">
					<set name="director_clean" value="${currDirector[0]:htmldecode}" />
					<replace name="director_clean" input="${director_clean}" pattern="\\u0027" with="'" />
					<set name="directors_tmp" value="${directors_tmp}${director_clean}|" />
				</loop>
			</if>

			<!-- MULTIPLE -->
			<if test="${global_options_one_writer_director}!=true">
				<loop name="currDirector" on="directors" limit="10">
					<set name="director_clean" value="${currDirector[0]:htmldecode}" />
					<replace name="director_clean" input="${director_clean}" pattern="\\u0027" with="'" />
					<set name="directors_tmp" value="${directors_tmp}${director_clean}|" />
				</loop>
			</if>

			<set name="movie.directors" value="${directors_tmp}" />
		</if>

		<if test="${movie.writers}="><set name="update_writers" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_writers" value="true" /></if>
		<if test="${update_writers}=true">
			<!-- WRITERS -->
			<set name="rx_writer"><![CDATA["name":"([^"]+)"[^}]*"job":"(Screenplay|Writer)"]]></set>
			<parse name="writers" input="${crew_block}" regex="${rx_writer}" />

			<set name="writers_tmp" value="|" />

			<!-- ONE ONLY -->
			<if test="${global_options_one_writer_director}=true">
				<loop name="currWriter" on="writers" limit="1">
					<set name="writer_clean" value="${currWriter[0]:htmldecode}" />
					<replace name="writer_clean" input="${writer_clean}" pattern="\\u0027" with="'" />
					<set name="writers_tmp" value="${writers_tmp}${writer_clean}|" />
				</loop>
			</if>

			<!-- MULTIPLE -->
			<if test="${global_options_one_writer_director}!=true">
				<loop name="currWriter" on="writers" limit="10">
					<set name="writer_clean" value="${currWriter[0]:htmldecode}" />
					<replace name="writer_clean" input="${writer_clean}" pattern="\\u0027" with="'" />
					<set name="writers_tmp" value="${writers_tmp}${writer_clean}|" />
				</loop>
			</if>

			<set name="movie.writers" value="${writers_tmp}" />
		</if>

		<if test="${global_options_debug_imdb_sources}=true">
			<log LogLevel="Debug" Message="CREW BLOCK: ${crew_block}" />
		</if>
		<log LogLevel="Info" Message="DIRECTORS FOUND: ${directors}" />
		<log LogLevel="Info" Message="WRITERS FOUND: ${writers}" />

		<!-- ========================================================= -->
		<!-- GENRES -->
		<!-- ========================================================= -->

		<if test="${movie.genres}="><set name="update_genres" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_genres" value="true" /></if>
		<if test="${update_genres}=true">
			<set name="rx_genre_block"><![CDATA["genres":\[(.+?)\]\,]]></set>
			<parse name="genre_block" input="${details_page}" regex="${rx_genre_block}" />

			<set name="rx_genre"><![CDATA["name":"([^"]+)"]]></set>
			<parse name="genres" input="${genre_block}" regex="${rx_genre}" />

			<!-- BUILD -->
			<set name="genres_tmp" value="|" />

			<loop name="currGenre" on="genres" limit="30">
				<set name="genres_tmp" value="${genres_tmp}${currGenre[0]:htmldecode}|" />
			</loop>

			<!-- APPLY -->
			<set name="movie.genres" value="${genres_tmp}" />			
		</if>

		<!-- ========================================================= -->
		<!-- CAST -->
		<!-- ========================================================= -->

		<if test="${movie.actors}="><set name="update_actors" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_actors" value="true" /></if>
		<if test="${update_actors}=true">
			<set name="rx_cast_block"><![CDATA["cast":\[(.+?)\]\,"crew"]]></set>
			<parse name="cast_block" input="${details_page}" regex="${rx_cast_block}" />

			<set name="rx_actor"><![CDATA["name":"([^"]+)"]]></set>
			<parse name="actors" input="${cast_block}" regex="${rx_actor}" />

			<!-- BUILD -->
			<set name="actors_tmp" value="|" />

			<loop name="currActor" on="actors" limit="30">
				<replace name="actor_clean" input="${currActor[0]:htmldecode}" pattern="\\u0027" with="'" />
				<set name="actors_tmp" value="${actors_tmp}${actor_clean}|" />
			</loop>

			<!-- APPLY -->
			<set name="movie.actors" value="${actors_tmp}" />
		</if>

		<!-- ========================================================= -->
		<!-- STUDIOS -->
		<!-- ========================================================= -->

		<if test="${movie.studios}="><set name="update_studios" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_studios" value="true" /></if>
		<if test="${update_studios}=true">
			<set name="rx_studio_block"><![CDATA["production_companies":\[(.+?)\]\,]]></set>
			<parse name="studio_block" input="${details_page}" regex="${rx_studio_block}" />

			<set name="rx_studio"><![CDATA["name":"([^"]+)"]]></set>
			<parse name="studios" input="${studio_block}" regex="${rx_studio}" />

			<!-- BUILD -->
			<set name="studios_tmp" value="|" />

			<loop name="currStudio" on="studios" limit="10">
				<set name="studios_tmp" value="${studios_tmp}${currStudio[0]:htmldecode}|" />
			</loop>

			<!-- APPLY -->
			<set name="movie.studios" value="${studios_tmp}" />
		</if>

		<!-- ========================================================= -->
		<!-- LANGUAGE -->
		<!-- ========================================================= -->

		<if test="${movie.language}="><set name="update_language" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_language" value="true" /></if>
		<if test="${update_language}=true">
			<set name="rx_lang"><![CDATA["original_language":"([^"]+)"]]></set>
			<parse name="language" input="${details_page}" regex="${rx_lang}" />
			<set name="movie.language" value="${language[0][0]}" />
		</if>

		<!-- ========================================================= -->
		<!-- ALTERNATIVE TITLES -->
		<!-- ========================================================= -->

		<if test="${movie.alternate_titles}="><set name="update_alternate_titles" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_alternate_titles" value="true" /></if>
		<if test="${update_alternate_titles}=true">
			<set name="rx_alt_title"><![CDATA[\{[^}]*"iso_3166_1":"US"[^}]*"title":"([^"]+)"[^}]*\}]]></set>
			<parse name="alt_titles" input="${details_page}" regex="${rx_alt_title}" />

			<!-- BUILD -->
			<set name="aka_tmp" value="|" />

			<loop name="currAka" on="alt_titles" limit="50">
				<set name="aka" value="${currAka[0]:htmldecode}" />

				<if test="${aka}!=">
					<if test="${aka}!=${title_combined}">
						<set name="aka_tmp" value="${aka_tmp}${aka}|" />
					</if>
				</if>
			</loop>

			<!-- APPLY -->
			<set name="movie.alternate_titles" value="${aka_tmp}" />

			<log LogLevel="Info" Message="ALT TITLES FOUND: ${aka_tmp}" />
		</if>

		<!-- ========================================================= -->
		<!-- RUNTIME -->
		<!-- ========================================================= -->

		<if test="${movie.runtime}="><set name="update_runtime" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_runtime" value="true" /></if>
		<if test="${update_runtime}=true">
			<set name="rx_runtime"><![CDATA["runtime":([0-9]+)]]></set>
			<parse name="runtime" input="${details_page}" regex="${rx_runtime}" />
			<set name="movie.runtime" value="${runtime[0][0]}" />
		</if>

		<!-- ========================================================= -->
		<!-- TAGLINE -->
		<!-- ========================================================= -->

		<if test="${movie.tagline}="><set name="update_tagline" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_tagline" value="true" /></if>
		<if test="${update_tagline}=true">
			<set name="rx_tagline"><![CDATA["tagline":"([^"]*)"]]></set>
			<parse name="tagline" input="${details_page}" regex="${rx_tagline}" />

			<!-- Clean -->
			<replace name="tagline_clean" input="${tagline[0][0]}" pattern="\\u0027|#39;" with="'" />

			<set name="movie.tagline" value="${tagline_clean}" />
		</if>
		
		<!-- ========================================================= -->
		<!-- PLOT KEYWORDS -->
		<!-- ========================================================= -->

		<if test="${movie.plotkeywords}="><set name="update_plotkeywords" value="true" /></if>
		<if test="${global_options_refresh_all_fields}=true"><set name="update_plotkeywords" value="true" /></if>
		<if test="${update_plotkeywords}=true">
			<set name="keywords_url" value="https://api.themoviedb.org/3/movie/${current_tmdb_id}/keywords?api_key=${tmdb_api_key}" />
			<retrieve name="keywords_page" url="${keywords_url}" encoding="UTF-8" />

			<if test="${global_options_debug_imdb_sources}=true">
				<log LogLevel="Debug" Message="TMDb KEYWORDS RESPONSE: ${keywords_page}" />
			</if>

			<set name="rx_kw"><![CDATA["name":"([^"]+)"]]></set>
			<parse name="keywords" input="${keywords_page}" regex="${rx_kw}" />

			<!-- BUILD -->
			<set name="keywords_tmp" value="|" />

			<loop name="currKw" on="keywords" limit="20">
				<set name="kw_clean" value="${currKw[0]:htmldecode}" />

				<if test="${kw_clean}!=">
					<set name="keywords_tmp" value="${keywords_tmp}${kw_clean}|" />
				</if>
			</loop>

			<!-- APPLY -->
			<set name="movie.plotkeywords" value="${keywords_tmp}" />
		</if>
	</if>
</action>

<!-- ========================================================= -->
<!-- POSTER (COVER ART) -->
<!-- ========================================================= -->

<action name="get_cover_art">
	<set name="tmdb_api_key" value="10ab68e4cfcedb66e9e911cd7cfa0546" />

	<if test="${movie.site_id}!=">
		<!-- Get ALL images -->
		<set name="images_url" value="https://api.themoviedb.org/3/movie/${movie.site_id}/images?api_key=${tmdb_api_key}&amp;include_image_language=en,null" />
		<log LogLevel="Info" Message="TMDb IMAGES URL: ${images_url}" />
		<retrieve name="images_page" url="${images_url}" encoding="UTF-8" />

		<!-- 1. Isolate the posters section block -->
		<set name="rx_posters"><![CDATA["posters":\s*\[([\s\S]*?)\]]]></set>
		<parse name="poster_section" input="${images_page}" regex="${rx_posters}" />

		<!-- 2. Parse items ONLY from that block -->
		<set name="rx_poster_items"><![CDATA["file_path":"([^"]+)"]]></set>
		<parse name="poster_list" input="${poster_section}" regex="${rx_poster_items}" />

		<log LogLevel="Debug" Message="POSTER MATCH COUNT: ${poster_list.count}" />

		<!-- Loop posters -->
		<set name="counter" value="0" />
		<if test="${poster_list}!=">
			<loop name="curPoster" on="poster_list" limit="10">
				<set name="poster_url" value="https://image.tmdb.org/t/p/original${curPoster[0]}" />
				<set name="cover_art[${counter}].url" value="${poster_url}" />
				<log LogLevel="Debug" Message="POSTER[${counter}]: ${poster_url}" />
				<add name="counter" value1="${counter}" value2="1" />
			</loop>
		</if>

		<if test="${poster_list}=">
			<log LogLevel="Debug" Message="No posters found (non-fatal)" />
		</if>
	</if>
</action>

<!-- ========================================================= -->
<!-- BACKDROP (FANART) -->
<!-- ========================================================= -->

<action name="get_backdrop">
	<set name="tmdb_api_key" value="10ab68e4cfcedb66e9e911cd7cfa0546" />

	<if test="${movie.site_id}!=">
		<!-- Get ALL images -->
		<set name="images_url" value="https://api.themoviedb.org/3/movie/${movie.site_id}/images?api_key=${tmdb_api_key}&amp;include_image_language=en,null" />
		<log LogLevel="Info" Message="TMDb IMAGES URL: ${images_url}" />
		<retrieve name="images_page" url="${images_url}" encoding="UTF-8" />

		<!-- 1. Isolate the backdrop section block -->
		<set name="rx_backdrops"><![CDATA["backdrops":\s*\[([\s\S]*?)\]]]></set>
		<parse name="backdrop_section" input="${images_page}" regex="${rx_backdrops}" />

		<!-- 2. Parse items ONLY from that block [0][1] is the first capture group -->
		<set name="rx_backdrop_items"><![CDATA["file_path":"([^"]+)"]]></set>
		<parse name="backdrop_list" input="${backdrop_section}" regex="${rx_backdrop_items}" />

		<log LogLevel="Debug" Message="BACKDROP MATCH COUNT: ${backdrop_list.count}" />

		<!-- Loop backdrops -->
		<set name="counter" value="0" />
		<if test="${backdrop_list}!=">
			<loop name="curBackdrop" on="backdrop_list" limit="10">
				<set name="backdrop_url" value="https://image.tmdb.org/t/p/original${curBackdrop[0]}" />
				<set name="backdrop[${counter}].url" value="${backdrop_url}" />
				<log LogLevel="Debug" Message="BACKDROP[${counter}]: ${backdrop_url}" />
				<add name="counter" value1="${counter}" value2="1" />
			</loop>
		</if>

		<if test="${backdrop_list}=">
			<log LogLevel="Debug" Message="No backdrops found (non-fatal)" />
		</if>
	</if>
</action>

</ScriptableScraper>