From 5c903401658b802651d060959ca7a48a98ac3365 Mon Sep 17 00:00:00 2001 From: Michael Feinbier Date: Thu, 2 Jan 2025 20:30:21 +0100 Subject: [PATCH 1/3] fix: Use Axios in magentatv provider to obtain token and cookies --- .../web.magentatv.de.config.js | 93 +++++++++---------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/sites/web.magentatv.de/web.magentatv.de.config.js b/sites/web.magentatv.de/web.magentatv.de.config.js index b5ecbaa2..83d82831 100644 --- a/sites/web.magentatv.de/web.magentatv.de.config.js +++ b/sites/web.magentatv.de/web.magentatv.de.config.js @@ -2,8 +2,7 @@ const axios = require('axios') const dayjs = require('dayjs') const utc = require('dayjs/plugin/utc') const customParseFormat = require('dayjs/plugin/customParseFormat') -const fetch = require('node-fetch') -const { upperCase } = require('lodash') +const { upperCase, method, head } = require('lodash') let X_CSRFTOKEN let COOKIE @@ -165,11 +164,10 @@ function parseItems(content) { return data.playbilllist } -// Function to try to fetch COOKIE and X_CSRFTOKEN -function fetchCookieAndToken() { - return fetch( - 'https://api.prod.sngtv.magentatv.de/EPG/JSON/Authenticate?SID=firstup&T=Windows_chrome_118', - { +async function fetchCookieAndToken() { + try { + const response = await axios.request({ + url: 'https://api.prod.sngtv.magentatv.de/EPG/JSON/Authenticate', headers: { accept: 'application/json, text/javascript, */*; q=0.01', 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8', @@ -180,51 +178,48 @@ function fetchCookieAndToken() { Referer: 'https://web.magentatv.de/', '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"}', - method: 'POST' - } - ) - .then(response => { - // Check if the response status is OK (2xx) - 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 - cookiesToExtract.forEach(cookieName => { - const regex = new RegExp(`${cookieName}=(.+?)(;|$)`) - const match = setCookieHeader.find(header => regex.test(header)) - - if (match) { - const cookieValue = regex.exec(match)[1] - extractedCookies[cookieName] = cookieValue - } - }) - - return response.json() + params: { + SID: 'firstup', + T: 'Windows_chrome_118' + }, + method: 'POST', + 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"}', }) - .then(data => { - 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.') + + + // Extract the cookies specified in cookiesToExtract + const setCookieHeader = response.headers['set-cookie'] || [] + let extractedCookies = [] + cookiesToExtract.forEach(cookieName => { + const regex = new RegExp(`${cookieName}=(.+?)(;|$)`) + const match = setCookieHeader.find(header => regex.test(header)) + + if (match) { + const cookieString = regex.exec(match)[0] + extractedCookies.push(cookieString) } }) - .catch(error => { - console.error(error) - }) + + + // check if we recieved a csrfToken only then store the values + if (!response.data.csrfToken) { + console.log('csrfToken not found in the response.') + return + } + + X_CSRFTOKEN = response.data.csrfToken + COOKIE = extractedCookies.join(' ') + + } catch(error) { + console.error(error) + } } -function setHeaders() { - return fetchCookieAndToken().then(() => { - return { - X_CSRFTOKEN: X_CSRFTOKEN, - 'Content-Type': 'application/json', - Cookie: COOKIE - } - }) +async function setHeaders() { + await fetchCookieAndToken() + return { + X_CSRFTOKEN: X_CSRFTOKEN, + 'Content-Type': 'application/json', + Cookie: COOKIE + } } From a738c3b211b9862739ebce5c64e380fcc2e428fe Mon Sep 17 00:00:00 2001 From: Michael Feinbier Date: Thu, 2 Jan 2025 20:59:58 +0100 Subject: [PATCH 2/3] Load the token and cookie only once --- .../web.magentatv.de.config.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sites/web.magentatv.de/web.magentatv.de.config.js b/sites/web.magentatv.de/web.magentatv.de.config.js index 83d82831..5aefde78 100644 --- a/sites/web.magentatv.de/web.magentatv.de.config.js +++ b/sites/web.magentatv.de/web.magentatv.de.config.js @@ -2,12 +2,11 @@ const axios = require('axios') const dayjs = require('dayjs') const utc = require('dayjs/plugin/utc') const customParseFormat = require('dayjs/plugin/customParseFormat') -const { upperCase, method, head } = require('lodash') +const { upperCase } = require('lodash') let X_CSRFTOKEN let COOKIE const cookiesToExtract = ['JSESSIONID', 'CSESSIONID', 'CSRFSESSION'] -const extractedCookies = {} dayjs.extend(utc) dayjs.extend(customParseFormat) @@ -165,6 +164,11 @@ function parseItems(content) { } async function fetchCookieAndToken() { + // Only fetch the cookies and csrfToken if they are not already set + if (X_CSRFTOKEN && COOKIE) { + return + } + try { const response = await axios.request({ url: 'https://api.prod.sngtv.magentatv.de/EPG/JSON/Authenticate', @@ -215,11 +219,12 @@ async function fetchCookieAndToken() { } } -async function setHeaders() { - await fetchCookieAndToken() - return { - X_CSRFTOKEN: X_CSRFTOKEN, - 'Content-Type': 'application/json', - Cookie: COOKIE - } +function setHeaders() { + return fetchCookieAndToken().then(() => { + return { + X_CSRFTOKEN: X_CSRFTOKEN, + 'Content-Type': 'application/json', + Cookie: COOKIE + } + }) } From 9a9d21dfe7781904d97da3934b8040d73bb73bba Mon Sep 17 00:00:00 2001 From: PopeyeTheSai10r <107763146+PopeyeTheSai10r@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:44:14 -0800 Subject: [PATCH 3/3] elcinema.com close #2541 --- sites/elcinema.com/elcinema.com.config.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sites/elcinema.com/elcinema.com.config.js b/sites/elcinema.com/elcinema.com.config.js index 02b9897f..3be42ef5 100644 --- a/sites/elcinema.com/elcinema.com.config.js +++ b/sites/elcinema.com/elcinema.com.config.js @@ -9,9 +9,14 @@ dayjs.extend(customParseFormat) dayjs.extend(timezone) dayjs.extend(utc) +const headers = { + 'User-Agent': +'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 OPR/115.0.0.0' } + module.exports = { site: 'elcinema.com', days: 2, + request: { headers }, url({ channel }) { const lang = channel.lang === 'en' ? 'en/' : '/'