Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: luogu route parse error #14170

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/v2/luogu/daily.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const md = require('markdown-it')({
html: true,
xhtmlOut: true,
breaks: true,
linkify: true,
});

module.exports = async (ctx) => {
const id = ctx.params.id ?? 47327;
Expand All @@ -9,19 +15,30 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response.data);
const title = $('head title').text();

const firstPost = $('.am-comment-main .am-comment-bd').first();
const dailyLink = firstPost.find('a').first().attr('href');
const issueHeading = firstPost.find('h1').text().trim();
const injection_script = $('head script')
.filter((_, el) => $(el).text().startsWith('window._feInjection'))
.first()
.text();
sakurayang marked this conversation as resolved.
Show resolved Hide resolved
// the window._feConfigVersion and window._tagVersion seems to be a constant, but in cast it will change, write a regexp to match it.
const JSONRaw = injection_script.replaceAll('window._feInjection = JSON.parse(decodeURIComponent("', '').replace(/"\)\);window._feConfigVersion=(\d*);window._tagVersion=(\d*);/g, '');
sakurayang marked this conversation as resolved.
Show resolved Hide resolved
const JSONDecode = JSON.parse(decodeURIComponent(JSONRaw));

const MDRaw = JSONDecode.currentData.post.content;
const MDDecode = md.render(MDRaw).replaceAll('\n', '<br />');
sakurayang marked this conversation as resolved.
Show resolved Hide resolved
sakurayang marked this conversation as resolved.
Show resolved Hide resolved

const $firstPost = cheerio.load(MDDecode);
const issueHeading = $firstPost('strong').first().text().trim();
const dailyLink = $firstPost.root().find('a').first().attr('href');
const { data: dailyResponse } = await got(dailyLink);
const $daily = cheerio.load(dailyResponse);
const item = [
{
title,
description: $daily('#article-content').html(),
link,
author: firstPost.find('p').eq(1).text(),
author: JSONDecode.currentData.post.author.name,
guid: `${link}#${issueHeading}`,
pubDate: parseDate(issueHeading.match(/(\d{4} 年 \d{1,2} 月 \d{1,2} 日)/)[1], 'YYYY 年 M 月 D 日'),
pubDate: parseDate(JSONDecode.currentData.post.time),
},
];

Expand Down