Skip to content

Commit

Permalink
Improve multiple disc support
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Dec 11, 2019
1 parent feb20fe commit 8bcbff7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
15 changes: 13 additions & 2 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ const getMetadata = async (album) => {
const metadata = await sourceMappings[cliOptions.source].getMetadata(album);
console.log('创建文件中...');
const { readdirSync, renameSync } = await Promise.resolve().then(() => require('fs'));
const { dirname } = await Promise.resolve().then(() => require('path'));
const { writerMappings } = await Promise.resolve().then(() => require('../core/writer/writer-mappings'));
const fileTypes = Object.keys(writerMappings);
const files = readdirSync('.').filter(file => fileTypes.some(type => file.endsWith(type))).slice(0, metadata.length);
const fileTypeFilter = (file) => fileTypes.some(type => file.endsWith(type));
const dir = readdirSync('.');
const discFiles = dir.filter(f => f.match(/^Disc (\d+)/)).flatMap(f => readdirSync(f).map(inner => `${f}/${inner}`));
const files = dir.filter(fileTypeFilter).concat(discFiles).slice(0, metadata.length);
if (files.length === 0) {
console.log('未找到任何支持的音乐文件.');
process.exit();
}
const targetFiles = files.map((file, index) => {
const maxLength = Math.max(Math.trunc(Math.log10(metadata.length)) + 1, 2);
return `${(index + 1).toString().padStart(maxLength, '0')} ${metadata[index].title}${path_1.extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '');
let dir = dirname(file);
if (dir === '.') {
dir = '';
}
else {
dir += '/';
}
return dir + `${metadata[index].trackNumber.padStart(maxLength, '0')} ${metadata[index].title}${path_1.extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '');
});
files.forEach((file, index) => {
renameSync(file, targetFiles[index]);
Expand Down
6 changes: 3 additions & 3 deletions dist/core/metadata/thb-wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ class THBWiki {
}
return {
title,
artists: [...new Set(artists)],
artists: [...new Set(artists)].map(it => it.replace('(人物)', '')),
trackNumber,
comments,
lyricists,
composers,
lyricists: lyricists ? lyricists.map(it => it.replace('(人物)', '')) : lyricists,
composers: composers ? composers.map(it => it.replace('(人物)', '')) : composers,
};
}
async getMetadata(albumName) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "touhou-tagger",
"version": "1.0.3",
"version": "1.0.4",
"description": "从 THBWiki 自动填写东方Project CD曲目信息.",
"main": "dist/core/index.js",
"bin": {
Expand Down
14 changes: 12 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ const getMetadata = async (album: string) => {
const metadata = await sourceMappings[cliOptions.source].getMetadata(album)
console.log('创建文件中...')
const { readdirSync, renameSync } = await import('fs')
const { dirname } = await import('path')
const { writerMappings } = await import('../core/writer/writer-mappings')
const fileTypes = Object.keys(writerMappings)
const files = readdirSync('.').filter(file => fileTypes.some(type => file.endsWith(type))).slice(0, metadata.length)
const fileTypeFilter = (file: string) => fileTypes.some(type => file.endsWith(type))
const dir = readdirSync('.')
const discFiles = dir.filter(f => f.match(/^Disc (\d+)/)).flatMap(f => readdirSync(f).map(inner => `${f}/${inner}`))
const files = dir.filter(fileTypeFilter).concat(discFiles).slice(0, metadata.length)
if (files.length === 0) {
console.log('未找到任何支持的音乐文件.')
process.exit()
}
const targetFiles = files.map((file, index) => {
const maxLength = Math.max(Math.trunc(Math.log10(metadata.length)) + 1, 2)
return `${(index + 1).toString().padStart(maxLength, '0')} ${metadata[index].title}${extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '')
let dir = dirname(file)
if (dir === '.') {
dir = ''
} else {
dir += '/'
}
return dir + `${metadata[index].trackNumber.padStart(maxLength, '0')} ${metadata[index].title}${extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '')
})
files.forEach((file, index) => {
renameSync(file, targetFiles[index])
Expand Down
11 changes: 6 additions & 5 deletions src/core/metadata/thb-wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ export class THBWiki implements MetadataSource {
const [comments] = infos
.filter(it => it.name === 'comments')
.map(it => it.result as string)
const artists = ['vocals', 'coverVocals', 'harmonyVocals', 'accompanyVocals', 'chorusVocals','instruments', 'remix', 'arrangers']
const artists = ['vocals', 'coverVocals', 'harmonyVocals', 'accompanyVocals', 'chorusVocals', 'instruments', 'remix', 'arrangers']
.flatMap(name => infos
.filter(it => it.name === name)
.map(it => it.result as string[])
.flat())
.flat()
)
const [composers] = infos
.filter(it => it.name === 'composers')
.map(it => it.result as string[])
Expand All @@ -141,11 +142,11 @@ export class THBWiki implements MetadataSource {
}
return {
title,
artists: [...new Set(artists)],
artists: [...new Set(artists)].map(it => it.replace('(人物)', '')),
trackNumber,
comments,
lyricists,
composers,
lyricists: lyricists ? lyricists.map(it => it.replace('(人物)', '')) : lyricists,
composers: composers ? composers.map(it => it.replace('(人物)', '')) : composers,
}
}
async getMetadata(albumName: string) {
Expand Down

0 comments on commit 8bcbff7

Please sign in to comment.