Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route):新增新浪黑猫投诉热点 #17492

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/routes/sina/tousu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @Descripttion: description
* @Author: -0V
* @Date: 2024-11-01 10:59:51
* @LastEditTime: 2024-11-07 16:11:14
*/
import { Route } from '@/types';

import logger from '@/utils/logger';
import puppeteer from '@/utils/puppeteer';
import { load } from 'cheerio';

export const route: Route = {
path: '/tousu',
name: '黑猫投诉',
url: 'tousu.sina.com.cn',
example: '/sina/tousu',
maintainers: ['JianLinWei1'],
radar: [
{
source: ['tousu.sina.com.cn'],
},
],
handler,
};

async function handler() {
const browser = await puppeteer();
const page = await browser.newPage();
const link = 'https://tousu.sina.cn';
logger.http(`Requesting ${link}`);
await page.goto(link, {
waitUntil: 'networkidle2',
});
const response = await page.content();
page.close();
const $ = load(response);
Comment on lines +28 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary use of puppeteer. You should prioritise using APIs to obtain data instead of using puppeteer.

The API endpoint is https://tousu.sina.cn/api/index/feed and you can use query like

const generateRandomString = (length) => {
    let result = '';
    const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for (let index = 0; index < length; index++) {
        result += characters[Math.round(Math.random() * (characters.length - 1))];
    }
    return result;
};

const query = {
    ts: Date.now(),
    rs: generateRandomString(16),
    type: 1,
    page_size: 10,
    page: 1,
    };

const response = await ofetch(`${link}/api/index/feed`, {
    query: {
        ...query,
        signature: crypto
            .createHash('sha256')
            .update(['$d6eb7ff91ee257475%', ...Object.values(query)].sort().join(''))
            .digest('hex'),
        },
    });

and the data is available in response.result.data.lists

const blackcatCons = $('div.blackcat-con').toArray();
const items = blackcatCons.map((item) => {
const title = $(item).find('h1.blackcat-hot').text();
const link = $(item).find('a.box').attr('href');
const description = $(item).find('p').text() + $(item).find('ul.list').text();
const author = $(item).find('span.name').text();
return {
title,
link,
description,
author,
};
});
return {
title: `黑猫投诉-新浪旗下消费者服务平台`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: `黑猫投诉-新浪旗下消费者服务平台`,
title: '黑猫投诉-新浪旗下消费者服务平台',

link: `https://tousu.sina.cn`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make use of the declared variable

const link = 'https://tousu.sina.cn';

Suggested change
link: `https://tousu.sina.cn`,
link: link,

description: `feedId:74988831961543680+userId:74002328552935424`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: `feedId:74988831961543680+userId:74002328552935424`,

item: items,
};
}
Loading