fix(route): dcfever use ofetch instead of got utils (#15194)

* fix(route): dcfever use ofetch instead of got utils

* fix: typo
This commit is contained in:
Tony
2024-04-12 01:50:07 +08:00
committed by GitHub
parent ef8b061ea2
commit 0a87eaf4be
5 changed files with 52 additions and 86 deletions
+14 -19
View File
@@ -1,6 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { baseUrl, parseItem } from './utils';
@@ -9,17 +8,15 @@ export const route: Route = {
categories: ['new-media'],
example: '/dcfever/news',
parameters: { type: '分類,預設為所有新聞' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '新聞中心',
maintainers: ['TonyRL'],
handler,
radar: [
{
source: ['dcfever.com/news/index.php'],
target: '/news',
},
],
description: `| 所有新聞 | 攝影器材 | 手機通訊 | 汽車熱話 | 攝影文化 | 影片攝錄 | 測試報告 | 生活科技 | 攝影技巧 |
| -------- | -------- | -------- | -------- | ----------- | ----------- | -------- | -------- | --------- |
| | camera | mobile | auto | photography | videography | reviews | gadget | technique |`,
@@ -28,12 +25,10 @@ export const route: Route = {
async function handler(ctx) {
const type = ctx.req.param('type');
const response = await got(`${baseUrl}/news/index.php`, {
searchParams: {
type: type ?? undefined,
},
});
const $ = load(response.data);
const link = new URL(`${baseUrl}/news/index.php`, baseUrl);
link.searchParams.append('type', type);
const response = await ofetch(link.href);
const $ = load(response);
const list = $('.col-md-left .title a')
.toArray()
@@ -41,18 +36,18 @@ async function handler(ctx) {
item = $(item);
return {
title: item.text(),
link: new URL(item.attr('href'), response.url).href,
link: new URL(item.attr('href'), link.href).href,
};
});
const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet)));
const items = await Promise.all(list.map((item) => parseItem(item)));
return {
title: `${$('.channel_nav')
.contents()
.filter((_, e) => e.nodeType === 3)
.text()} - ${$('head title').text()}`,
link: response.url,
link: link.href,
image: 'https://cdn10.dcfever.com/images/android_192.png',
item: items,
};
+7 -15
View File
@@ -1,6 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { baseUrl, parseItem } from './utils';
@@ -9,14 +8,6 @@ export const route: Route = {
categories: ['new-media'],
example: '/dcfever/reviews/cameras',
parameters: { type: '分類,預設為 `cameras`' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['dcfever.com/:type/reviews.php'],
@@ -34,8 +25,9 @@ export const route: Route = {
async function handler(ctx) {
const { type = 'cameras' } = ctx.req.param();
const response = await got(`${baseUrl}/${type}/reviews.php`);
const $ = load(response.data);
const link = `${baseUrl}/${type}/reviews.php`;
const response = await ofetch(link);
const $ = load(response);
const list = $('.col-md-left .title a')
.toArray()
@@ -43,15 +35,15 @@ async function handler(ctx) {
item = $(item);
return {
title: item.text(),
link: new URL(item.attr('href'), response.url).href,
link: new URL(item.attr('href'), link).href,
};
});
const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet)));
const items = await Promise.all(list.map((item) => parseItem(item)));
return {
title: $('head title').text(),
link: response.url,
link,
image: 'https://cdn10.dcfever.com/images/android_192.png',
item: items,
};
+11 -22
View File
@@ -1,6 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
// import { parseRelativeDate } from '@/utils/parse-date';
import { baseUrl, parseTradeItem } from './utils';
@@ -10,14 +9,6 @@ export const route: Route = {
categories: ['new-media'],
example: '/dcfever/trading/search/Sony',
parameters: { keyword: '關鍵字', mainCat: '主要分類 ID,見上表' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '二手市集 - 物品搜尋',
maintainers: ['TonyRL'],
handler,
@@ -26,15 +17,13 @@ export const route: Route = {
async function handler(ctx) {
const { keyword, mainCat } = ctx.req.param();
const response = await got(`${baseUrl}/trading/search.php`, {
searchParams: {
keyword,
type: 'all',
main_cat: mainCat,
form_action: 'search_action',
},
});
const $ = load(response.data);
const link = new URL(`${baseUrl}/trading/search.php`, baseUrl);
link.searchParams.append('keyword', keyword);
link.searchParams.append('type', 'all');
mainCat && link.searchParams.append('main_cat', mainCat);
link.searchParams.append('form_action', 'search_action');
const response = await ofetch(link.href);
const $ = load(response);
const list = $('.item_list li a')
.toArray()
@@ -43,16 +32,16 @@ async function handler(ctx) {
item.find('.optional').remove();
return {
title: item.find('.trade_title').text(),
link: new URL(item.attr('href'), response.url).href,
link: new URL(item.attr('href'), link.href).href,
author: item.find('.trade_info').text(),
};
});
const items = await Promise.all(list.map((item) => parseTradeItem(item, cache.tryGet)));
const items = await Promise.all(list.map((item) => parseTradeItem(item)));
return {
title: $('head title').text(),
link: response.url,
link: link.href,
image: 'https://cdn10.dcfever.com/images/android_192.png',
item: items,
};
+10 -21
View File
@@ -1,6 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
// import { parseRelativeDate } from '@/utils/parse-date';
import { baseUrl, parseTradeItem } from './utils';
@@ -10,14 +9,6 @@ export const route: Route = {
categories: ['new-media'],
example: '/dcfever/trading/1',
parameters: { id: '分類 ID,見下表' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '二手市集',
maintainers: ['TonyRL'],
handler,
@@ -31,14 +22,12 @@ export const route: Route = {
async function handler(ctx) {
const { id, order = 'new' } = ctx.req.param();
const response = await got(`${baseUrl}/trading/listing.php`, {
searchParams: {
id,
order,
type: 'all',
},
});
const $ = load(response.data);
const link = new URL(`${baseUrl}/trading/listing.php`, baseUrl);
link.searchParams.append('id', id);
link.searchParams.append('order', order);
link.searchParams.append('type', 'all');
const response = await ofetch(link.href);
const $ = load(response);
const list = $('.item_list li a')
.toArray()
@@ -48,16 +37,16 @@ async function handler(ctx) {
item.find('.optional').remove();
return {
title: item.find('.trade_title').text(),
link: new URL(item.attr('href'), response.url).href,
link: new URL(item.attr('href'), link.href).href,
author: item.find('.trade_info').text(),
};
});
const items = await Promise.all(list.map((item) => parseTradeItem(item, cache.tryGet)));
const items = await Promise.all(list.map((item) => parseTradeItem(item)));
return {
title: $('head title').text(),
link: response.url,
link: link.href,
image: 'https://cdn10.dcfever.com/images/android_192.png',
item: items,
};
+10 -9
View File
@@ -1,17 +1,18 @@
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import cache from '@/utils/cache';
const baseUrl = 'https://www.dcfever.com';
const parseItem = (item, tryGet) =>
tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const parseItem = (item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);
const content = $('div[itemprop="articleBody"], .column_article_content_html');
@@ -23,7 +24,7 @@ const parseItem = (item, tryGet) =>
if (pageLinks.length) {
const pages = await Promise.all(
pageLinks.map(async (pageLink) => {
const { data: response } = await got(pageLink.link);
const response = await ofetch(pageLink.link);
const $ = load(response);
return $('div[itemprop="articleBody"]').html();
})
@@ -32,7 +33,7 @@ const parseItem = (item, tryGet) =>
}
content.find('img').each((_, e) => {
if (e.attribs.src.includes('?')) {
if (e.attribs.src?.includes('?')) {
e.attribs.src = e.attribs.src.split('?')[0];
}
});
@@ -57,9 +58,9 @@ const parseItem = (item, tryGet) =>
return item;
});
const parseTradeItem = (item, tryGet) =>
tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const parseTradeItem = (item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);
$('.selector_text').remove();