From edb6e5deecfed2d64a8b8c4686769597a5500834 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 14 Apr 2022 20:19:33 -0300 Subject: [PATCH] fix(route): add debug json output and cookie support (#9539) * fix(route): add debug json output and cookie support * Update lib/v2/qbittorrent/news.js --- lib/v2/agirls/index.js | 6 ++++++ lib/v2/firefox/index.js | 9 +++++++-- lib/v2/github/comments.js | 7 +++++++ lib/v2/gitpod/blog.js | 8 ++++++++ lib/v2/hkej/index.js | 24 +++++++++--------------- lib/v2/hkepc/index.js | 8 ++++++++ lib/v2/hket/index.js | 12 ++++++++++-- lib/v2/qbittorrent/news.js | 6 ++++++ 8 files changed, 61 insertions(+), 19 deletions(-) diff --git a/lib/v2/agirls/index.js b/lib/v2/agirls/index.js index d51c782c5..ad2ea76d8 100644 --- a/lib/v2/agirls/index.js +++ b/lib/v2/agirls/index.js @@ -4,6 +4,9 @@ const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const path = require('path'); const { art } = require('@/utils/render'); +const { CookieJar } = require('tough-cookie'); + +const cookieJar = new CookieJar(); module.exports = async (ctx) => { const baseUrl = 'https://agirls.aotter.net'; @@ -14,6 +17,7 @@ module.exports = async (ctx) => { headers: { Referer: baseUrl, }, + cookieJar, }); const $ = cheerio.load(response.data); @@ -36,6 +40,7 @@ module.exports = async (ctx) => { headers: { Referer: baseUrl + '/posts/' + (category ? `/${category}` : ''), }, + cookieJar, }); const content = cheerio.load(detailResponse.data); @@ -67,5 +72,6 @@ module.exports = async (ctx) => { description: $('head meta[name=description]').attr('content'), item: items, language: $('html').attr('lang'), + cookieJar, }; }; diff --git a/lib/v2/firefox/index.js b/lib/v2/firefox/index.js index debb9e980..7a28b7634 100644 --- a/lib/v2/firefox/index.js +++ b/lib/v2/firefox/index.js @@ -4,6 +4,9 @@ const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const { art } = require('@/utils/render'); const path = require('path'); +const { CookieJar } = require('tough-cookie'); + +const cookieJar = new CookieJar(); module.exports = async (ctx) => { const baseUrl = 'https://monitor.firefox.com'; @@ -13,6 +16,7 @@ module.exports = async (ctx) => { headers: { Referer: baseUrl, }, + cookieJar, }); const $ = cheerio.load(response.body); @@ -35,6 +39,7 @@ module.exports = async (ctx) => { headers: { Referer: baseUrl + '/breaches', }, + cookieJar, }); const content = cheerio.load(article.body); @@ -54,7 +59,7 @@ module.exports = async (ctx) => { ctx.state.data = { title: $('title').text(), link: baseUrl, - description: String($('head meta[name=description]').attr('content').trim()), + description: $('head meta[name=description]').attr('content').trim(), item: items, image: $('head meta[property=og:image]').attr('content'), }; @@ -62,7 +67,7 @@ module.exports = async (ctx) => { ctx.state.json = { title: $('title').text(), link: baseUrl, - description: String($('head meta[name=description]').attr('content').trim()), + description: $('head meta[name=description]').attr('content').trim(), item: items, image: $('head meta[property=og:image]').attr('content'), }; diff --git a/lib/v2/github/comments.js b/lib/v2/github/comments.js index d0c148779..5ef814d5f 100644 --- a/lib/v2/github/comments.js +++ b/lib/v2/github/comments.js @@ -118,5 +118,12 @@ module.exports = async (ctx) => { title: `${user}/${repo}: ${typeDict[type].title} #${number} - ${issue.title}`, link: issue.html_url, item: items, + rateLimit: { + limit: parseInt(response.headers['x-ratelimit-limit']), + remaining: parseInt(response.headers['x-ratelimit-remaining']), + reset: parseDate(parseInt(response.headers['x-ratelimit-reset']) * 1000), + resoure: response.headers['x-ratelimit-resource'], + used: parseInt(response.headers['x-ratelimit-used']), + }, }; }; diff --git a/lib/v2/gitpod/blog.js b/lib/v2/gitpod/blog.js index c4ccf225b..f57f12640 100644 --- a/lib/v2/gitpod/blog.js +++ b/lib/v2/gitpod/blog.js @@ -49,4 +49,12 @@ module.exports = async (ctx) => { language: 'en-US', item: items, }; + + ctx.state.json = { + title: $('title').text(), + link: rootUrl + '/blog', + description: $('meta[name="description"]').attr('content'), + language: 'en-US', + item: items, + }; }; diff --git a/lib/v2/hkej/index.js b/lib/v2/hkej/index.js index afbfca634..3f589a7e8 100644 --- a/lib/v2/hkej/index.js +++ b/lib/v2/hkej/index.js @@ -1,11 +1,12 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const dayjs = require('dayjs'); -const utc = require('dayjs/plugin/utc'); -const { parseDate } = require('@/utils/parse-date'); +const { parseDate, parseRelativeDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const { art } = require('@/utils/render'); const path = require('path'); +const { CookieJar } = require('tough-cookie'); + +const cookieJar = new CookieJar(); const categories = { index: { @@ -57,16 +58,13 @@ module.exports = async (ctx) => { const cat = categories[category]; const baseUrl = 'https://www2.hkej.com'; - dayjs.extend(utc); - const yesterday = dayjs().utcOffset(8).subtract(1, 'day').format('YYYY-MM-DD'); - const today = dayjs().utcOffset(8).format('YYYY-MM-DD'); - const response = await got({ method: 'get', url: baseUrl + cat.link, headers: { Referer: baseUrl, }, + cookieJar, }); const $ = cheerio.load(response.data); @@ -102,6 +100,7 @@ module.exports = async (ctx) => { headers: { Referer: cat.link, }, + cookieJar, }); const content = cheerio.load(article.data); @@ -123,19 +122,13 @@ module.exports = async (ctx) => { }) .get(); - const pubDate = content('p.info span.date') - .text() - .trim() - .replace('日', '') - .replace('今', today) - .replace('昨', yesterday) - .replace(/[年月]/g, '-'); + const pubDate = content('p.info span.date').text().trim(); item.category = content('p.info span.cate a') .toArray() .map((e) => content(e).text().trim()); item.description = renderDesc(articleImg, content('div#article-content').html()); - item.pubDate = timezone(parseDate(pubDate), +8); + item.pubDate = timezone(/(今|昨)/.test(pubDate) ? parseRelativeDate(pubDate) : parseDate(pubDate, 'YYYY M D'), +8); return item; }) @@ -156,5 +149,6 @@ module.exports = async (ctx) => { description: `信報網站(www.hkej.com)即時新聞${cat.name},提供${cat.description}。`, item: items, language: 'zh-hk', + cookieJar, }; }; diff --git a/lib/v2/hkepc/index.js b/lib/v2/hkepc/index.js index 2d9fab1f4..677e08289 100644 --- a/lib/v2/hkepc/index.js +++ b/lib/v2/hkepc/index.js @@ -195,4 +195,12 @@ module.exports = async (ctx) => { language: 'zh-hk', item: items, }; + + ctx.state.json = { + title: feedTitle, + link: `https://www.hkepc.com/${category}`, + description: '電腦領域 HKEPC Hardware - 全港 No.1 PC網站', + language: 'zh-hk', + item: items, + }; }; diff --git a/lib/v2/hket/index.js b/lib/v2/hket/index.js index 47a233cd6..3997b17b8 100644 --- a/lib/v2/hket/index.js +++ b/lib/v2/hket/index.js @@ -105,9 +105,17 @@ module.exports = async (ctx) => { ); ctx.state.data = { - title: String($('head meta[name=title]').attr('content').trim()), + title: $('head meta[name=title]').attr('content').trim(), link: baseUrl + '/' + category, - description: String($('head meta[name=description]').attr('content').trim()), + description: $('head meta[name=description]').attr('content').trim(), + item: items, + language: 'zh-hk', + }; + + ctx.state.json = { + title: $('head meta[name=title]').attr('content').trim(), + link: baseUrl + '/' + category, + description: $('head meta[name=description]').attr('content').trim(), item: items, language: 'zh-hk', }; diff --git a/lib/v2/qbittorrent/news.js b/lib/v2/qbittorrent/news.js index 6fd530af8..5391adb2a 100644 --- a/lib/v2/qbittorrent/news.js +++ b/lib/v2/qbittorrent/news.js @@ -50,4 +50,10 @@ module.exports = async (ctx) => { link: `${baseUrl}/news.php`, item, }; + + ctx.state.json = { + title: 'qBittorrent News', + link: `${baseUrl}/news.php`, + item, + }; };