1
1
import { Injectable , Logger } from '@nestjs/common'
2
2
import { InjectModel } from '@nestjs/mongoose'
3
- import { EpisodeModel , Episode } from '@pct-org/mongo-models'
3
+ import { EpisodeModel , Episode , Show } from '@pct-org/mongo-models'
4
4
import { defaultEpisodeImages } from '@pct-org/constants/default-image-sizes'
5
5
import { TYPE_EPISODE } from '@pct-org/constants/item-types'
6
+ import { TraktEpisode } from '@pct-org/services/trakt'
7
+ import { formatTorrents } from '@pct-org/torrent/utils'
8
+ import { ScrapedTorrent } from '@pct-org/scraper/providers/base'
6
9
7
10
@Injectable ( )
8
11
export class EpisodeHelperService {
@@ -12,7 +15,7 @@ export class EpisodeHelperService {
12
15
13
16
protected readonly logger = new Logger ( 'EpisodeHelper' )
14
17
15
- public getEmptyEpisode ( ) : Episode {
18
+ private getEmptyEpisode ( ) : Episode {
16
19
return {
17
20
_id : null ,
18
21
firstAired : 0 ,
@@ -42,8 +45,40 @@ export class EpisodeHelperService {
42
45
}
43
46
}
44
47
48
+ public formatTraktEpisode ( show : Show , episode : TraktEpisode , torrents : ScrapedTorrent [ ] ) : Episode {
49
+ return {
50
+ ...this . getEmptyEpisode ( ) ,
51
+
52
+ _id : `${ show . _id } -${ episode . season } -${ episode . number } ` ,
53
+ showImdbId : show . _id ,
54
+ tmdbId : episode . ids . tmdb ,
55
+ number : episode . number ,
56
+ season : episode . season ,
57
+ title : episode . title ,
58
+ synopsis : episode . overview ,
59
+ firstAired : Number ( new Date ( episode . first_aired ) ) ?? 0 ,
60
+ torrents : formatTorrents ( torrents )
61
+ }
62
+ }
63
+
64
+ public formatUnknownEpisode ( show : Show , seasonNr : number , episodeNr : number , torrents : ScrapedTorrent [ ] ) : Episode {
65
+ return {
66
+ ...this . getEmptyEpisode ( ) ,
67
+
68
+ _id : `${ show . _id } -${ seasonNr } -${ episodeNr } ` ,
69
+ showImdbId : show . _id ,
70
+ number : episodeNr ,
71
+ season : seasonNr ,
72
+ title : `Episode ${ episodeNr } ` ,
73
+ // Use the createdAt date, otherwise it wont show in the app, will be overwritten when
74
+ // metadata becomes available
75
+ firstAired : Number ( new Date ( ) ) ,
76
+ torrents : formatTorrents ( torrents )
77
+ }
78
+ }
79
+
45
80
public async addEpisodesToDatabase ( episodes : Episode [ ] ) : Promise < void > {
46
- await Promise . all ( episodes . map ( this . addEpisodeToDatabase ) )
81
+ await Promise . all ( episodes . map ( ( episode ) => this . addEpisodeToDatabase ( episode ) ) )
47
82
}
48
83
49
84
public async updateEpisodesInDatabase ( episodes : Episode [ ] ) : Promise < void > {
0 commit comments