From 5afb04400cb78560a5216eaf93c775079cfd361e Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 5 Oct 2022 10:12:23 -0900 Subject: [PATCH] fix(route): cw cookie (#11011) --- lib/v2/cw/master.js | 5 ++++- lib/v2/cw/sub.js | 5 ++++- lib/v2/cw/today.js | 5 ++++- lib/v2/cw/utils.js | 16 ++++++++++++++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/v2/cw/master.js b/lib/v2/cw/master.js index 6530807d9..e2e81022e 100644 --- a/lib/v2/cw/master.js +++ b/lib/v2/cw/master.js @@ -1,9 +1,12 @@ const cheerio = require('cheerio'); -const { baseUrl, cookieJar, got, parseList, parseItems } = require('./utils'); +const { baseUrl, cookieJar, got, parseList, parseItems, getCookie } = require('./utils'); module.exports = async (ctx) => { const { channel } = ctx.params; const pageUrl = `${baseUrl}/masterChannel.action`; + if (!cookieJar) { + await getCookie(); + } const { data: response } = await got(pageUrl, { headers: { Referer: baseUrl, diff --git a/lib/v2/cw/sub.js b/lib/v2/cw/sub.js index 7d4d9b0ad..f995171f6 100644 --- a/lib/v2/cw/sub.js +++ b/lib/v2/cw/sub.js @@ -1,9 +1,12 @@ const cheerio = require('cheerio'); -const { baseUrl, cookieJar, got, parseList, parseItems } = require('./utils'); +const { baseUrl, cookieJar, got, parseList, parseItems, getCookie } = require('./utils'); module.exports = async (ctx) => { const { channel } = ctx.params; const pageUrl = `${baseUrl}/subchannel.action`; + if (!cookieJar) { + await getCookie(); + } const { data: response } = await got(pageUrl, { headers: { Referer: baseUrl, diff --git a/lib/v2/cw/today.js b/lib/v2/cw/today.js index 5aaa7b06f..6e494e37b 100644 --- a/lib/v2/cw/today.js +++ b/lib/v2/cw/today.js @@ -1,8 +1,11 @@ const cheerio = require('cheerio'); -const { baseUrl, cookieJar, got, parseList, parseItems } = require('./utils'); +const { baseUrl, cookieJar, got, parseList, parseItems, getCookie } = require('./utils'); module.exports = async (ctx) => { const pageUrl = `${baseUrl}/today`; + if (!cookieJar) { + await getCookie(); + } const { data: response } = await got(pageUrl, { headers: { Referer: baseUrl, diff --git a/lib/v2/cw/utils.js b/lib/v2/cw/utils.js index e3263c870..2e583e9dd 100644 --- a/lib/v2/cw/utils.js +++ b/lib/v2/cw/utils.js @@ -1,7 +1,7 @@ const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); -const { CookieJar } = require('tough-cookie'); -const cookieJar = new CookieJar(); +const { Cookie, CookieJar } = require('tough-cookie'); +let cookieJar; const config = require('@/config').value; const baseUrl = 'https://www.cw.com.tw'; @@ -12,6 +12,17 @@ const got = require('@/utils/got').extend({ }, }); +const getCookie = async () => { + const response = await got(`${baseUrl}/user/get/cookie-bar`); + const cookies = response.headers['set-cookie']; + if (Array.isArray(cookies)) { + cookieJar = cookies.map(Cookie.parse); + } else { + cookieJar = [Cookie.parse(cookieJar)]; + } + cookieJar = CookieJar.fromJSON({ cookies: cookieJar }); +}; + const parseList = ($, limit) => $('.caption') .toArray() @@ -58,6 +69,7 @@ module.exports = { baseUrl, cookieJar, got, + getCookie, parseList, parseItems, };