Skip to content

Commit

Permalink
[RenameFile] Fixed bug associated with rename (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Maisonave authored Jul 23, 2024
1 parent 962771a commit 495c14a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
6 changes: 4 additions & 2 deletions plugins/RenameFile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ To avoid this error, refresh the URL before changing the Title field.
### Installation
- Follow **Requirements** instructions.
- In the stash plugin directory (C:\Users\MyUserName\.stash\plugins), create a folder named **RenameFile**.
- Copy all the plugin files to this folder.(**C:\Users\MyUserName\.stash\plugins\RenameFile**).
- Copy all the plugin files to this folder.(**C:\Users\MyUserName\\.stash\plugins\RenameFile**).
- Restart Stash.

That's it!!!

### Options
To change options, see **renamefile_settings.py** file. After making changes, go to http://localhost:9999/settings?tab=plugins, and click [Reload Plugins].
- Main options are accessible in the GUI via Settings->Plugins->Plugins->[RenameFile].
- Advanced options are avialable in the **renamefile_settings.py** file. After making changes, go to http://localhost:9999/settings?tab=plugins, and click [Reload Plugins].

99 changes: 63 additions & 36 deletions plugins/RenameFile/renamefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
DEFAULT_ENDPOINT = "http://localhost:9999/graphql" # Default GraphQL endpoint
DEFAULT_FIELD_KEY_LIST = "title, performers, tags" # Default Field Key List with the desired order
DEFAULT_SEPERATOR = "-"
PLUGIN_ARGS = False



# ------------------------------------------
# ------------------------------------------
Expand Down Expand Up @@ -58,7 +61,11 @@
# Extract dry_run setting from settings
dry_run = settings["dryRun"]
dry_run_prefix = ''
logger.info(f"\nStarting (debugTracing={debugTracing}) (dry_run={dry_run})************************************************")
try:
PLUGIN_ARGS = json_input['args']["mode"]
except:
pass
logger.info(f"\nStarting (debugTracing={debugTracing}) (dry_run={dry_run}) (PLUGIN_ARGS={PLUGIN_ARGS})************************************************")
if debugTracing: logger.info("settings: %s " % (settings,))
if dry_run:
logger.info("Dry run mode is enabled.")
Expand Down Expand Up @@ -96,6 +103,7 @@
double_separator = separator + separator



# GraphQL query to fetch all scenes
query_all_scenes = """
query AllScenes {
Expand Down Expand Up @@ -152,7 +160,7 @@ def form_filename(original_file_stem, scene_details, wrapper_styles):
title = default_title
# ...................

if debugTracing: logger.info("Debug Tracing................")
if debugTracing: logger.info(f"Debug Tracing (title=\"{title}\")................")

# Function to add tag to filename
def add_tag(tag_name):
Expand Down Expand Up @@ -195,7 +203,9 @@ def add_tag(tag_name):
if settings["performerAppend"]:
performers = '-'.join([performer.get('name', '') for performer in scene_details.get('performers', [])])
if performers:
if not include_performer_if_in_name or performers.lower() not in title.lower():
if debugTracing: logger.info(f"Debug Tracing (include_performer_if_in_name={include_performer_if_in_name})................")
if include_performer_if_in_name or performers.lower() not in title.lower():
if debugTracing: logger.info(f"Debug Tracing (performers={performers})................")
if wrapper_styles.get('performers'):
filename_parts.append(f"{wrapper_styles['performers'][0]}{performers}{wrapper_styles['performers'][1]}")
else:
Expand Down Expand Up @@ -237,7 +247,7 @@ def add_tag(tag_name):
if debugTracing: logger.info(f"Debug Tracing (include_tag_if_in_name={include_tag_if_in_name})................")
if include_tag_if_in_name or tag_name.lower() not in title.lower():
add_tag(tag_name)
if debugTracing: logger.info("Debug Tracing................")
if debugTracing: logger.info(f"Debug Tracing (tag_name={tag_name})................")

new_filename = separator.join(filename_parts).replace(double_separator, separator)

Expand Down Expand Up @@ -408,49 +418,66 @@ def rename_scene(scene_id, wrapper_styles, stash_directory):

return new_filename, original_path_info, new_path_info

if debugTracing: logger.info("Debug Tracing................")
# Execute the GraphQL query to fetch all scenes
scene_result = graphql_request(query_all_scenes)
if debugTracing: logger.info("Debug Tracing................")
all_scenes = scene_result.get('data', {}).get('allScenes', [])
if debugTracing: logger.info("Debug Tracing................")
if not all_scenes:
# Main default function for rename scene
def rename_files_task():
if debugTracing: logger.info("Debug Tracing................")
# Execute the GraphQL query to fetch all scenes
scene_result = graphql_request(query_all_scenes)
if debugTracing: logger.info("Debug Tracing................")
all_scenes = scene_result.get('data', {}).get('allScenes', [])
if debugTracing: logger.info("Debug Tracing................")
if not all_scenes:
if debugTracing: logger.info("Debug Tracing................")
log.error("No scenes found.")
logger.error("No scenes found.")
exit()
if debugTracing: logger.info("Debug Tracing................")
log.error("No scenes found.")
logger.error("No scenes found.")
exit()
if debugTracing: logger.info("Debug Tracing................")

# Find the scene with the latest updated_at timestamp
latest_scene = max(all_scenes, key=lambda scene: scene['updated_at'])
# Find the scene with the latest updated_at timestamp
latest_scene = max(all_scenes, key=lambda scene: scene['updated_at'])

# Extract the ID of the latest scene
latest_scene_id = latest_scene.get('id')
# Extract the ID of the latest scene
latest_scene_id = latest_scene.get('id')


# Extract wrapper styles
wrapper_styles = config["wrapper_styles"]
# Extract wrapper styles
wrapper_styles = config["wrapper_styles"]

# Read stash directory from renamefile_settings.py
stash_directory = config.get('stash_directory', '')
if debugTracing: logger.info("Debug Tracing................")
# Read stash directory from renamefile_settings.py
stash_directory = config.get('stash_directory', '')
if debugTracing: logger.info("Debug Tracing................")

if debugTracing: logger.info("Debug Tracing................")
if debugTracing: logger.info("Debug Tracing................")

# Rename the latest scene and trigger metadata scan
new_filename = rename_scene(latest_scene_id, wrapper_styles, stash_directory)
if debugTracing: logger.info("Debug Tracing................")
# Rename the latest scene and trigger metadata scan
new_filename = rename_scene(latest_scene_id, wrapper_styles, stash_directory)
if debugTracing: logger.info("Debug Tracing................")

# Log dry run state and indicate if no changes were made
if dry_run:
log.info("Dry run: Script executed in dry run mode. No changes were made.")
logger.info("Dry run: Script executed in dry run mode. No changes were made.")
elif not new_filename:
logger.info("No changes were made.")
# Log dry run state and indicate if no changes were made
if dry_run:
log.info("Dry run: Script executed in dry run mode. No changes were made.")
logger.info("Dry run: Script executed in dry run mode. No changes were made.")
elif not new_filename:
logger.info("No changes were made.")
else:
logger.info("Change success!")
return

def fetch_dup_filename_tags(): # Place holder for new implementation
return

if PLUGIN_ARGS == "fetch_dup_filename_tags":
fetch_dup_filename_tags()
elif PLUGIN_ARGS == "rename_files_task":
rename_files_task()
else:
logger.info("Change success!")
rename_files_task()

if debugTracing: logger.info("\n*********************************\nEXITING ***********************\n*********************************")


# ToDo List
# Add logic to max_filename_length code so it checks base file length and checks folder length, instead of lumping them altogether.
# Add logic to update Sqlite DB on file name change, instead of perform_metadata_scan.
# Get variables from the Plugins Settings UI instead of from renamefile_settings.py
# Get variables from the Plugins Settings UI instead of from renamefile_settings.py
# Add code to get tags from duplicate filenames
2 changes: 1 addition & 1 deletion plugins/RenameFile/renamefile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: RenameFile
description: Renames video (scene) file names when the user edits the [Title] field located in the scene [Edit] tab.
version: 0.2.5
version: 0.2.6
url: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/RenameFile
settings:
dryRun:
Expand Down

0 comments on commit 495c14a

Please sign in to comment.