Skip to content

Commit fd91a9c

Browse files
Merge pull request #2525 from iptv-org/bellezaemporium/fixes/pickx
[Fix] pickx.be
2 parents af932a9 + 782f911 commit fd91a9c

File tree

2 files changed

+57
-61
lines changed

2 files changed

+57
-61
lines changed

sites/pickx.be/pickx.be.config.js

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,22 @@ const dayjs = require('dayjs')
33
const utc = require('dayjs/plugin/utc')
44

55
let apiVersion
6-
let isApiVersionFetched = false
7-
8-
;(async () => {
9-
try {
10-
await fetchApiVersion()
11-
isApiVersionFetched = true
12-
} catch (error) {
13-
console.error('Error during script initialization:', error)
14-
}
15-
})()
166

177
dayjs.extend(utc)
188

199
module.exports = {
2010
site: 'pickx.be',
2111
days: 2,
22-
apiVersion: function () {
12+
setApiVersion: function (version) {
13+
apiVersion = version
14+
},
15+
getApiVersion: function () {
2316
return apiVersion
2417
},
25-
fetchApiVersion: fetchApiVersion, // Export fetchApiVersion
18+
fetchApiVersion: fetchApiVersion,
2619
url: async function ({ channel, date }) {
27-
while (!isApiVersionFetched) {
28-
await new Promise(resolve => setTimeout(resolve, 100)) // Wait for 100 milliseconds
20+
if (!apiVersion) {
21+
await fetchApiVersion()
2922
}
3023
return `https://px-epg.azureedge.net/airings/${apiVersion}/${date.format(
3124
'YYYY-MM-DD'
@@ -116,7 +109,7 @@ module.exports = {
116109
}`
117110
}
118111
const result = await axios
119-
.post('https://api.proximusmwc.be/tiams/v2/graphql', query)
112+
.post('https://api.proximusmwc.be/tiams/v3/graphql', query)
120113
.then(r => r.data)
121114
.catch(console.error)
122115

@@ -140,53 +133,38 @@ function fetchApiVersion() {
140133
return new Promise(async (resolve, reject) => {
141134
try {
142135
// you'll never find what happened here :)
143-
// load pickx bundle and get react version hash (regex).
136+
// load the pickx page and get the hash from the MWC configuration.
144137
// it's not the best way to get the version but it's the only way to get it.
145138

146-
// find bundle version
147-
const minBundleVer = "https://www.pickx.be/minimal-bundle-version"
148-
const bundleVerData = await axios.get(minBundleVer, {
149-
headers: {
150-
Origin: 'https://www.pickx.be',
151-
Referer: 'https://www.pickx.be/'
152-
}
153-
})
154-
155-
if (bundleVerData.status !== 200) {
156-
console.error(`Failed to fetch bundle version. Status: ${bundleVerData.status}`)
157-
reject(`Failed to fetch bundle version. Status: ${bundleVerData.status}`)
158-
} else {
159-
const bundleVer = bundleVerData.data.version
160-
// get the minified JS app bundle
161-
const bundleUrl = `https://components.pickx.be/pxReactPlayer/${bundleVer}/bundle.min.js`
162-
163-
// now, find the react hash inside the bundle URL
164-
const bundle = await axios.get(bundleUrl).then(r => {
165-
const re = /REACT_APP_VERSION_HASH:"([^"]+)"/
166-
const match = r.data.match(re)
167-
if (match && match[1]) {
168-
return match[1]
169-
} else {
170-
throw new Error('React app version hash not found')
171-
}
172-
}).catch(console.error)
173-
174-
const versionUrl = `https://www.pickx.be/api/s-${bundle.replace('/REACT_APP_VERSION_HASH:"', '')}`
139+
const hashUrl = 'https://www.pickx.be/nl/televisie/tv-gids';
175140

176-
const response = await axios.get(versionUrl, {
177-
headers: {
178-
Origin: 'https://www.pickx.be',
179-
Referer: 'https://www.pickx.be/'
180-
}
181-
})
182-
183-
if (response.status === 200) {
184-
apiVersion = response.data.version
185-
resolve()
141+
const hashData = await axios.get(hashUrl)
142+
.then(r => {
143+
const re = /"hashes":\["(.*)"\]/
144+
const match = r.data.match(re)
145+
if (match && match[1]) {
146+
return match[1]
186147
} else {
187-
console.error(`Failed to fetch API version. Status: ${response.status}`)
188-
reject(`Failed to fetch API version. Status: ${response.status}`)
148+
throw new Error('React app version hash not found')
189149
}
150+
})
151+
.catch(console.error);
152+
153+
const versionUrl = `https://www.pickx.be/api/s-${hashData}`
154+
155+
const response = await axios.get(versionUrl, {
156+
headers: {
157+
Origin: 'https://www.pickx.be',
158+
Referer: 'https://www.pickx.be/'
159+
}
160+
})
161+
162+
if (response.status === 200) {
163+
apiVersion = response.data.version
164+
resolve()
165+
} else {
166+
console.error(`Failed to fetch API version. Status: ${response.status}`)
167+
reject(`Failed to fetch API version. Status: ${response.status}`)
190168
}
191169
} catch (error) {
192170
console.error('Error during fetchApiVersion:', error)

sites/pickx.be/pickx.be.test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
const { parser, url, request, fetchApiVersion, apiVersion } = require('./pickx.be.config.js')
1+
jest.mock('./pickx.be.config.js', () => {
2+
const originalModule = jest.requireActual('./pickx.be.config.js')
3+
return {
4+
...originalModule,
5+
fetchApiVersion: jest.fn(() => Promise.resolve())
6+
}
7+
})
8+
9+
const {
10+
parser,
11+
url,
12+
request,
13+
fetchApiVersion,
14+
setApiVersion,
15+
getApiVersion
16+
} = require('./pickx.be.config.js')
17+
218
const fs = require('fs')
319
const path = require('path')
420
const dayjs = require('dayjs')
@@ -13,12 +29,14 @@ const channel = {
1329
xmltv_id: 'Vedia.be'
1430
}
1531

32+
beforeEach(() => {
33+
setApiVersion('mockedApiVersion')
34+
})
35+
1636
it('can generate valid url', async () => {
17-
await fetchApiVersion()
1837
const generatedUrl = await url({ channel, date })
19-
const resolvedApiVersion = apiVersion()
2038
expect(generatedUrl).toBe(
21-
`https://px-epg.azureedge.net/airings/${resolvedApiVersion}/2023-12-13/channel/UID0118?timezone=Europe%2FBrussels`
39+
`https://px-epg.azureedge.net/airings/mockedApiVersion/2023-12-13/channel/UID0118?timezone=Europe%2FBrussels`
2240
)
2341
})
2442

0 commit comments

Comments
 (0)