mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-15 03:20:08 -04:00
fix: Use Axios in magentatv provider to obtain token and cookies
This commit is contained in:
parent
49a3902fb1
commit
5c90340165
1 changed files with 44 additions and 49 deletions
|
@ -2,8 +2,7 @@ const axios = require('axios')
|
||||||
const dayjs = require('dayjs')
|
const dayjs = require('dayjs')
|
||||||
const utc = require('dayjs/plugin/utc')
|
const utc = require('dayjs/plugin/utc')
|
||||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||||
const fetch = require('node-fetch')
|
const { upperCase, method, head } = require('lodash')
|
||||||
const { upperCase } = require('lodash')
|
|
||||||
|
|
||||||
let X_CSRFTOKEN
|
let X_CSRFTOKEN
|
||||||
let COOKIE
|
let COOKIE
|
||||||
|
@ -165,11 +164,10 @@ function parseItems(content) {
|
||||||
return data.playbilllist
|
return data.playbilllist
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to try to fetch COOKIE and X_CSRFTOKEN
|
async function fetchCookieAndToken() {
|
||||||
function fetchCookieAndToken() {
|
try {
|
||||||
return fetch(
|
const response = await axios.request({
|
||||||
'https://api.prod.sngtv.magentatv.de/EPG/JSON/Authenticate?SID=firstup&T=Windows_chrome_118',
|
url: 'https://api.prod.sngtv.magentatv.de/EPG/JSON/Authenticate',
|
||||||
{
|
|
||||||
headers: {
|
headers: {
|
||||||
accept: 'application/json, text/javascript, */*; q=0.01',
|
accept: 'application/json, text/javascript, */*; q=0.01',
|
||||||
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||||
|
@ -180,51 +178,48 @@ function fetchCookieAndToken() {
|
||||||
Referer: 'https://web.magentatv.de/',
|
Referer: 'https://web.magentatv.de/',
|
||||||
'Referrer-Policy': 'strict-origin-when-cross-origin'
|
'Referrer-Policy': 'strict-origin-when-cross-origin'
|
||||||
},
|
},
|
||||||
body: '{"terminalid":"00:00:00:00:00:00","mac":"00:00:00:00:00:00","terminaltype":"WEBTV","utcEnable":1,"timezone":"Etc/GMT0","userType":3,"terminalvendor":"Unknown"}',
|
params: {
|
||||||
method: 'POST'
|
SID: 'firstup',
|
||||||
}
|
T: 'Windows_chrome_118'
|
||||||
)
|
},
|
||||||
.then(response => {
|
method: 'POST',
|
||||||
// Check if the response status is OK (2xx)
|
data: '{"terminalid":"00:00:00:00:00:00","mac":"00:00:00:00:00:00","terminaltype":"WEBTV","utcEnable":1,"timezone":"Etc/GMT0","userType":3,"terminalvendor":"Unknown"}',
|
||||||
if (!response.ok) {
|
})
|
||||||
throw new Error('HTTP request failed')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the set-cookie header
|
|
||||||
const setCookieHeader = response.headers.raw()['set-cookie']
|
|
||||||
|
|
||||||
// Extract the cookies specified in cookiesToExtract
|
// Extract the cookies specified in cookiesToExtract
|
||||||
|
const setCookieHeader = response.headers['set-cookie'] || []
|
||||||
|
let extractedCookies = []
|
||||||
cookiesToExtract.forEach(cookieName => {
|
cookiesToExtract.forEach(cookieName => {
|
||||||
const regex = new RegExp(`${cookieName}=(.+?)(;|$)`)
|
const regex = new RegExp(`${cookieName}=(.+?)(;|$)`)
|
||||||
const match = setCookieHeader.find(header => regex.test(header))
|
const match = setCookieHeader.find(header => regex.test(header))
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
const cookieValue = regex.exec(match)[1]
|
const cookieString = regex.exec(match)[0]
|
||||||
extractedCookies[cookieName] = cookieValue
|
extractedCookies.push(cookieString)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return response.json()
|
|
||||||
})
|
// check if we recieved a csrfToken only then store the values
|
||||||
.then(data => {
|
if (!response.data.csrfToken) {
|
||||||
if (data.csrfToken) {
|
|
||||||
X_CSRFTOKEN = data.csrfToken
|
|
||||||
COOKIE = `JSESSIONID=${extractedCookies.JSESSIONID}; CSESSIONID=${extractedCookies.CSESSIONID}; CSRFSESSION=${extractedCookies.CSRFSESSION}; JSESSIONID=${extractedCookies.JSESSIONID};`
|
|
||||||
} else {
|
|
||||||
console.log('csrfToken not found in the response.')
|
console.log('csrfToken not found in the response.')
|
||||||
|
return
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(error => {
|
X_CSRFTOKEN = response.data.csrfToken
|
||||||
|
COOKIE = extractedCookies.join(' ')
|
||||||
|
|
||||||
|
} catch(error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setHeaders() {
|
async function setHeaders() {
|
||||||
return fetchCookieAndToken().then(() => {
|
await fetchCookieAndToken()
|
||||||
return {
|
return {
|
||||||
X_CSRFTOKEN: X_CSRFTOKEN,
|
X_CSRFTOKEN: X_CSRFTOKEN,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Cookie: COOKIE
|
Cookie: COOKIE
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue