Skip to content

Commit

Permalink
fix(route/oschina): improve cookie handling (#17564)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Nov 13, 2024
1 parent 489630a commit f6c2ebb
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/routes/oschina/news.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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();

Expand All @@ -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)) {
Expand Down

0 comments on commit f6c2ebb

Please sign in to comment.