From a69ff2fa7bae1835ba0bfdbd6b6b7f2793bdfa69 Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Thu, 31 Oct 2024 13:27:57 +0700 Subject: [PATCH] feat(route/dianping): add dianping user route --- lib/config.ts | 6 ++ lib/routes/dianping/namespace.ts | 6 ++ lib/routes/dianping/user.ts | 148 +++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 lib/routes/dianping/namespace.ts create mode 100644 lib/routes/dianping/user.ts diff --git a/lib/config.ts b/lib/config.ts index 217bb65e293328..2d7437bdf16f75 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -97,6 +97,9 @@ export type Config = { civitai: { cookie?: string; }; + dianping: { + cookie?: string; + }; dida365: { username?: string; password?: string; @@ -500,6 +503,9 @@ const calculateValue = () => { civitai: { cookie: envs.CIVITAI_COOKIE, }, + dianping: { + cookie: envs.DIANPING_COOKIE, + }, dida365: { username: envs.DIDA365_USERNAME, password: envs.DIDA365_PASSWORD, diff --git a/lib/routes/dianping/namespace.ts b/lib/routes/dianping/namespace.ts new file mode 100644 index 00000000000000..e73a9f7d9360b7 --- /dev/null +++ b/lib/routes/dianping/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '大众点评', + url: 'dianping.com', +}; diff --git a/lib/routes/dianping/user.ts b/lib/routes/dianping/user.ts new file mode 100644 index 00000000000000..8a0de081ec796e --- /dev/null +++ b/lib/routes/dianping/user.ts @@ -0,0 +1,148 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { config } from '@/config'; + +function addPictureAndVideo(item: any) { + let content = ''; + content += item.pictureList ? item.pictureList.map((ele: any) => ``).join('
') : ''; + content += item.videoUrl ? `` : ''; + return content; +} + +export const route: Route = { + path: '/user/:id', + categories: ['shopping'], + example: '/dianping/user/808259118', + parameters: { id: 'User id' }, + features: { + requireConfig: [ + { + name: 'DIANPING_COOKIE', + optional: false, + description: 'Cookie for Dianping', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['dianping.com/member/:id'], + target: '/dianping/user/:id', + }, + ], + name: 'User', + maintainers: ['pseudoyu'], + handler, + description: 'Get user reviews, check-ins, guides.', +}; + +const starMap: Record = { + 0: '无', + 10: '一星', + 20: '二星', + 30: '三星', + 35: '三星半', + 40: '四星', + 45: '四星半', + 50: '五星', +}; + +async function handler(ctx) { + const id = ctx.req.param('id'); + + const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'; + const userPage = `https://m.dianping.com/userprofile/${id}`; + const cookie = config.dianping.cookie; + + const headers: Record = { + 'User-Agent': userAgent, + Referer: userPage, + }; + + if (cookie) { + headers.Cookie = cookie; + } + + const pageResponse = await ofetch(userPage, { + headers, + }); + + const nickNameReg = /window\.nickName = "(.*?)"/g; + const nickName = nickNameReg.exec(pageResponse as string)?.[1]; + + const response = await ofetch(`https://m.dianping.com/member/ajax/NobleUserFeeds?userId=${id}`, { + headers, + }); + + const data = response.data; + + const items = data.map((item: any) => { + let link = ''; + let title = ''; + let content = ''; + + const poi = item.poi ? `地点:${item.poi.name} - ${item.poi.regionCategory}` : ''; + const poiName = item.poi ? item.poi.name : ''; + + switch (item.feedType) { + case 1101: + // 签到成功 + link = `https://m.dianping.com/ugcdetail/${item.mainId}?sceneType=0&bizType=3`; + content = poi; + title = `签到成功: ${poiName} `; + + break; + + case 101: + // 对商户、地点发布点评 + link = `https://m.dianping.com/ugcdetail/${item.mainId}?sceneType=0&bizType=1`; + content = item.content.replaceAll(/\n+/g, '
') + '
'; + content += `评分:${starMap[item.star]}
`; + content += poi + '
'; + content += addPictureAndVideo(item); + title = `发布点评: ${poiName}`; + + break; + + case 131: + // 发布点评 + link = `https://m.dianping.com/ugcdetail/${item.mainId}?sceneType=0&bizType=29`; + content = item.content.replaceAll(/\n+/g, '
') + '
'; + content += addPictureAndVideo(item); + title = `发布点评: ${content}`; + + break; + + case 4208: + // 发布攻略 + link = `https://m.dianping.com/cityinsight/${item.mainId}`; + content = item.content.replaceAll(/\n+/g, '
') + '
'; + content += poi + '
'; + content += item.moreDesc ? `${item.moreDesc}
` : ''; + content += addPictureAndVideo(item); + title = `发布攻略: ${poiName}`; + + break; + + default: + // Do nothing + } + + return { + description: content, + title, + link, + }; + }); + + return { + title: `大众点评 - ${nickName}`, + link: userPage, + description: `大众点评 - ${nickName}`, + item: items, + }; +}