Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/source/helper/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import { getBDInfoOrMediaInfoFromBBCode } from '.';

describe('getBDInfoOrMediaInfoFromBBCode', () => {
it('recognizes a sectioned MediaInfo without identifying fields', () => {
const mediaInfo = [
'General',
'Complete name : demo.mkv',
'Format : Matroska',
'',
'Video',
'Format : AVC',
'Width : 1 920 pixels',
'',
'Audio',
'Format : AAC LC',
'Channel(s) : 2 channels',
].join('\n');

expect(
getBDInfoOrMediaInfoFromBBCode(`[quote]${mediaInfo}[/quote]`),
).toEqual({ bdInfo: [], mediaInfo: [mediaInfo] });
});

it('recognizes MediaInfo containing square brackets', () => {
const mediaInfo = [
'General',
'Unique ID : 261661174147273455199166502268741581595',
'',
'Video',
'Codec ID : V_MPEG4/ISO/AVC',
'',
'Audio',
'Codec ID : A_DTS',
'',
'Text #1',
'Title : 简体中文&English [MT机翻]',
].join('\n');

expect(
getBDInfoOrMediaInfoFromBBCode(`[quote]${mediaInfo}[/quote]`),
).toEqual({ bdInfo: [], mediaInfo: [mediaInfo] });
});

it('does not treat an ordinary quote mentioning sections as MediaInfo', () => {
const quote = 'General information about Video and Audio';

expect(getBDInfoOrMediaInfoFromBBCode(`[quote]${quote}[/quote]`)).toEqual({
bdInfo: [],
mediaInfo: [],
});
});
});
40 changes: 32 additions & 8 deletions src/source/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,48 @@ export const getBDInfoOrMediaInfoFromBBCode = (
bbcode: string,
): { bdInfo: string[]; mediaInfo: string[] } => {
const quoteList: string[] = [];
const quoteRegex = /\[quote\]([^[\]])+?\[\/quote\]/gi;
while (bbcode?.match(quoteRegex)?.length) {
const matchContent = bbcode?.match(quoteRegex)?.[0] ?? '';
quoteList.push(matchContent);
bbcode = bbcode.replace(matchContent, '');
const quoteRegex = /\[quote(?:=[^\]]*)?\][\s\S]*?\[\/quote\]/gi;
let quoteMatch: RegExpExecArray | null;
while ((quoteMatch = quoteRegex.exec(bbcode)) !== null) {
quoteList.push(quoteMatch[0]);
}
bbcode = bbcode.replace(quoteRegex, '');
const cleanQuoteContent = (quote: string) =>
quote
.replace(/\[\/?quote\]/g, '')
.replace(/^\[quote(?:=[^\]]*)?\]/i, '')
.replace(/\[\/quote\]$/i, '')
.replace(/\u200D/g, '')
.trim();

const isBDInfo = (content: string) =>
/Disc\s?Size|\.mpls/i.test(content) ||
/Disc\s+(Info|Title|Label)[^[]+/i.test(content);

const isMediaInfo = (content: string) =>
/(Unique\s*ID)|(Codec\s*ID)|(Stream\s*size)/i.test(content);
const isMediaInfo = (content: string) => {
if (/(Unique\s*ID)|(Codec\s*ID)|(Stream\s*size)/i.test(content)) {
return true;
}

// Some MediaInfo outputs omit the identifying fields above. In that case,
// recognize the standard section order instead of leaving the whole dump as
// an ordinary quote.
const sectionNames = content
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) =>
/^(General|Video(?:\s*#\d+)?|Audio(?:\s*#\d+)?)$/i.test(line),
)
.map((line) => line.match(/^[A-Za-z]+/)?.[0].toLowerCase());
const generalIndex = sectionNames.indexOf('general');
const videoIndex = sectionNames.indexOf('video');
const audioIndex = sectionNames.indexOf('audio');

return (
generalIndex !== -1 &&
videoIndex > generalIndex &&
audioIndex > videoIndex
);
};

const { bdInfo, mediaInfo } = quoteList.reduce(
(acc, quote) => {
Expand Down
Loading