Skip to content

Commit

Permalink
feat: normalize result
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Apr 11, 2023
1 parent 0361dc8 commit 7eacaf8
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 37 deletions.
26 changes: 13 additions & 13 deletions packages/anitomy/src/element.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export enum ElementCategory {
AnimeSeason = 'season',
AnimeSeasonPrefix = 'seasonPrefix',
AnimeSeasonPrefix = 'prefix.season',
AnimeTitle = 'title',
AnimeType = 'type',
AnimeYear = 'year',
DeviceCompatibility = 'DeviceCompatibility',
Source = 'source',
// Episode
EpisodeNumber = 'episodeNumber',
EpisodeNumberAlt = 'episodeNumberAlt',
EpisodePrefix = 'episodePrefix',
EpisodeTitle = 'episodeTitle',
EpisodeNumber = 'episode.number',
EpisodeNumberAlt = 'episode.numberAlt',
EpisodePrefix = 'prefix.episode',
EpisodeTitle = 'episode.title',
// File
FileChecksum = 'checksum',
FileExtension = 'extension',
Expand All @@ -19,16 +19,16 @@ export enum ElementCategory {
Language = 'language',
Subtitles = 'subtitles',
// Audio and Video
AudioTerm = 'audioTerm',
VideoResolution = 'videoResolution',
VideoTerm = 'videoTerm',
AudioTerm = 'audio.term',
VideoResolution = 'video.resolution',
VideoTerm = 'video.term',
// Volume
VolumeNumber = 'volumeNumber',
VolumePrefix = 'volumePrefix',
VolumeNumber = 'volume',
VolumePrefix = 'prefix.volume',
// Release
ReleaseGroup = 'releaseGroup',
ReleaseInformation = 'releaseInformation',
ReleaseVersion = 'releaseVersion',
ReleaseGroup = 'release.group',
ReleaseInformation = 'release.information',
ReleaseVersion = 'release.version',
// Other
Unknown = 'unknown',
Other = 'other'
Expand Down
49 changes: 44 additions & 5 deletions packages/anitomy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnitomyOptions, ParsedResult } from './types';
import type { AnitomyOptions, AnitomyResult, ParsedResult } from './types';

import { mergeResult } from './utils';
import { KeywordManager } from './keyword';
Expand All @@ -8,8 +8,11 @@ import { tokenize as doTokenize } from './tokenizer';

export type * from './types';

export function parse(filename: string, _options: Partial<AnitomyOptions> = {}): ParsedResult {
if (filename === '') return {};
export function parse(
filename: string,
_options: Partial<AnitomyOptions> = {}
): AnitomyResult | undefined {
if (filename === '') return undefined;

let result: ParsedResult = {};

Expand All @@ -27,13 +30,13 @@ export function parse(filename: string, _options: Partial<AnitomyOptions> = {}):
const tokenized = doTokenize(result.filename!, options);
result = mergeResult(result, tokenized.result);
if (!tokenized.ok) {
return result;
return resolveResult(result);
}

const parsed = doParse(tokenized.tokens, options);
result = mergeResult(result, parsed.result);

return result;
return resolveResult(result);
}

export function resolveOptions(options: Partial<AnitomyOptions>): AnitomyOptions {
Expand All @@ -45,6 +48,42 @@ export function resolveOptions(options: Partial<AnitomyOptions>): AnitomyOptions
};
}

function resolveResult(result: ParsedResult): AnitomyResult {
const resolved: AnitomyResult = {
title: result['title'],
season: result['season'],
type: result['type'],
language: result['language'],
subtitles: result['subtitles'],
episode: {
number: result['episode.number'],
numberAlt: result['episode.numberAlt'],
title: result['episode.title']
},
volume: {
number: result['volume']
},
video: {
term: result['video.term'],
resolution: result['video.resolution']
},
audio: {
term: result['audio.term']
},
file: {
name: result['filename']!,
extension: result['extension'],
checksum: result['checksum']
},
prefix: {
season: result['prefix.season'],
volume: result['prefix.volume'],
episode: result['prefix.episode']
}
};
return resolved;
}

function removeExtension(filename: string) {
const position = filename.lastIndexOf('.');
if (position === -1) return undefined;
Expand Down
50 changes: 50 additions & 0 deletions packages/anitomy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,53 @@ export interface AnitomyOptions {
}

export type ParsedResult = Partial<Record<ElementCategory, string>>;

export interface AnitomyResult {
title: string | undefined;

season: string | undefined;

type: string | undefined;

language: string | undefined;

subtitles: string | undefined;

episode: {
number: string | undefined;

numberAlt: string | undefined;

title: string | undefined;
};

volume: {
number: string | undefined;
};

video: {
term: string | undefined;

resolution: string | undefined;
};

audio: {
term: string | undefined;
};

file: {
name: string;

checksum: string | undefined;

extension: string | undefined;
};

prefix: {
season: string | undefined;

volume: string | undefined;

episode: string | undefined;
};
}
164 changes: 150 additions & 14 deletions packages/anitomy/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,175 @@

exports[`parse > should work 1`] = `
{
"audioTerm": "AAC",
"filename": "[Lilith-Raws] 熊熊勇闖異世界 PUNCH! / Kuma Kuma Kuma Bear S02 - 02 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]",
"videoResolution": "1080p",
"videoTerm": "AVC",
"audio": {
"term": "AAC",
},
"episode": {
"number": undefined,
"numberAlt": undefined,
"title": undefined,
},
"file": {
"checksum": undefined,
"extension": undefined,
"name": "[Lilith-Raws] 熊熊勇闖異世界 PUNCH! / Kuma Kuma Kuma Bear S02 - 02 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4]",
},
"language": undefined,
"prefix": {
"episode": undefined,
"season": undefined,
"volume": undefined,
},
"season": undefined,
"subtitles": undefined,
"title": undefined,
"type": undefined,
"video": {
"resolution": "1080p",
"term": "AVC",
},
"volume": {
"number": undefined,
},
}
`;

exports[`parse > should work 2`] = `
{
"audioTerm": "AAC",
"filename": "[ANi] 機動戰士鋼彈 水星的魔女 Season 2 - 13 [1080P][Baha][WEB-DL][AAC AVC][CHT][MP4]",
"videoResolution": "1080P",
"videoTerm": "AVC",
"audio": {
"term": "AAC",
},
"episode": {
"number": undefined,
"numberAlt": undefined,
"title": undefined,
},
"file": {
"checksum": undefined,
"extension": undefined,
"name": "[ANi] 機動戰士鋼彈 水星的魔女 Season 2 - 13 [1080P][Baha][WEB-DL][AAC AVC][CHT][MP4]",
},
"language": undefined,
"prefix": {
"episode": undefined,
"season": undefined,
"volume": undefined,
},
"season": undefined,
"subtitles": undefined,
"title": undefined,
"type": undefined,
"video": {
"resolution": "1080P",
"term": "AVC",
},
"volume": {
"number": undefined,
},
}
`;

exports[`parse > should work 3`] = `
{
"filename": "[爱恋字幕社][4月新番][魔法使的新娘2][Mahoutsukai no Yome S2][01][1080P][MP4][GB][简中]",
"videoResolution": "1080P",
"audio": {
"term": undefined,
},
"episode": {
"number": undefined,
"numberAlt": undefined,
"title": undefined,
},
"file": {
"checksum": undefined,
"extension": undefined,
"name": "[爱恋字幕社][4月新番][魔法使的新娘2][Mahoutsukai no Yome S2][01][1080P][MP4][GB][简中]",
},
"language": undefined,
"prefix": {
"episode": undefined,
"season": undefined,
"volume": undefined,
},
"season": undefined,
"subtitles": undefined,
"title": undefined,
"type": undefined,
"video": {
"resolution": "1080P",
"term": undefined,
},
"volume": {
"number": undefined,
},
}
`;

exports[`parse > should work 4`] = `
{
"filename": "[桜都字幕组] 总之就是非常可爱 S2 / Tonikaku Kawaii S2 [01][1080p][简体内嵌]",
"videoResolution": "1080p",
"audio": {
"term": undefined,
},
"episode": {
"number": undefined,
"numberAlt": undefined,
"title": undefined,
},
"file": {
"checksum": undefined,
"extension": undefined,
"name": "[桜都字幕组] 总之就是非常可爱 S2 / Tonikaku Kawaii S2 [01][1080p][简体内嵌]",
},
"language": undefined,
"prefix": {
"episode": undefined,
"season": undefined,
"volume": undefined,
},
"season": undefined,
"subtitles": undefined,
"title": undefined,
"type": undefined,
"video": {
"resolution": "1080p",
"term": undefined,
},
"volume": {
"number": undefined,
},
}
`;

exports[`parse > should work 5`] = `
{
"filename": "【喵萌奶茶屋】★04月新番★[百合是我的工作!/我的百合乃工作是也!/私の百合はお仕事です!/Watashi no Yuri wa Oshigoto desu!][01][720p][繁日雙語][招募翻譯校對]",
"videoResolution": "720p",
"audio": {
"term": undefined,
},
"episode": {
"number": undefined,
"numberAlt": undefined,
"title": undefined,
},
"file": {
"checksum": undefined,
"extension": undefined,
"name": "【喵萌奶茶屋】★04月新番★[百合是我的工作!/我的百合乃工作是也!/私の百合はお仕事です!/Watashi no Yuri wa Oshigoto desu!][01][720p][繁日雙語][招募翻譯校對]",
},
"language": undefined,
"prefix": {
"episode": undefined,
"season": undefined,
"volume": undefined,
},
"season": undefined,
"subtitles": undefined,
"title": undefined,
"type": undefined,
"video": {
"resolution": "720p",
"term": undefined,
},
"volume": {
"number": undefined,
},
}
`;
Loading

0 comments on commit 7eacaf8

Please sign in to comment.