Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

titleDiff plugin #77

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions plugins/titleDiff/README.md
Original file line number Diff line number Diff line change
@@ -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`
73 changes: 73 additions & 0 deletions plugins/titleDiff/titleDiff.js
Original file line number Diff line number Diff line change
@@ -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(/\.[^/.]+$/, "")
Comment on lines +30 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an extra check for future proofing here?
With the stashapp/stash#3006 PR there can be scenes with no files. For those the path is empty and thus you get Plugin returned error: TypeError: Cannot access member 'replace' of undefined and the plugin exits. For those cases i think just skipping the scene is ok

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()
12 changes: 12 additions & 0 deletions plugins/titleDiff/titleDiff.yml
Original file line number Diff line number Diff line change
@@ -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