Skip to content

Commit

Permalink
fix: batch resolving media
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Aug 5, 2024
1 parent c07ca0f commit 5a6ea08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions common/modules/anilist.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,19 @@ class AnilistClient {
// isAdult doesn't need an extra variable, as the title is the same regardless of type, so we re-use the same variable for adult and non-adult requests
/** @type {Record<`v${number}`, string>} */
const requestVariables = flattenedTitles.reduce((obj, { title, isAdult }, i) => {
if (isAdult) return obj
if (isAdult && i !== 0) return obj
obj[`v${i}`] = title
return obj
}, {})

const queryVariables = flattenedTitles.reduce((arr, { isAdult }, i) => {
if (isAdult) return arr
if (isAdult && i !== 0) return arr
arr.push(`$v${i}: String`)
return arr
}, []).join(', ')
const fragmentQueries = flattenedTitles.map(({ year, isAdult }, i) => /* js */`
v${i}: Page(perPage: 10) {
media(type: ANIME, search: $v${isAdult ? i - 1 : i}, status_in: [RELEASING, FINISHED], isAdult: ${!!isAdult} ${year ? `, seasonYear: ${year}` : ''}) {
media(type: ANIME, search: $v${(isAdult && i !== 0) ? i - 1 : i}, status_in: [RELEASING, FINISHED], isAdult: ${!!isAdult} ${year ? `, seasonYear: ${year}` : ''}) {
...med
}
}`)
Expand Down
33 changes: 18 additions & 15 deletions common/modules/animeresolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ export default new class AnimeResolver {
* @returns {string[]}
*/
alternativeTitles (title) {
const titles = []
const titles = new Set()

let modified = title
// preemptively change S2 into Season 2 or 2nd Season, otherwise this will have accuracy issues
const seasonMatch = title.match(/ S(\d+)/)
if (seasonMatch) {
if (Number(seasonMatch[1]) === 1) { // if this is S1, remove the " S1" or " S01"
modified = title.replace(/ S(\d+)/, '')
titles.push(modified)
titles.add(modified)
} else {
modified = title.replace(/ S(\d+)/, ` ${Number(seasonMatch[1])}${postfix[Number(seasonMatch[1])] || 'th'} Season`)
titles.push(modified)
titles.push(title.replace(/ S(\d+)/, ` Season ${Number(seasonMatch[1])}`))
titles.add(modified)
titles.add(title.replace(/ S(\d+)/, ` Season ${Number(seasonMatch[1])}`))
}
} else {
titles.push(title)
titles.add(title)
}

// remove - :
const specialMatch = modified.match(/[-:]/g)
if (specialMatch) {
modified = modified.replace(/[-:]/g, '')
titles.push(modified)
modified = modified.replace(/[-:]/g, '').replace(/[ ]{2,}/, ' ')
titles.add(modified)
}

// remove (TV)
const tvMatch = modified.match(/\(TV\)/)
if (tvMatch) {
modified = modified.replace('(TV)', '')
titles.push(modified)
titles.add(modified)
}

return titles
return [...titles]
}

/**
Expand All @@ -73,8 +73,10 @@ export default new class AnimeResolver {
return titleObjects
}).flat()

for (const [key, media] of await anilistClient.alSearchCompound(titleObjects)) {
this.animeNameCache[key] = media
for (const chunk of chunks(titleObjects, 62)) { // single title has a complexity of 8.1, al limits complexity to 500
for (const [key, media] of await anilistClient.alSearchCompound(chunk)) {
this.animeNameCache[key] = media
}
}
}

Expand All @@ -97,16 +99,17 @@ export default new class AnimeResolver {
if (!fileName) return [{}]
const parseObjs = await anitomyscript(fileName)

const TYPE_EXCLUSIONS = ['ED', 'ENDING', 'NCED', 'NCOP', 'OP', 'OPENING', 'PREVIEW', 'PV']

/** @type {Record<string, import('anitomyscript').AnitomyResult>} */
const uniq = {}
for (const obj of parseObjs) {
const key = this.getCacheKeyForTitle(obj)
if (key in this.animeNameCache) continue
if (key in this.animeNameCache) continue // skip already resolved
if (obj.anime_type && TYPE_EXCLUSIONS.includes(obj.anime_type.toUpperCase())) continue // skip non-episode media
uniq[key] = obj
}
for (const chunk of chunks(Object.values(uniq), 50)) {
await this.findAnimesByTitle(chunk)
}
await this.findAnimesByTitle(Object.values(uniq))

const fileAnimes = []
for (const parseObj of parseObjs) {
Expand Down

0 comments on commit 5a6ea08

Please sign in to comment.