Skip to content

Commit 07fa418

Browse files
committed
[ethnicity-tag] new plugin
1 parent 3ae2774 commit 07fa418

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ custom-filepath
1717
edit-unorganized
1818
- Jumps to a chosen tab when an unorganized (or organized) scene is opened
1919

20+
ethnicity-tag
21+
- Adds ethnicity tags to performers, similar to [nationality-tag](plugins/nationality-tag/)
22+
2023
log-console
2124
- log-toast but to console instead of toasts
2225

plugins/ethnicity-tag/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ethnicity-tag
2+
3+
Tags performers with ethnicity tags
4+
5+
Similar to [nationality-tag](../nationality-tag/) but with ethnicity
6+
7+
Ethnicity manually mapped to stashdb dropdown
8+
9+
https://github.com/stashapp/stash-box/blob/master/frontend/src/pages/performers/performerForm/PerformerForm.tsx#L96-L106
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const ok = () => ({ output: "ok" })
2+
3+
function main() {
4+
log.Debug("Running ethnicity-tag plugin")
5+
const mode = input.Args.mode
6+
if (!mode) {
7+
// just return
8+
return ok()
9+
}
10+
getPerformers()
11+
}
12+
13+
// helper functions
14+
function findTag(tagname) {
15+
const result = gql.Do(`
16+
query ($tagname: String!) {
17+
findTags(tag_filter: {
18+
name: { value: $tagname, modifier: EQUALS }
19+
OR: { aliases: { value: $tagname, modifier: EQUALS } }
20+
}) {
21+
tags { id }
22+
}
23+
}`, { tagname })
24+
if (result.findTags.tags.length == 1) {
25+
log.Debug(`Found tag ${tagname}`)
26+
return result.findTags.tags[0].id
27+
} else {
28+
return null
29+
}
30+
}
31+
32+
const createChildTag = (tagname, parentid) =>
33+
gql.Do(`
34+
mutation ($tagname: String!, $parentid: ID!) {
35+
tagCreate(
36+
input: {
37+
name: $tagname,
38+
parent_ids: [$parentid],
39+
ignore_auto_tag: true
40+
}) { id }
41+
}`, {
42+
tagname,
43+
parentid
44+
}).tagCreate.id
45+
46+
const createTag = (tagname) =>
47+
gql.Do(`
48+
mutation ($tagname: String!) {
49+
tagCreate(
50+
input: { name: $tagname, ignore_auto_tag: true }
51+
) { id }
52+
}`, { tagname }).tagCreate.id
53+
54+
function findAddTag(ethnicity) {
55+
// look for tag
56+
const tag = findTag(ethnicity)
57+
return tag ? tag : createChildTag(ethnicity, parentTagID)
58+
}
59+
60+
function addTag(performerID, tagID) {
61+
const oldtags = gql.Do(`
62+
query ($id: ID!) {
63+
findPerformer(id: $id) {
64+
tags { id }}}`, {
65+
id: performerID
66+
}).findPerformer.tags
67+
.map(tag => tag.id)
68+
gql.Do(`
69+
mutation ($performerID: ID!, $newtags: [ID!]) {
70+
performerUpdate(
71+
input: { id: $performerID, tag_ids: $newtags }
72+
) { id }}`,
73+
{
74+
performerID,
75+
newtags: oldtags.concat(tagID)
76+
})
77+
}
78+
79+
// constants
80+
const PARENT_TAG_NAME = "ethnicity-tag"
81+
82+
// get parent tag
83+
let parentTagID = findTag(PARENT_TAG_NAME)
84+
if (!parentTagID) {
85+
log.Info("Parent tag not found")
86+
createTag(PARENT_TAG_NAME)
87+
// set parentTagID
88+
parentTagID = findTag(PARENT_TAG_NAME)
89+
}
90+
91+
// iterate over performer
92+
const getPerformers = () => {
93+
const results = gql.Do(`
94+
query ($exid: [ID!]) {
95+
findPerformers(
96+
filter: { per_page: -1 }
97+
performer_filter: {
98+
tags: {
99+
excludes: $exid,
100+
modifier: INCLUDES_ALL,
101+
depth: -1,
102+
value: [] }
103+
ethnicity: { modifier: NOT_NULL, value: "" }}) {
104+
performers {
105+
id ethnicity
106+
}}}`, {
107+
exid: [parentTagID]
108+
})
109+
const performers = results.findPerformers.performers
110+
const count = performers.length
111+
log.Debug(`Tagging ${count} performers`)
112+
for (let i=0; i < performers.length; i++) {
113+
const performer = performers[i]
114+
log.Progress(i/count)
115+
setPerformer(performer.id, performer.ethnicity)
116+
}
117+
}
118+
119+
// manual map to stashdb dropdown, please open an issue if a new tag is created
120+
// https://github.com/stashapp/stash-box/blob/master/frontend/src/pages/performers/performerForm/PerformerForm.tsx#L96-L106
121+
const ethMap = [
122+
'Caucasian',
123+
'Black',
124+
'Asian',
125+
'Indian',
126+
'Latin',
127+
'Middle Eastern',
128+
'Mixed',
129+
'Other'
130+
]
131+
132+
// get performer
133+
function setPerformer(id, ethnicity) {
134+
log.Debug(`Trying to tag performer: ${id}`)
135+
if (!ethnicity || !ethMap.includes(ethnicity)) {
136+
log.Error(`Performer ${id} has no matching ethnicity ${ethnicity}`)
137+
return
138+
}
139+
// find or add ethTag
140+
const ethTag = findAddTag(ethnicity)
141+
// add ethTag to performer
142+
addTag(id, ethTag)
143+
log.Info(`Added tag ${ethnicity} to performer ${id}`)
144+
}
145+
main()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: ethnicity-tag
2+
description: Tag performers with ethnicities
3+
version: 0.1.0
4+
exec:
5+
- ethnicity-tag.js
6+
interface: js
7+
tasks:
8+
- name: Create Ethnicity Tags
9+
description: Create Ethnicity tags
10+
defaultArgs:
11+
mode: create

0 commit comments

Comments
 (0)