Skip to content

Commit

Permalink
Add metadata lyric time
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Apr 2, 2020
1 parent 7cc939e commit 597b492
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 19 deletions.
2 changes: 2 additions & 0 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const cliOptions = commandLineArgs([
{ name: 'lyric', alias: 'l', type: Boolean, defaultValue: false },
{ name: 'lyric-type', alias: 't', type: String, defaultValue: 'original' },
{ name: 'lyric-output', alias: 'o', type: String, defaultValue: 'metadata' },
{ name: 'no-lyric-time', alias: 'T', type: Boolean, defaultValue: false },
]);
debug_1.setDebug(cliOptions.debug);
const metadataConfig = {
lyric: cliOptions.lyric ? {
type: cliOptions['lyric-type'],
output: cliOptions['lyric-output'],
time: !cliOptions['no-lyric-time']
} : undefined
};
debug_1.log(cliOptions, metadataConfig);
Expand Down
29 changes: 22 additions & 7 deletions dist/core/metadata/thb-wiki/lyrics/lyric-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("../../../debug");
class LyricParser {
constructor(table) {
constructor(table, config) {
this.table = table;
this.config = config;
this.rows = [...table.querySelectorAll('tbody > tr:not(.tt-lyrics-header)')];
debug_1.log('rows length: ', this.rows.length);
this.rowData = this.rows.map(row => {
const time = row.querySelector('td.tt-time');
let [originalData, translatedData] = [...row.querySelectorAll('td:not(.tt-time)')];
const hasTranslatedData = Boolean(translatedData && translatedData.textContent);
if (!hasTranslatedData) {
translatedData = originalData;
}
return {
time: time ? `[${time.textContent}] ` : '',
originalData,
translatedData,
hasTranslatedData,
Expand All @@ -30,6 +33,7 @@ class LyricParser {
return this.readLyricRow(row);
}
}).join('\n');
// TODO: lyric timeline
}
getRowData(row) {
return this.rowData[this.rows.indexOf(row)];
Expand All @@ -41,7 +45,11 @@ class OriginalLyricParser extends LyricParser {
return this.firstRowData.originalData.getAttribute('lang');
}
readLyricRow(row) {
return this.getRowData(row).originalData.textContent;
const { originalData, time } = this.getRowData(row);
if (this.config.time) {
return time + originalData.textContent;
}
return originalData.textContent;
}
getLrcFileSuffix() {
return '';
Expand All @@ -56,7 +64,11 @@ class TranslatedLyricParser extends LyricParser {
return originalData.getAttribute('lang');
}
readLyricRow(row) {
return this.getRowData(row).translatedData.textContent;
const { translatedData, time } = this.getRowData(row);
if (this.config.time) {
return time + translatedData.textContent;
}
return translatedData.textContent;
}
getLrcFileSuffix() {
return '.' + this.findLanguage();
Expand All @@ -73,11 +85,14 @@ class MixedLyricParser extends LyricParser {
}
}
readLyricRow(row) {
const { originalData, translatedData, hasTranslatedData } = this.getRowData(row);
const { originalData, translatedData, hasTranslatedData, time } = this.getRowData(row);
let lyric = originalData.textContent;
if (hasTranslatedData) {
lyric += '\n' + translatedData.textContent;
}
if (this.config.time) {
lyric = time + lyric;
}
return lyric;
}
getLrcFileSuffix() {
Expand All @@ -87,8 +102,8 @@ class MixedLyricParser extends LyricParser {
exports.getLyricParser = (table, config) => {
switch (config.type) {
default: // fallthrough
case 'original': return new OriginalLyricParser(table);
case 'translated': return new TranslatedLyricParser(table);
case 'mixed': return new MixedLyricParser(table);
case 'original': return new OriginalLyricParser(table, config);
case 'translated': return new TranslatedLyricParser(table, config);
case 'mixed': return new MixedLyricParser(table, config);
}
};
3 changes: 2 additions & 1 deletion dist/core/metadata/thb-wiki/lyrics/thb-wiki-lyrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ test('Write lyrics to mp3', async () => {
const album = 'POP|CULTURE 8';
__1.thbWiki.config.lyric = {
type: 'original',
output: 'metadata'
output: 'metadata',
time: false,
};
debug_1.setDebug(true);
const metadata = (await __1.thbWiki.getMetadata(album))[1];
Expand Down
2 changes: 1 addition & 1 deletion dist/core/metadata/thb-wiki/thb-wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class THBWiki extends metadata_source_1.MetadataSource {
let textContent = data.textContent;
/*
要是这个值就是一个_, THBWiki 会转成一个警告...
例如疯帽子茶会'千年战争'中出现的编曲者就有一个_
例如疯帽子茶会'千年战争'中出现的编曲者就有一个_ (现已更名为'底线')
https://thwiki.cc/%E5%8D%83%E5%B9%B4%E6%88%98%E4%BA%89%EF%BD%9Eiek_loin_staim_haf_il_dis_o-del_al
*/
const warningMatch = textContent.match(/\[\[(.+)\]\]/);
Expand Down
3 changes: 3 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ const cliOptions = commandLineArgs([
{ name: 'lyric', alias: 'l', type: Boolean, defaultValue: false },
{ name: 'lyric-type', alias: 't', type: String, defaultValue: 'original' },
{ name: 'lyric-output', alias: 'o', type: String, defaultValue: 'metadata' },
{ name: 'no-lyric-time', alias: 'T', type: Boolean, defaultValue: false },
]) as {
cover: boolean
debug: boolean
source: string
lyric: boolean
'lyric-type': string
'lyric-output': string
'no-lyric-time': boolean
}
setDebug(cliOptions.debug)
const metadataConfig: MetadataConfig = {
lyric: cliOptions.lyric ? {
type: cliOptions['lyric-type'],
output: cliOptions['lyric-output'],
time: !cliOptions['no-lyric-time']
} as LyricConfig : undefined
}
log(cliOptions, metadataConfig)
Expand Down
1 change: 1 addition & 0 deletions src/core/core-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const MetadataSeparator = ', '
export interface LyricConfig {
type: 'original' | 'translated' | 'mixed'
output: 'metadata' | 'lrc'
time: boolean
}
export interface MetadataConfig {
lyric?: LyricConfig
Expand Down
30 changes: 22 additions & 8 deletions src/core/metadata/thb-wiki/lyrics/lyric-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { log } from '../../../debug'

export abstract class LyricParser {
protected rows: Element[]
protected rowData: { originalData: Element, translatedData: Element, hasTranslatedData: boolean }[]
protected rowData: { originalData: Element, translatedData: Element, hasTranslatedData: boolean, time: string }[]
protected get firstRow() { return this.rows[0] }
protected get firstRowData() { return this.rowData[0] }
constructor(protected table: Element) {
constructor(protected table: Element, public config: LyricConfig) {
this.rows = [...table.querySelectorAll('tbody > tr:not(.tt-lyrics-header)')]
log('rows length: ', this.rows.length)
this.rowData = this.rows.map(row => {
const time = row.querySelector('td.tt-time') as Element
let [originalData, translatedData] = [...row.querySelectorAll('td:not(.tt-time)')]
const hasTranslatedData = Boolean(translatedData && translatedData.textContent)
if (!hasTranslatedData) {
translatedData = originalData
}
return {
time: time ? `[${time.textContent}] ` : '',
originalData,
translatedData,
hasTranslatedData,
Expand All @@ -30,6 +32,7 @@ export abstract class LyricParser {
return this.readLyricRow(row)
}
}).join('\n')
// TODO: lyric timeline
}
protected abstract readLyricRow(row: Element): string
protected getRowData(row: Element) {
Expand All @@ -43,7 +46,11 @@ class OriginalLyricParser extends LyricParser {
return this.firstRowData.originalData.getAttribute('lang')!!
}
readLyricRow(row: Element): string {
return this.getRowData(row).originalData.textContent!!
const { originalData, time } = this.getRowData(row)
if (this.config.time) {
return time + originalData.textContent
}
return originalData.textContent!!
}
getLrcFileSuffix(): string {
return ''
Expand All @@ -58,7 +65,11 @@ class TranslatedLyricParser extends LyricParser {
return originalData.getAttribute('lang')!!
}
readLyricRow(row: Element): string {
return this.getRowData(row).translatedData.textContent!!
const { translatedData, time } = this.getRowData(row)
if (this.config.time) {
return time + translatedData.textContent
}
return translatedData.textContent!!
}
getLrcFileSuffix(): string {
return '.' + this.findLanguage()
Expand All @@ -74,11 +85,14 @@ class MixedLyricParser extends LyricParser {
}
}
readLyricRow(row: Element): string {
const { originalData, translatedData, hasTranslatedData } = this.getRowData(row)
const { originalData, translatedData, hasTranslatedData, time } = this.getRowData(row)
let lyric = originalData.textContent!!
if (hasTranslatedData) {
lyric += '\n' + translatedData.textContent
}
if (this.config.time) {
lyric = time + lyric
}
return lyric
}
getLrcFileSuffix(): string {
Expand All @@ -88,8 +102,8 @@ class MixedLyricParser extends LyricParser {
export const getLyricParser = (table: Element, config: LyricConfig) => {
switch (config.type) {
default: // fallthrough
case 'original': return new OriginalLyricParser(table)
case 'translated': return new TranslatedLyricParser(table)
case 'mixed': return new MixedLyricParser(table)
case 'original': return new OriginalLyricParser(table, config)
case 'translated': return new TranslatedLyricParser(table, config)
case 'mixed': return new MixedLyricParser(table, config)
}
}
3 changes: 2 additions & 1 deletion src/core/metadata/thb-wiki/lyrics/thb-wiki-lyrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test('Write lyrics to mp3', async () => {
const album = 'POP|CULTURE 8'
thbWiki.config.lyric = {
type: 'original',
output: 'metadata'
output: 'metadata',
time: false,
}
setDebug(true)
const metadata = (await thbWiki.getMetadata(album))[1]
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 @@ -88,7 +88,7 @@ export class THBWiki extends MetadataSource {
let textContent = data.textContent!
/*
要是这个值就是一个_, THBWiki 会转成一个警告...
例如疯帽子茶会'千年战争'中出现的编曲者就有一个_
例如疯帽子茶会'千年战争'中出现的编曲者就有一个_ (现已更名为'底线')
https://thwiki.cc/%E5%8D%83%E5%B9%B4%E6%88%98%E4%BA%89%EF%BD%9Eiek_loin_staim_haf_il_dis_o-del_al
*/
const warningMatch = textContent.match(/\[\[(.+)\]\]/)
Expand Down

0 comments on commit 597b492

Please sign in to comment.