Files
RSSHub/lib/routes/uchicago/current.ts
T
Tony cfcdc34e1f refactor: replace playwright with patchright (#21976)
* refactor: use patchwright

* refactor: migrate setRequestInterception and waitUntil

* fix: overwrite ua

* refactor: drop puppeteer cookie/requestfinished shims

Migrate page.cookies/setCookie and browser.cookies/setCookie call sites to the native context APIs (page.context().cookies/addCookies), rewrite requestfinished handlers to await request.response() instead of consuming the synchronous puppeteer-shaped payload, and remove the now-redundant browser.close context-cleanup wrapper. Playwright already handles all of these natively; the shims existed only for the puppeteer -> playwright migration.

* refactor: remove page.authenticate, page.setUserAgent,  browser.newPage(), browser.userAgent() shims

update cookie utility tests

* refactor: replace browser instance with context in Playwright usage across multiple routes

- Updated all instances of `browser` to `context` in Playwright-related functions for consistency and improved resource management.
- Ensured proper closure of context after operations to prevent memory leaks.
- Adjusted request interception methods to utilize the new context variable.

* fix: direct type import

* refactor: remove Puppeteer compatibility layers and unify page handling with Playwright

* fix: test case

* chore: bump patchright to v1.60.0

* chore: pin patchright to 1.59.1

nft can't resolve browser.json due to https://github.com/microsoft/playwright/pull/40093

* chore: pin to node 24.15 for playwright

browser install on node 24.16 stucks due to https://github.com/microsoft/playwright/issues/40724

* fix: update patchright and patchright-core to version 1.60.2 and adjust minify-docker.js for compatibility

* fix: import BrowserContext from patchright
2026-06-07 02:43:20 +08:00

109 lines
3.8 KiB
TypeScript

import { load } from 'cheerio';
import type { Route } from '@/types';
import cache from '@/utils/cache';
import logger from '@/utils/logger';
import { parseDate } from '@/utils/parse-date';
import playwright from '@/utils/playwright';
import { getCookies, setCookies } from '@/utils/playwright-utils';
export const route: Route = {
path: '/journals/current/:journal',
categories: ['journal'],
example: '/uchicago/journals/current/jpe',
parameters: { journal: 'Journal id, can be found in URL. [Browse journals by title](https://www.journals.uchicago.edu/action/showPublications)' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['journals.uchicago.edu/toc/:journal/current', 'journals.uchicago.edu/journal/:journal'],
},
],
name: 'Current Issue',
maintainers: ['TonyRL'],
handler,
};
async function handler(ctx) {
const journal = ctx.req.param('journal');
const baseUrl = 'https://www.journals.uchicago.edu';
const link = `${baseUrl}/toc/${journal}/current`;
const context = await playwright();
const page = await context.newPage();
await page.route('**/*', (route) => {
const request = route.request();
request.resourceType() === 'document' ? route.continue() : route.abort();
});
logger.http(`Requesting ${link}`);
await page.goto(link, {
waitUntil: 'domcontentloaded',
});
const response = await page.evaluate(() => document.documentElement.innerHTML);
const cookies = await getCookies(page);
await page.close();
const $ = load(response);
const list = $('.issue-item__title')
.toArray()
.map((item) => ({ link: `${baseUrl}${$(item).find('a').attr('href')}` }));
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const page = await context.newPage();
await setCookies(page, cookies, 'journals.uchicago.edu');
await page.route('**/*', (route) => {
const request = route.request();
request.resourceType() === 'document' ? route.continue() : route.abort();
});
logger.http(`Requesting ${item.link}`);
await page.goto(item.link, {
waitUntil: 'domcontentloaded',
referer: link,
});
const response = await page.evaluate(() => document.documentElement.innerHTML);
await page.close();
const $ = load(response);
item.title = $('head title').text();
item.pubDate = parseDate($('head meta[name="dc.Date"]').attr('content'));
item.doi = $('head meta[scheme="doi"]').attr('content');
item.author = $('.author-name span')
.toArray()
.map((author) => $(author).text())
.join(', ');
$('.figure__image').each((_, elem) => {
if (elem.attribs['data-lg-src']) {
$(elem).attr('src', `${baseUrl}${elem.attribs['data-lg-src']}`);
delete elem.attribs['data-lg-src'];
}
});
item.description = $('.article__body').html();
return item;
})
)
);
await context.close();
return {
title: $('head title').text(),
description: $('.jumbotron-journal-info').text(),
link,
image: $('head meta[property="og:image"]').attr('content'),
item: items,
language: $('html').attr('lang'),
};
}