fix(route/oschina): improve cookie handling (#17564)

This commit is contained in:
Tony 2024-11-12 19:47:47 -08:00 committed by GitHub
parent 489630a1b9
commit f6c2ebb4ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 7 deletions

View File

@ -1,10 +1,11 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { load } from 'cheerio';
import { fetchArticle } from '@/utils/wechat-mp';
import { config } from '@/config';
const configs = {
all: {
@ -73,17 +74,34 @@ export const route: Route = {
[osc_pl]: https://www.oschina.net/news/programming "开源中国 - 编程语言资讯"`,
};
const getCookie = () =>
cache.tryGet(
'oschina:cookie',
async () => {
const res = await ofetch.raw('https://www.oschina.net/news');
const cookie = res.headers
.getSetCookie()
.map((i) => i.split(';')[0])
.join('; ');
return cookie;
},
config.cache.routeExpire,
false
);
async function handler(ctx) {
const category = ctx.req.param('category') ?? 'all';
const config = configs[category];
const res = await got(config.ajaxUrl, {
const cookie = await getCookie();
const res = await ofetch(config.ajaxUrl, {
headers: {
Referer: config.link,
'X-Requested-With': 'XMLHttpRequest',
Cookie: cookie,
},
});
const $ = load(res.data);
const $ = load(res);
$('.ad-wrap').remove();
@ -104,23 +122,24 @@ async function handler(ctx) {
list.map((item) =>
cache.tryGet(item.link, async () => {
if (/^https?:\/\/(my|www)\.oschina.net\/.*$/.test(item.link)) {
const detail = await got(item.link, {
const detail = await ofetch(item.link, {
headers: {
Referer: config.link,
Cookie: cookie,
},
});
const content = load(detail.data);
const content = load(detail);
content('.ad-wrap').remove();
item.description = content('.article-detail').html();
item.author = content('.article-box__meta .item').first().text();
} else if (/^https?:\/\/gitee\.com\/.*$/.test(item.link)) {
const detail = await got(item.link, {
const detail = await ofetch(item.link, {
headers: {
Referer: config.link,
},
});
const content = load(detail.data);
const content = load(detail);
item.description = content('.file_content').html();
} else if (/^https?:\/\/osc\.cool\/.*$/.test(item.link)) {