去除注释

去除登录
This commit is contained in:
Nite07
2022-01-28 14:32:56 +08:00
parent b0be040c30
commit 8b1409aee2
3 changed files with 34 additions and 51 deletions
+4 -7
View File
@@ -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,
+3 -16
View File
@@ -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,
+27 -28
View File
@@ -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,
};