mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
add assign subtitles tests, and fix subtitles service
This commit is contained in:
@@ -10,8 +10,14 @@ export class TorrentSubtitleService implements ITorrentSubtitleService {
|
|||||||
public assignSubtitles = (fileCollection: ITorrentFileCollection): ITorrentFileCollection => {
|
public assignSubtitles = (fileCollection: ITorrentFileCollection): ITorrentFileCollection => {
|
||||||
if (fileCollection.videos && fileCollection.videos.length && fileCollection.subtitles && fileCollection.subtitles.length) {
|
if (fileCollection.videos && fileCollection.videos.length && fileCollection.subtitles && fileCollection.subtitles.length) {
|
||||||
if (fileCollection.videos.length === 1) {
|
if (fileCollection.videos.length === 1) {
|
||||||
fileCollection.videos[0].subtitles = fileCollection.subtitles;
|
const matchingSubtitles = fileCollection.subtitles.filter(subtitle =>
|
||||||
return {...fileCollection, subtitles: []};
|
this.mostProbableSubtitleVideos(subtitle, [fileCollection.videos[0]]).length > 0
|
||||||
|
);
|
||||||
|
fileCollection.videos[0].subtitles = matchingSubtitles;
|
||||||
|
const nonMatchingSubtitles = fileCollection.subtitles.filter(subtitle =>
|
||||||
|
!matchingSubtitles.includes(subtitle)
|
||||||
|
);
|
||||||
|
return {...fileCollection, subtitles: nonMatchingSubtitles};
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedVideos = fileCollection.videos.map(video => this.parseVideo(video));
|
const parsedVideos = fileCollection.videos.map(video => this.parseVideo(video));
|
||||||
@@ -32,12 +38,11 @@ export class TorrentSubtitleService implements ITorrentSubtitleService {
|
|||||||
private parseVideo = (video: IFileAttributes): IFileAttributes => {
|
private parseVideo = (video: IFileAttributes): IFileAttributes => {
|
||||||
const fileName = video.title?.split('/')?.pop()?.replace(/\.(\w{2,4})$/, '') || '';
|
const fileName = video.title?.split('/')?.pop()?.replace(/\.(\w{2,4})$/, '') || '';
|
||||||
const folderName = video.title?.replace(/\/?[^/]+$/, '') || '';
|
const folderName = video.title?.replace(/\/?[^/]+$/, '') || '';
|
||||||
return {
|
return Object.assign(video, {
|
||||||
videoFile: video,
|
|
||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
folderName: folderName,
|
folderName: folderName,
|
||||||
...this.parseFilename(video.title.toString() || '')
|
...this.parseFilename(video.title.toString() || '')
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private mostProbableSubtitleVideos = (subtitle: ISubtitleAttributes, parsedVideos: IFileAttributes[]): IFileAttributes[] => {
|
private mostProbableSubtitleVideos = (subtitle: ISubtitleAttributes, parsedVideos: IFileAttributes[]): IFileAttributes[] => {
|
||||||
@@ -48,13 +53,15 @@ export class TorrentSubtitleService implements ITorrentSubtitleService {
|
|||||||
return byFileName.map(v => v);
|
return byFileName.map(v => v);
|
||||||
}
|
}
|
||||||
const byTitleSeasonEpisode = parsedVideos.filter(video => video.title === parsedSub.title
|
const byTitleSeasonEpisode = parsedVideos.filter(video => video.title === parsedSub.title
|
||||||
&& this.arrayEquals(video.seasons || [], parsedSub.seasons || [])
|
&& parsedSub.seasons && parsedSub.episodes
|
||||||
&& this.arrayEquals(video.episodes || [], parsedSub.episodes || []));
|
&& this.arrayEquals(video.seasons || [], parsedSub.seasons)
|
||||||
|
&& this.arrayEquals(video.episodes || [], parsedSub.episodes));
|
||||||
if (this.singleVideoFile(byTitleSeasonEpisode)) {
|
if (this.singleVideoFile(byTitleSeasonEpisode)) {
|
||||||
return byTitleSeasonEpisode.map(v => v);
|
return byTitleSeasonEpisode.map(v => v);
|
||||||
}
|
}
|
||||||
const bySeasonEpisode = parsedVideos.filter(video => this.arrayEquals(video.seasons || [], parsedSub.seasons || [])
|
const bySeasonEpisode = parsedVideos.filter(video => parsedSub.seasons && parsedSub.episodes
|
||||||
&& this.arrayEquals(video.episodes || [], parsedSub.episodes || []));
|
&& this.arrayEquals(video.seasons || [], parsedSub.seasons)
|
||||||
|
&& this.arrayEquals(video.episodes || [], parsedSub.episodes));
|
||||||
if (this.singleVideoFile(bySeasonEpisode)) {
|
if (this.singleVideoFile(bySeasonEpisode)) {
|
||||||
return bySeasonEpisode.map(v => v);
|
return bySeasonEpisode.map(v => v);
|
||||||
}
|
}
|
||||||
@@ -62,10 +69,15 @@ export class TorrentSubtitleService implements ITorrentSubtitleService {
|
|||||||
if (this.singleVideoFile(byTitle)) {
|
if (this.singleVideoFile(byTitle)) {
|
||||||
return byTitle.map(v => v);
|
return byTitle.map(v => v);
|
||||||
}
|
}
|
||||||
const byEpisode = parsedVideos.filter(video => this.arrayEquals(video.episodes || [], parsedSub.episodes || []));
|
const byEpisode = parsedVideos.filter(video => parsedSub.episodes
|
||||||
|
&& this.arrayEquals(video.episodes || [], parsedSub.episodes || []));
|
||||||
if (this.singleVideoFile(byEpisode)) {
|
if (this.singleVideoFile(byEpisode)) {
|
||||||
return byEpisode.map(v => v);
|
return byEpisode.map(v => v);
|
||||||
}
|
}
|
||||||
|
const byInfoHash = parsedVideos.filter(video => video.infoHash === subtitle.infoHash);
|
||||||
|
if (this.singleVideoFile(byInfoHash)) {
|
||||||
|
return byInfoHash.map(v => v);
|
||||||
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
102
src/node/consumer/test/torrent_subtitle_service.test.ts
Normal file
102
src/node/consumer/test/torrent_subtitle_service.test.ts
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
import "reflect-metadata"; // required
|
||||||
|
import {ITorrentFileCollection} from "@interfaces/torrent_file_collection";
|
||||||
|
import {TorrentSubtitleService} from "@services/torrent_subtitle_service";
|
||||||
|
|
||||||
|
describe('TrrentSubtitleService tests', () => {
|
||||||
|
let torrentSubtitleService: TorrentSubtitleService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
torrentSubtitleService = new TorrentSubtitleService();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should assign subtitles to a single video', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [{ title: 'Test video', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' }],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [{ title: 'Test subtitle', fileIndex: 0, path: 'Test path', infoHash: 'Test infoHash' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result.videos[0].subtitles).toEqual(fileCollection.subtitles);
|
||||||
|
expect(result.subtitles).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not assign subtitles if there are no videos', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [{ title: 'Test subtitle', fileIndex: 0, path: 'Test path', infoHash: 'Test infoHash' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result).toEqual(fileCollection);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not assign subtitles if there are no subtitles', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [{ title: 'Test video', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' }],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result).toEqual(fileCollection);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should assign subtitles to multiple videos', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [
|
||||||
|
{ title: 'Test video S01E01', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' },
|
||||||
|
{ title: 'Test video S01E02', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' }
|
||||||
|
],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [
|
||||||
|
{ title: 'Test subtitle S01E01', fileIndex: 0, path: 'Test path', infoHash: 'Test infoHash' },
|
||||||
|
{ title: 'Test subtitle S01E02', fileIndex: 1, path: 'Test path', infoHash: 'Test infoHash' }
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result.videos[0].subtitles).toEqual([fileCollection.subtitles[0]]);
|
||||||
|
expect(result.videos[1].subtitles).toEqual([fileCollection.subtitles[1]]);
|
||||||
|
expect(result.subtitles).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not assign subtitles if there are no matching videos', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [{ title: 'Test video', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' }],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [{ title: 'Non-matching subtitle', fileIndex: 0, path: 'Test path', infoHash: 'Non-matching infoHash' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result.videos[0].subtitles).toEqual([]);
|
||||||
|
expect(result.subtitles).toEqual([fileCollection.subtitles[0]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should assign subtitles to the most probable videos based on filename, title, season, and episode', () => {
|
||||||
|
const fileCollection: ITorrentFileCollection = {
|
||||||
|
videos: [
|
||||||
|
{ title: 'Test video S01E01', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' },
|
||||||
|
{ title: 'Test video S01E02', size: 123456, imdbId: 'tt1234567', infoHash: 'Test infoHash' }
|
||||||
|
],
|
||||||
|
contents: [],
|
||||||
|
subtitles: [
|
||||||
|
{ title: 'Test subtitle S01E01', fileIndex: 0, path: 'Test path', infoHash: 'Test infoHash' },
|
||||||
|
{ title: 'Test subtitle S01E02', fileIndex: 1, path: 'Test path', infoHash: 'Test infoHash' }
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = torrentSubtitleService.assignSubtitles(fileCollection);
|
||||||
|
|
||||||
|
expect(result.videos[0].subtitles).toEqual([fileCollection.subtitles[0]]);
|
||||||
|
expect(result.videos[1].subtitles).toEqual([fileCollection.subtitles[1]]);
|
||||||
|
expect(result.subtitles).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user