diff --git a/lib/routes/u3c3/index.ts b/lib/routes/u3c3/index.ts index 01fea524c..cc88b6ad0 100644 --- a/lib/routes/u3c3/index.ts +++ b/lib/routes/u3c3/index.ts @@ -23,7 +23,7 @@ export const route: Route = { }; async function handler(ctx) { - const { type, keywoard, preview } = ctx.req.param(); + const { type, keyword, preview } = ctx.req.param(); // should be: // undefined // U3C3 @@ -36,9 +36,18 @@ async function handler(ctx) { const rootURL = 'https://www.u3c3.com'; let currentURL; let title; - if (keywoard) { - currentURL = `${rootURL}/?search=${keywoard}`; - title = `search ${keywoard} - u3c3`; + if (keyword) { + // u3c3 search needs an anti-scrape token: its search box (search21() JS on the + // homepage) redirects to /?search2=&search=. The token rotates and is + // embedded in the page, so scrape it live; hitting /?search= alone silently + // returns the homepage listing (the long-standing "always 國產原创" bug). + const { data: home } = await got(rootURL); + const varName = home.match(/search2="\s*\+\s*(\w+)/)?.[1]; + const token = varName ? home.match(new RegExp(String.raw`(? div.container > div.table-responsive > table > tbody > tr') .toArray() - .map((item) => { - item = $(item); - const title = item.find('td:nth-of-type(2) > a ').attr('title'); - const guid = rootURL + item.find('td:nth-of-type(2) > a').attr('href'); - const link = guid; - const pubDate = item.find('td:nth-of-type(5)').text(); - const enclosure_url = item.find('td:nth-of-type(3) > a:nth-of-type(2)').attr('href'); + // Skip u3c3's sticky promo rows that appear on every page (home AND search): + // category-nav rows (href="/?type=…") and external ad rows (href="http://…", + // e.g. the "國產原创" entries). Real torrent rows always link to /view. + .filter((el) => $(el).find('td:nth-of-type(2) > a').attr('href')?.startsWith('/view')) + .map((el) => { + const item = $(el); + const a = item.find('td:nth-of-type(2) > a'); + const guid = rootURL + a.attr('href'); return { - title, + title: a.attr('title'), guid, - link, - pubDate, - - enclosure_url, + link: guid, + pubDate: item.find('td:nth-of-type(5)').text(), + enclosure_url: item.find('td:nth-of-type(3) > a:nth-of-type(2)').attr('href'), enclosure_type: 'application/x-bittorrent', }; });