From 0a307e83c6c3499ba11a31cb626dc838d196a68b Mon Sep 17 00:00:00 2001 From: junfengP <840282629@qq.com> Date: Sat, 21 Dec 2024 22:28:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20implement=20=E6=96=B0=E7=94=B5?= =?UTF-8?q?=E5=BD=B1=E5=A4=A9=E5=A0=82=20route=20with=20content=20scraping?= =?UTF-8?q?=20and=20caching=20(#17945)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): implement 新电影天堂 route with content scraping and caching - Added a new route for 电影天堂 (Movie Heaven) that scrapes movie data from ygdy8.net. - Implemented content loading with caching to optimize performance. - Created a namespace for the route with relevant metadata. - Removed the deprecated JavaScript version of the route to streamline the codebase. * fix(route): update 电影天堂 route for improved content scraping --------- Co-authored-by: junfengP --- lib/routes-deprecated/dytt/index.js | 60 ----------------------- lib/routes/dytt/index.ts | 75 +++++++++++++++++++++++++++++ lib/routes/dytt/namespace.ts | 7 +++ 3 files changed, 82 insertions(+), 60 deletions(-) delete mode 100644 lib/routes-deprecated/dytt/index.js create mode 100644 lib/routes/dytt/index.ts create mode 100644 lib/routes/dytt/namespace.ts diff --git a/lib/routes-deprecated/dytt/index.js b/lib/routes-deprecated/dytt/index.js deleted file mode 100644 index 8266c061942d9a..00000000000000 --- a/lib/routes-deprecated/dytt/index.js +++ /dev/null @@ -1,60 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); -const baseURL = 'https://www.ygdy8.net/index.html'; -async function load(link, ctx) { - const cache = await ctx.cache.get(link); - if (cache) { - return cache; - } - const response = await got.get(link, { - responseType: 'buffer', - }); - response.data = iconv.decode(response.data, 'gb2312'); - - const $ = cheerio.load(response.data); - - const description = $('div#Zoom').html(); - ctx.cache.set(link, description); - return description; -} - -module.exports = async (ctx) => { - const response = await got.get(baseURL, { - responseType: 'buffer', - }); - response.data = iconv.decode(response.data, 'gb2312'); - - const $ = cheerio.load(response.data); - const list = $('.co_content8 table tr').get(); - // 页面含有2个.co_content8 table - // 仅第一个table内第一个tr元素是广告连接 - // 去除该广告连接 - list.splice(0, 1); - // const list = $('.co_content8 table tr:not(:first-child)').get(); - const process = await Promise.all( - list.slice(0, 20).map(async (item) => { - const link = $(item).find('a:nth-of-type(2)'); - const itemUrl = 'https://www.ygdy8.net' + link.attr('href'); - const other = await load(itemUrl, ctx); - - return { - enclosure_url: String(other.match(/magnet:.*?(?=">)/)), - enclosure_type: 'application/x-bittorrent', - title: link.text(), - description: other, - pubDate: new Date($(item).find('font').text()).toUTCString(), - link: itemUrl, - }; - }) - ); - - const data = { - title: '电影天堂/阳光电影', - link: baseURL, - description: '电影天堂RSS', - item: process, - }; - - ctx.state.data = data; -}; diff --git a/lib/routes/dytt/index.ts b/lib/routes/dytt/index.ts new file mode 100644 index 00000000000000..d4c4828f227477 --- /dev/null +++ b/lib/routes/dytt/index.ts @@ -0,0 +1,75 @@ +import type { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import iconv from 'iconv-lite'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/', + categories: ['multimedia'], + example: '/dytt', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: true, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['ygdy8.net/index.html'], + }, + ], + name: '最新电影', + maintainers: ['junfengP'], + handler, +}; + +async function loadContent(link: string) { + const response = await got.get(link, { + responseType: 'buffer', + }); + const data = iconv.decode(response.data, 'gb2312'); + const $ = load(data); + return $('div#Zoom').html() || ''; +} + +async function handler() { + const baseURL = 'https://www.ygdy8.net/html/gndy/dyzz/index.html'; + const response = await got.get(baseURL, { + responseType: 'buffer', + }); + const data = iconv.decode(response.data, 'gb2312'); + + const $ = load(data); + const list = $('.co_content8 table tr b a').toArray(); + + const items = await Promise.all( + list.map(async (item) => { + const link = $(item); + const itemUrl = 'https://www.ygdy8.net' + link.attr('href'); + + return await cache.tryGet(itemUrl, async () => { + const description = await loadContent(itemUrl); + return { + enclosure_url: description.match(/magnet:.*?(?=">)/) || '', + enclosure_type: 'application/x-bittorrent', + title: link.text(), + description, + pubDate: parseDate($(item).find('font').text()), + link: itemUrl, + }; + }); + }) + ); + + return { + title: '电影天堂/阳光电影', + link: baseURL, + description: '电影天堂RSS', + item: items, + }; +} diff --git a/lib/routes/dytt/namespace.ts b/lib/routes/dytt/namespace.ts new file mode 100644 index 00000000000000..557b7d37ad4288 --- /dev/null +++ b/lib/routes/dytt/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '电影天堂', + url: 'www.ygdy8.net', + lang: 'zh-CN', +};