Skip to content

Commit

Permalink
Support alt names for json input
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Sep 12, 2022
1 parent 5640163 commit ff1c526
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "touhou-tagger",
"version": "1.5.1",
"version": "1.5.2",
"description": "从 THBWiki 自动填写东方 Project CD 曲目信息.",
"main": "dist/core/index.js",
"bin": {
Expand Down Expand Up @@ -32,7 +32,6 @@
"axios": "^0.27.2",
"flac-metadata": "^0.1.1",
"fuse.js": "^6.6.2",
"iconv-lite": "0.6.3",
"image-type": "^5.0.0",
"imageinfo": "^1.0.4",
"jsdom": "^20.0.0",
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
27 changes: 27 additions & 0 deletions src/core/metadata/local-json/alt-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { altNames } from '../alt-names'
import { LocalJsonPlugin } from './local-json'

/** 使用 alt-names 的数据进行替换 */
export const altNamesPlugin: LocalJsonPlugin = () => {
const altNameFields = [
'artists',
'lyricists',
'composers',
]
return ({ metadata }) => {
altNameFields.forEach(field => {
if (!altNameFields.includes(field) || !Array.isArray(metadata[field])) {
return
}
const values = metadata[field] as string[]
const replaceValues = values.map(v => {
const replaceValue = altNames.get(v)
if (replaceValue !== undefined) {
return replaceValue
}
return v
})
metadata[field] = replaceValues
})
}
}
3 changes: 2 additions & 1 deletion src/core/metadata/local-json/local-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { commonFieldsPlugin } from './common-fields'
import { MetadataConfig } from '../../core-config'
import { fetchCoverPlugin } from './cover'
import { omitArtistsPlugin } from './omit-artists'
import { altNamesPlugin } from './alt-names'

export type LocalJsonPlugin = (init: {
cover?: Buffer
Expand All @@ -16,7 +17,7 @@ export type LocalJsonPlugin = (init: {
index: number
}) => void | Promise<void>

const plugins = [fetchCoverPlugin, omitArtistsPlugin, inferNumberPlugin, commonFieldsPlugin]
const plugins = [fetchCoverPlugin, omitArtistsPlugin, inferNumberPlugin, commonFieldsPlugin, altNamesPlugin]
export class LocalJson extends MetadataSource {
async normalize(metadatas: Metadata[], cover?: Buffer) {
if (!metadatas || metadatas.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/metadata/thb-wiki/thb-wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Axios from 'axios'
import { JSDOM } from 'jsdom'
import { log } from '../../debug'
import { MetadataConfig } from '../../core-config'
import { altNames } from './alt-names'
import { altNames } from '../alt-names'

type TrackParseInfo = { name: string, result: string | string[] }

Expand Down

0 comments on commit ff1c526

Please sign in to comment.