From 8b1409aee2f44335bca39efdab8d524e749ccb3e Mon Sep 17 00:00:00 2001 From: Nite07 Date: Fri, 28 Jan 2022 14:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=B3=A8=E9=87=8A=20?= =?UTF-8?q?=E5=8E=BB=E9=99=A4=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/v2/mobilism/forums.js | 11 +++----- lib/v2/mobilism/portal.js | 19 +++----------- lib/v2/mobilism/utils.js | 55 +++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/lib/v2/mobilism/forums.js b/lib/v2/mobilism/forums.js index 413fa923fe..03b3fa03fd 100644 --- a/lib/v2/mobilism/forums.js +++ b/lib/v2/mobilism/forums.js @@ -1,6 +1,5 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const utils = require('./utils'); const rootUrl = `https://forum.mobilism.org/viewforum.php?f=`; @@ -29,13 +28,10 @@ const categeoryMap = { module.exports = async (ctx) => { const { category, type } = ctx.params; const currentUrl = rootUrl + categeoryMap[category][type]; - const response = await got.get(currentUrl, { - headers: { - cookie: await utils.login(), - }, - }); + const response = await got.get(currentUrl); const $ = cheerio.load(cheerio.load(response.data)('tbody')[1]); const list = $('tbody > tr'); + const items = list .map((index, item) => { item = $(item); @@ -43,7 +39,7 @@ module.exports = async (ctx) => { const link = new URL(item.find('td').first().find('a[title]').attr('href'), 'https://forum.mobilism.org'); const date = item.find('td').first().find('small').text(); const author = item.find('td').first().find('a.username-coloured').text(); - const pubDate = timezone(parseDate(date, 'DD MMM YYYY, HH:mm'), 0); + const pubDate = timezone(utils.parseDate(date), 0); return { title, pubDate, @@ -52,6 +48,7 @@ module.exports = async (ctx) => { }; }) .get(); + ctx.state.data = { title: `Mobilism Forums ${utils.firstUpperCase(category)} ${utils.firstUpperCase(type)} Release`, link: currentUrl, diff --git a/lib/v2/mobilism/portal.js b/lib/v2/mobilism/portal.js index 1bfccf5866..cd4807f329 100644 --- a/lib/v2/mobilism/portal.js +++ b/lib/v2/mobilism/portal.js @@ -1,6 +1,5 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const utils = require('./utils'); @@ -10,12 +9,13 @@ const get = (data) => { const items = posts .map((index, item) => { item = $(item); + const date = item.find('div.well > span.muted > em').text(); return { title: item.find('div.page-header > h3').text(), link: new URL(item.find('div.well > a').attr('href'), 'https://forum.mobilism.org'), author: item.find('div.well > span.muted > em > strong').text().replace('Posted by: ', ''), description: item.find('div.well > div.postbody').text(), - pubDate: timezone(parseDate(item.find('div.well > span.muted > em').text().replace(item.find('div.well > span.muted > em > strong').text(), '').replace('at', '').replace('in', '').trim(), 'DD MMM YYYY, HH:mm'), 0), + pubDate: timezone(utils.parseDate(date), 0), }; }) .get(); @@ -24,23 +24,10 @@ const get = (data) => { module.exports = async (ctx) => { const { type } = ctx.params; - const cookie = await utils.login(); const url = `https://forum.mobilism.org/portal.php?mode=articles&block=${type}&sk=t&sd=d&start=0`; let items = []; - const response = await got.get(url, { - headers: { - cookie, - }, - }); + const response = await got.get(url); items = items.concat(get(response.data)); - - // response = await got.get(url.replace('0', '8'), { - // headers: { - // cookie, - // }, - // }); - // items = items.concat(get(response.data)); - ctx.state.data = { title: `Mobilism Portal ${utils.firstUpperCase(type)} Release`, link: url, diff --git a/lib/v2/mobilism/utils.js b/lib/v2/mobilism/utils.js index 56700c8c0b..4d2cb7487b 100644 --- a/lib/v2/mobilism/utils.js +++ b/lib/v2/mobilism/utils.js @@ -1,34 +1,33 @@ -const got = require('@/utils/got'); - -const login = async () => { - const res = await got.post('https://forum.mobilism.org/ucp.php?mode=login', { - form: { - username: 'Nite07', - password: 'hjwqgToCkGza2Y', - login: 'Login', - autologin: 'on', - sid: 'd0fc9abd2dc3148d2b8641f0bb075e64', - }, - }); - let cookie = ''; - const cookies = res.headers['set-cookie']; - for (let i = 3; i < cookies.length; i++) { - const reg = /ppcw_29d3s(.*?);/; - const res = reg.exec(cookies[i]); - if (res) { - cookie += 'ppcw_29d3s' + res[1] + '; '; - } - } - return cookie; -}; - -// const login = () => { -// return 'ppcw_29d3s_sid=734f915fb8aaad5be1fd49e57886bae1; ppcw_29d3s_k=888d0e8b5da2a04f; ppcw_29d3s_u=2160336; '; -// }; +const dayjs = require('dayjs'); const firstUpperCase = (str) => str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()); +const patterns = [ + { + regexp: /(\d+) minutes ago/, + handler: (minute) => dayjs().subtract(minute, 'minutes'), + }, + { + regexp: /Today, (\d+:\d+\s(am|pm))/, + handler: (hm) => dayjs(hm || '0:00 am', ['hh:m a', 'hh:mm a', 'h:m a', 'h:mm a']), + }, + { + regexp: /Yesterday, (\d+:\d+\s(am|pm))/, + handler: (hm) => dayjs(hm || '0:00 am', ['hh:m a', 'hh:mm a', 'h:m a', 'h:mm a']).subtract(1, 'day'), + }, +]; + +const parseDate = (date) => { + for (const pattern of patterns) { + const match = pattern.regexp.exec(date); + if (match !== null) { + return pattern.handler(match[1]).toDate(); + } + } + return dayjs(date, 'MMM Do, YYYY, h:mm a').toDate(); +}; + module.exports = { - login, firstUpperCase, + parseDate, };