2024-08-25 00:51:16 +00:00
|
|
|
import { format } from 'date-fns';
|
|
|
|
import { parse } from 'yaml';
|
|
|
|
|
|
|
|
import slugify from '#/utils/slugify.js';
|
2024-08-26 04:15:22 +00:00
|
|
|
import ellipsis from '#/utils/ellipsis.js';
|
2024-08-25 00:51:16 +00:00
|
|
|
|
|
|
|
const genderMap = {
|
|
|
|
f: 'female',
|
|
|
|
m: 'male',
|
|
|
|
t: 'transsexual',
|
|
|
|
o: 'other',
|
|
|
|
u: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const propProcessors = {
|
|
|
|
channel: (sceneInfo) => sceneInfo.channel?.name || sceneInfo.network?.name,
|
|
|
|
network: (sceneInfo) => sceneInfo.network?.name || sceneInfo.channel?.name,
|
|
|
|
actors: (sceneInfo, options) => {
|
|
|
|
const genders = (options.genders || 'fmtou').split('').map((genderKey) => genderMap[genderKey]);
|
|
|
|
|
|
|
|
return sceneInfo.actors
|
|
|
|
.filter((actor) => genders.includes(actor.gender))
|
|
|
|
.map((actor) => actor.name);
|
|
|
|
},
|
|
|
|
tags: (sceneInfo, options) => sceneInfo.tags
|
|
|
|
.filter((tag) => {
|
2024-08-26 04:15:22 +00:00
|
|
|
if (options.include && !options.include.includes(tag.slug)) {
|
2024-08-25 00:51:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
if (options.exclude?.includes(tag.slug)) {
|
2024-08-25 00:51:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.priority && tag.priority < options.priority) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.map((tag) => tag.name),
|
2024-09-01 00:54:03 +00:00
|
|
|
movie: (sceneInfo) => sceneInfo.movies?.[0]?.title,
|
|
|
|
date: (sceneInfo, options) => (sceneInfo.effectiveDate ? format(sceneInfo.effectiveDate, options.format || 'yyyy-MM-dd') : ''),
|
2024-08-25 00:51:16 +00:00
|
|
|
};
|
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
function curateValue(value, item) {
|
|
|
|
return [].concat(value) // account for both arrays (actors, tags) and strings (title, channel)
|
|
|
|
.slice(0, item.limit || Infinity)
|
|
|
|
.map((listValue) => (item.slugify ? slugify(listValue, item.slugify) : listValue))
|
|
|
|
.map((listValue) => ellipsis(listValue, item.slice || Infinity, item.ellipsis || ''))
|
2024-08-29 00:19:57 +00:00
|
|
|
.map((listValue) => `${item.wrap?.[0] || ''}${listValue}${item.wrap?.[1] || ''}`)
|
2024-08-26 04:15:22 +00:00
|
|
|
.join(item.delimit || ', ');
|
|
|
|
}
|
|
|
|
|
2024-08-29 00:19:57 +00:00
|
|
|
function traverseTemplate(chain, release, { delimit = ' ' } = {}) {
|
2024-08-25 00:51:16 +00:00
|
|
|
const results = chain.reduce((result, item) => {
|
2024-08-26 04:15:22 +00:00
|
|
|
const keys = typeof item === 'string' ? item : item.key;
|
|
|
|
|
|
|
|
if ((item.channels && !item.channels.includes(release.channel?.slug)) || item.notChannels?.includes(release.channel?.slug)) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-08-25 00:51:16 +00:00
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
if ((item.networks && !item.networks.includes(release.network?.slug)) || item.notNetworks?.includes(release.network?.slug)) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-08-25 00:51:16 +00:00
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
if (keys) {
|
|
|
|
const value = keys.split('|').reduce((acc, key) => acc
|
|
|
|
|| propProcessors[key]?.(release, typeof item === 'string' ? { key } : item)
|
|
|
|
|| release[key], null);
|
2024-08-25 00:51:16 +00:00
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
return result.concat(curateValue(value, item));
|
2024-08-25 00:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.items) {
|
2024-08-26 04:15:22 +00:00
|
|
|
const group = traverseTemplate(item.items, release, {
|
|
|
|
delimit: item.delimit,
|
|
|
|
});
|
2024-08-25 00:51:16 +00:00
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
return result.concat(curateValue(group, item));
|
2024-08-25 00:51:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 04:15:22 +00:00
|
|
|
return result;
|
2024-08-25 00:51:16 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (results.length > 0) {
|
2024-08-29 00:19:57 +00:00
|
|
|
// return `${wrap[0] || ''}${results.filter(Boolean).join(delimit)}${wrap[1] || ''}`;
|
|
|
|
return results.filter(Boolean).join(delimit);
|
2024-08-25 00:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2024-08-26 04:15:22 +00:00
|
|
|
|
|
|
|
export default function processSummaryTemplate(template, release) {
|
|
|
|
const chain = parse(template);
|
|
|
|
|
|
|
|
return traverseTemplate(chain, release);
|
|
|
|
}
|