-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie-assembler.ts
37 lines (34 loc) · 1.14 KB
/
movie-assembler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { decode } from 'magnet-uri';
import { IStream } from '../models/stream';
import MovieDTO, { MovieMagnet } from '../models/transfer-objects/movie';
function toStreamData(movie: MovieDTO): IStream[] {
return movie.magnets.map((m: MovieMagnet) => {
const decodedMagnet = decode(m.magnet);
if(!decodedMagnet.infoHash) {
throw new Error("Magnet does not contain a valid infohash");
}
const infoHash = decodedMagnet.infoHash.toLowerCase();
const sources = decodedMagnet.announce;
return <IStream> {
metaId: movie.meta.id,
streamId: movie.meta.id,
seeders: m.seeders,
title: m.title,
size: m.size,
type: 'movie',
infoHash,
sources
};
});
}
/**
* Data mapper function that transform a received MovieDTO into an object with a meta and many streams
* @param series Data Transfer Object for movie content
* @returns the movie's metadata and streams
*/
export default function disassemble (movie: MovieDTO) {
return {
meta: movie.meta,
streams: toStreamData(movie),
};
}