diff --git a/README.md b/README.md index 84d9ea41..256ad296 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ StashDB |[StashDB Submission Helper](/userscripts/StashDB_Submission_Helper)|Ad ## Utility Scripts -Category|Plugin Name|Description|Minimum Stash version ---------|-----------|-----------|--------------------- -Kodi|[Kodi Helper](scripts/kodi-helper)|Generates `nfo` and `strm` for use with Kodi.|v0.7 -Maintenance|[Stash Sqlite Renamer](scripts/Sqlite_Renamer)|Renames your files using stash's metadata.|v0.7 +Category| Plugin Name | Description |Minimum Stash version +--------|------------------------------------------------|----------------------------------------------------------------------------------------|--------------------- +Kodi| [Kodi Helper](scripts/kodi-helper) | Generates `nfo` and `strm` for use with Kodi. |v0.7 +Maintenance| [Stash Sqlite Renamer](scripts/Sqlite_Renamer) | Renames your files using stash's metadata. |v0.7 +Maintenance| [Title Diff](plugins/titleDiff) | Tag scenes where title differs from filename|v0.7 diff --git a/plugins/titleDiff/README.md b/plugins/titleDiff/README.md new file mode 100644 index 00000000..044c1dfd --- /dev/null +++ b/plugins/titleDiff/README.md @@ -0,0 +1,7 @@ +# Title Diff +Compare scene title to filename (with and without extension) and if they differ tag the scene with a special tag. +The tag will be created if not found. + +# Configuration +Set `TAG_NAME` in `titleDiff.js` to change name of the tag used to tag scenes. +Defaults to `_titleDiff` \ No newline at end of file diff --git a/plugins/titleDiff/titleDiff.js b/plugins/titleDiff/titleDiff.js new file mode 100644 index 00000000..ecdbff1d --- /dev/null +++ b/plugins/titleDiff/titleDiff.js @@ -0,0 +1,73 @@ +var TAG_NAME = "_titleDiff" // Name of tag to set when scene title differs from filename, will create if not found + + +function main() { + switch (input.Args.mode) { + case "diff": + diff() + } +} + +function diff() { + log.Info("diff: Begin diff title/filename") + var sceneIds = findDiffingScenes() + var tagId = getOrCreateTag() + var n = setTag(sceneIds, [tagId]) + + log.Info("diff: Number of scenes found with differing title/filename: " + n) +} + +function findDiffingScenes() { + var query = "{findScenes(filter:{per_page:-1}){scenes{id title path}}}" + var result = gql.Do(query) + if (result.findScenes == null) { + log.Info("diff: No scenes found") + return + } + var scenes = result.findScenes.scenes + var diffs = [] + for (var i = 0; i < scenes.length; i++) { + var filename = scenes[i].path.split(/.*[\/|\\]/)[1]; + var filenameNoExt = filename.replace(/\.[^/.]+$/, "") + if (scenes[i].title !== filename && scenes[i].title !== filenameNoExt) { + log.Debug("diff: Id=" + scenes[i].id + " Title='" + scenes[i].title + "' Filename='" + filename + "'") + diffs.push(scenes[i].id) + } + } + return diffs +} + +function getOrCreateTag() { + var query = "query FindTag($name: String!){\ + findTags(tag_filter:{name:{value:$name, modifier:EQUALS}}){tags{id}}}" + + var result = gql.Do(query, {name: TAG_NAME}) + if (result.findTags == null) { + log.Warn("diff: No tags?") + return + } + if (result.findTags.tags.length === 0) { + log.Debug("diff: Tag not found, creating...") + return createTag() + } else { + return result.findTags.tags[0].id + } +} + +function createTag() { + var query = "mutation CreateTag($name: String!){tagCreate(input:{name:$name}){id}}" + var result = gql.Do(query, {name: TAG_NAME}) + log.Debug("diff: Tag created Id='" + result.tagCreate.id + "' name='" + TAG_NAME + "'") + return result.tagCreate.id +} + +function setTag(sceneIds, tagIds) { + var query = "mutation BulkSceneSetTag($scene_ids: [ID!], $tag_ids: [ID!]){\ + bulkSceneUpdate(input:{ids: $scene_ids, tag_ids:{ids: $tag_ids, mode:ADD}}){id}}" + + var result = gql.Do(query, {scene_ids: sceneIds, tag_ids: tagIds}) + log.Debug(result) + return result.bulkSceneUpdate.length +} + +main() \ No newline at end of file diff --git a/plugins/titleDiff/titleDiff.yml b/plugins/titleDiff/titleDiff.yml new file mode 100644 index 00000000..c1807ef2 --- /dev/null +++ b/plugins/titleDiff/titleDiff.yml @@ -0,0 +1,12 @@ +name: titleDiff +description: Tag Scenes where scene title differs from filename +url: https://github.com/stashapp/CommunityScripts +version: 1.0 +exec: + - titleDiff.js +interface: js +tasks: + - name: Run diff + description: 'Tag scenes where title differs from filename' + defaultArgs: + mode: diff