Skip to content

Commit

Permalink
feat(route/dianping): add dianping user route
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Oct 31, 2024
1 parent 7ff06d2 commit a69ff2f
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export type Config = {
civitai: {
cookie?: string;
};
dianping: {
cookie?: string;
};
dida365: {
username?: string;
password?: string;
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions lib/routes/dianping/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '大众点评',
url: 'dianping.com',
};
148 changes: 148 additions & 0 deletions lib/routes/dianping/user.ts
Original file line number Diff line number Diff line change
@@ -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) => `<img src="${ele.picUrl}" />`).join('<br>') : '';
content += item.videoUrl ? `<img src="${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<number, string> = {
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<string, string> = {
'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 ? `地点:<a href="http://www.dianping.com/shop/${item.poi.shopId}">${item.poi.name} - ${item.poi.regionCategory}</a>` : '';
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, '<br>') + '<br>';
content += `评分:${starMap[item.star]}<br>`;
content += poi + '<br>';
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, '<br>') + '<br>';
content += addPictureAndVideo(item);
title = `发布点评: ${content}`;

break;

case 4208:
// 发布攻略
link = `https://m.dianping.com/cityinsight/${item.mainId}`;
content = item.content.replaceAll(/\n+/g, '<br>') + '<br>';
content += poi + '<br>';
content += item.moreDesc ? `<a href='${link}'>${item.moreDesc}</a><br>` : '';
content += addPictureAndVideo(item);
title = `发布攻略: ${poiName}`;

break;

default:
// Do nothing
}

return {
description: content,
title,
link,
};
});

return {
title: `大众点评 - ${nickName}`,
link: userPage,
description: `大众点评 - ${nickName}`,
item: items,
};
}

0 comments on commit a69ff2f

Please sign in to comment.