Skip to content

Commit 0772ad3

Browse files
committed
feat(eztv-api): Added possibility for multiple shows to be in one id
This will add the second show and format the episodes correctly for that one. This is in case EZTV maps episode / shows incorrectly
1 parent 8ca4e1f commit 0772ad3

File tree

5 files changed

+207
-119
lines changed

5 files changed

+207
-119
lines changed

packages/eztv-api/src/eztv-api.ts

+134-109
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import bytes from 'bytes'
77
import { name } from '../package.json'
88
import slugMap from './slug-map'
99
import imdbMap from './imdb-map'
10+
import formatMap from './format-map'
1011
import { Show, ShowWithEpisodes } from './interfaces'
1112

1213
/**
@@ -23,18 +24,6 @@ export class EztvApi {
2324

2425
private debug = debug(name)
2526

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-
3827
/**
3928
* Create a new instance of the module.
4029
* @param {!Object} config={} - The configuration object for the module.
@@ -56,9 +45,10 @@ export class EztvApi {
5645

5746
this.debug(`Making request to: '${uri}'`)
5847

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+
})
6252
}
6353

6454
/**
@@ -70,129 +60,164 @@ export class EztvApi {
7060
.attr('href')
7161

7262
imdb = imdb ? imdb.match(/\/title\/(.*)\//)[1] : undefined
73-
imdb = imdb in EztvApi.imdbMap ? EztvApi.imdbMap[imdb] : imdb
63+
imdb = imdb in imdbMap ? imdbMap[imdb] : imdb
7464

7565
if (imdb) {
7666
data.imdb = imdb
7767
}
7868

7969
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')
9078

91-
const seasonBased = /S?0*(\d+)[xE]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) {
10880
return
10981
}
11082

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+)[xE]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
122111
}
123112

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+
})
126153
}
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+
})
158155

159156
return data
160157
}
161158

162159
/**
163160
* Get all the available shows from eztv.
164161
*/
165-
public getAllShows(): Promise<Show[]> {
166-
return this.get('showlist/').then(($) => {
167-
const regex = /\/shows\/(.*)\/(.*)\//
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 = /\/shows\/(.*)\/(.*)\//
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+
}
185192
})
193+
194+
return shows
186195
}
187196

188197
/**
189198
* Get episodes for a show.
190199
* @param {Show} data - Teh show to get episodes for.
191200
* @returns {Promise<Show, Error>} - The show with additional data.
192201
*/
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}/`)
195213
.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
196221
}
197222

198223
}

packages/eztv-api/src/format-map.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { ShowWithEpisodes, Torrent } from './interfaces'
2+
3+
const addTorrentToShow = (show: ShowWithEpisodes, season: string, episode: string, torrent: Torrent) => {
4+
if (!show.torrents[season]) {
5+
show.torrents[season] = {}
6+
}
7+
8+
if (!show.torrents[season][episode]) {
9+
show.torrents[season][episode] = []
10+
}
11+
12+
show.torrents[season][episode].push(torrent)
13+
14+
return show
15+
}
16+
17+
export default {
18+
'what-if-2021': {
19+
id: 6179,
20+
slug: 'what-if',
21+
formatShow: (show: ShowWithEpisodes): ShowWithEpisodes => {
22+
const marvelsWhatIfShow = {
23+
...show,
24+
id: 0,
25+
slug: 'what-if-2021',
26+
imdb: 'tt10168312',
27+
torrents: {}
28+
}
29+
30+
Object.keys(show.torrents)
31+
.forEach((season) => {
32+
Object.keys(show.torrents[season])
33+
.forEach((episode) => {
34+
show.torrents[season][episode].forEach((torrent: Torrent) => {
35+
if (torrent.title.includes('What If')) {
36+
addTorrentToShow(marvelsWhatIfShow, season, episode, torrent)
37+
}
38+
})
39+
})
40+
})
41+
42+
return marvelsWhatIfShow
43+
}
44+
},
45+
46+
'what-if': {
47+
additionalShow: {
48+
id: 0,
49+
slug: 'what-if-2021',
50+
title: 'What If...'
51+
}
52+
}
53+
}

packages/eztv-api/src/imdb-map.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Maps the EZTV slugs to trakt.tv slugs.
3+
* @type {Object}
4+
*/
15
export default {
26
tt0093036: 'tt3074694',
37
tt0102517: 'tt1657505',

packages/eztv-api/src/interfaces.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ export interface Show {
88

99
}
1010

11+
export interface Torrent {
12+
title: string
13+
url: string
14+
seeds: number
15+
peers: 0,
16+
provider: string
17+
size: number
18+
quality: string
19+
}
20+
1121
export interface ShowWithEpisodes extends Show {
1222

1323
imdb: string
1424

15-
episodes: {
25+
torrents: {
1626
[season: number]: {
17-
[episode: number]: [{
18-
title: string
19-
url: string
20-
seeds: number
21-
peers: 0,
22-
provider: string
23-
size: number
24-
quality: string
25-
}]
27+
[episode: number]: Torrent[]
2628
}
2729
}
2830

0 commit comments

Comments
 (0)