@@ -7,6 +7,7 @@ import bytes from 'bytes'
7
7
import { name } from '../package.json'
8
8
import slugMap from './slug-map'
9
9
import imdbMap from './imdb-map'
10
+ import formatMap from './format-map'
10
11
import { Show , ShowWithEpisodes } from './interfaces'
11
12
12
13
/**
@@ -23,18 +24,6 @@ export class EztvApi {
23
24
24
25
private debug = debug ( name )
25
26
26
- /**
27
- * Maps the EZTV imdb codes to trakt.tv imdb codes.
28
- * @type {Object }
29
- */
30
- public static readonly slugMap = slugMap
31
-
32
- /**
33
- * Maps the EZTV slugs to trakt.tv slugs.
34
- * @type {Object }
35
- */
36
- public static readonly imdbMap = imdbMap
37
-
38
27
/**
39
28
* Create a new instance of the module.
40
29
* @param {!Object } config={} - The configuration object for the module.
@@ -56,9 +45,10 @@ export class EztvApi {
56
45
57
46
this . debug ( `Making request to: '${ uri } '` )
58
47
59
- return got . get ( uri ) . then ( ( { body } ) => {
60
- return cheerio . load ( body )
61
- } )
48
+ return got . get ( uri )
49
+ . then ( ( { body } ) => {
50
+ return cheerio . load ( body )
51
+ } )
62
52
}
63
53
64
54
/**
@@ -70,129 +60,164 @@ export class EztvApi {
70
60
. attr ( 'href' )
71
61
72
62
imdb = imdb ? imdb . match ( / \/ t i t l e \/ ( .* ) \/ / ) [ 1 ] : undefined
73
- imdb = imdb in EztvApi . imdbMap ? EztvApi . imdbMap [ imdb ] : imdb
63
+ imdb = imdb in imdbMap ? imdbMap [ imdb ] : imdb
74
64
75
65
if ( imdb ) {
76
66
data . imdb = imdb
77
67
}
78
68
79
69
const table = 'tr.forum_header_border[name="hover"]'
80
- $ ( table ) . each ( function ( ) {
81
- const entry = $ ( this )
82
- const magnet = entry . children ( 'td' ) . eq ( 2 )
83
- . children ( 'a.magnet' )
84
- . first ( )
85
- . attr ( 'href' )
86
-
87
- if ( ! magnet ) {
88
- return
89
- }
70
+ $ ( table )
71
+ . each ( function ( ) {
72
+ const entry = $ ( this )
73
+ const magnet = entry . children ( 'td' )
74
+ . eq ( 2 )
75
+ . children ( 'a.magnet' )
76
+ . first ( )
77
+ . attr ( 'href' )
90
78
91
- const seasonBased = / S ? 0 * ( \d + ) [ x E ] 0 * ( \d + ) / i
92
- const dateBased = / ( \d { 4 } ) .( \d { 2 } .\d { 2 } ) / i
93
- const title = entry . children ( 'td' ) . eq ( 1 )
94
- . text ( )
95
- . replace ( 'x264' , '' )
96
- let season
97
- let episode
98
-
99
- if ( title . match ( seasonBased ) ) {
100
- season = parseInt ( title . match ( seasonBased ) [ 1 ] , 10 )
101
- episode = parseInt ( title . match ( seasonBased ) [ 2 ] , 10 )
102
- data . dateBased = false
103
-
104
- } else if ( title . match ( dateBased ) ) {
105
- // If a item becomes data based check if the name of the show is in the
106
- // item this prevents wrongly mapped items to be added
107
- if ( ! data . dateBased && ! title . toLowerCase ( ) . includes ( data . title . toLowerCase ( ) ) ) {
79
+ if ( ! magnet ) {
108
80
return
109
81
}
110
82
111
- season = title . match ( dateBased ) [ 1 ]
112
- episode = title . match ( dateBased ) [ 2 ] . replace ( / \s / g, '-' )
113
- data . dateBased = true
114
- } else {
115
- season = null
116
- episode = null
117
- }
118
-
119
- if ( season !== null && episode !== null ) {
120
- if ( ! data . torrents ) {
121
- data . torrents = { }
83
+ const seasonBased = / S ? 0 * ( \d + ) [ x E ] 0 * ( \d + ) / i
84
+ const dateBased = / ( \d { 4 } ) .( \d { 2 } .\d { 2 } ) / i
85
+ const title = entry . children ( 'td' )
86
+ . eq ( 1 )
87
+ . text ( )
88
+ . replace ( 'x264' , '' )
89
+ let season
90
+ let episode
91
+
92
+ if ( title . match ( seasonBased ) ) {
93
+ season = parseInt ( title . match ( seasonBased ) [ 1 ] , 10 )
94
+ episode = parseInt ( title . match ( seasonBased ) [ 2 ] , 10 )
95
+ data . dateBased = false
96
+
97
+ } else if ( title . match ( dateBased ) ) {
98
+ // If a item becomes data based check if the name of the show is in the
99
+ // item this prevents wrongly mapped items to be added
100
+ if ( ! data . dateBased && ! title . toLowerCase ( )
101
+ . includes ( data . title . toLowerCase ( ) ) ) {
102
+ return
103
+ }
104
+
105
+ season = title . match ( dateBased ) [ 1 ]
106
+ episode = title . match ( dateBased ) [ 2 ] . replace ( / \s / g, '-' )
107
+ data . dateBased = true
108
+ } else {
109
+ season = null
110
+ episode = null
122
111
}
123
112
124
- if ( ! data . torrents [ season ] ) {
125
- data . torrents [ season ] = { }
113
+ if ( season !== null && episode !== null ) {
114
+ if ( ! data . torrents ) {
115
+ data . torrents = { }
116
+ }
117
+
118
+ if ( ! data . torrents [ season ] ) {
119
+ data . torrents [ season ] = { }
120
+ }
121
+
122
+ if ( ! data . torrents [ season ] [ episode ] ) {
123
+ data . torrents [ season ] [ episode ] = [ ]
124
+ }
125
+
126
+ const quality = title . match ( / ( \d { 3 , 4 } ) p / )
127
+ ? title . match ( / ( \d { 3 , 4 } ) p / ) [ 0 ]
128
+ : '480p'
129
+
130
+ const seeds = parseInt (
131
+ entry . children ( 'td' )
132
+ . last ( )
133
+ . text ( ) ,
134
+ 10
135
+ )
136
+
137
+ const sizeText = entry . children ( 'td' )
138
+ . eq ( 3 )
139
+ . text ( )
140
+ . toUpperCase ( )
141
+
142
+ const size = bytes ( sizeText . trim ( ) )
143
+
144
+ data . torrents [ season ] [ episode ] . push ( {
145
+ title,
146
+ url : magnet ,
147
+ seeds : isNaN ( seeds ) ? 0 : seeds ,
148
+ peers : 0 ,
149
+ provider : 'EZTV' ,
150
+ size : isNaN ( size ) ? 0 : size ,
151
+ quality
152
+ } )
126
153
}
127
-
128
- if ( ! data . torrents [ season ] [ episode ] ) {
129
- data . torrents [ season ] [ episode ] = [ ]
130
- }
131
-
132
- const quality = title . match ( / ( \d { 3 , 4 } ) p / )
133
- ? title . match ( / ( \d { 3 , 4 } ) p / ) [ 0 ]
134
- : '480p'
135
-
136
- const seeds = parseInt (
137
- entry . children ( 'td' ) . last ( )
138
- . text ( ) ,
139
- 10
140
- )
141
-
142
- const sizeText = entry . children ( 'td' ) . eq ( 3 )
143
- . text ( ) . toUpperCase ( )
144
-
145
- const size = bytes ( sizeText . trim ( ) )
146
-
147
- data . torrents [ season ] [ episode ] . push ( {
148
- title,
149
- url : magnet ,
150
- seeds : isNaN ( seeds ) ? 0 : seeds ,
151
- peers : 0 ,
152
- provider : 'EZTV' ,
153
- size : isNaN ( size ) ? 0 : size ,
154
- quality
155
- } )
156
- }
157
- } )
154
+ } )
158
155
159
156
return data
160
157
}
161
158
162
159
/**
163
160
* Get all the available shows from eztv.
164
161
*/
165
- public getAllShows ( ) : Promise < Show [ ] > {
166
- return this . get ( 'showlist/' ) . then ( ( $ ) => {
167
- const regex = / \/ s h o w s \/ ( .* ) \/ ( .* ) \/ /
168
-
169
- return $ ( '.thread_link' ) . map ( function ( ) {
170
- const entry = $ ( this )
171
- const href = entry . attr ( 'href' )
172
-
173
- const title = entry . text ( )
174
- const id = parseInt ( href . match ( regex ) [ 1 ] , 10 )
175
-
176
- let slug = href . match ( regex ) [ 2 ]
177
- slug = slug in EztvApi . slugMap ? EztvApi . slugMap [ slug ] : slug
178
-
179
- return {
180
- title,
181
- id,
182
- slug
183
- }
184
- } ) . get ( )
162
+ public async getAllShows ( ) : Promise < Show [ ] > {
163
+ const shows = await this . get ( 'showlist/' )
164
+ . then ( ( $ ) => {
165
+ const regex = / \/ s h o w s \/ ( .* ) \/ ( .* ) \/ /
166
+
167
+ return $ ( '.thread_link' )
168
+ . map ( function ( ) {
169
+ const entry = $ ( this )
170
+ const href = entry . attr ( 'href' )
171
+
172
+ const title = entry . text ( )
173
+ const id = parseInt ( href . match ( regex ) [ 1 ] , 10 )
174
+
175
+ let slug = href . match ( regex ) [ 2 ]
176
+ slug = slug in slugMap ? slugMap [ slug ] : slug
177
+
178
+ return {
179
+ title,
180
+ id,
181
+ slug
182
+ }
183
+ } )
184
+ . get ( )
185
+ } )
186
+
187
+ // Loop through all shows to see if they have a additional show in it
188
+ shows . forEach ( ( show ) => {
189
+ if ( show . slug in formatMap ) {
190
+ shows . push ( formatMap [ show . slug ] . additionalShow )
191
+ }
185
192
} )
193
+
194
+ return shows
186
195
}
187
196
188
197
/**
189
198
* Get episodes for a show.
190
199
* @param {Show } data - Teh show to get episodes for.
191
200
* @returns {Promise<Show, Error> } - The show with additional data.
192
201
*/
193
- public getShowData ( data : Show ) {
194
- return this . get ( `shows/${ data . id } /${ data . slug } /` )
202
+ public async getShowData ( data : Show ) : Promise < ShowWithEpisodes > {
203
+ let showId = data . id
204
+ let showSlug = data . slug
205
+
206
+ // Check if the slug is in the format map, if so get the original id and slug
207
+ if ( data . slug in formatMap ) {
208
+ showId = formatMap [ showSlug ] . id
209
+ showSlug = formatMap [ showSlug ] . slug
210
+ }
211
+
212
+ const showData = await this . get ( `shows/${ showId } /${ showSlug } /` )
195
213
. then ( ( $ ) => this . getEpisodeData ( data , $ ) )
214
+
215
+ // If the slug is inside the format map and has formatShow then return the formatted show
216
+ if ( data . slug in formatMap && formatMap [ data . slug ] . formatShow ) {
217
+ return formatMap [ showData . slug ] . formatShow ( showData )
218
+ }
219
+
220
+ return showData
196
221
}
197
222
198
223
}
0 commit comments