-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path下厨房.js
58 lines (49 loc) · 1.62 KB
/
下厨房.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @author YG
* @name 下厨房
* @team YG
* @version 1.0.0
* @description 查询下厨房菜谱
* @rule ^(.+怎么做)$
* @admin true
*/
const axios = require('axios');
const cheerio = require('cheerio');
module.exports = async sender => {
const keyword = sender.param(1).replace('怎么做', '').trim();
const url = `https://www.xiachufang.com/search/?keyword=${encodeURIComponent(keyword)}`;
try {
const { data } = await axios.get(url);
const $ = cheerio.load(data);
const firstResult = $('.normal-recipe-list li').first();
const recipeLink = firstResult.find('a').attr('href');
const recipeTitle = firstResult.find('.name').text().trim();
const recipeUrl = `https://www.xiachufang.com${recipeLink}`;
const recipePage = await axios.get(recipeUrl);
const $$ = cheerio.load(recipePage.data);
const recipeImg = $$('div.cover img').attr('src');
const ingredients = $$('.ings li').map((i, el) => $(el).text().trim()).get().join('\n');
const steps = $$('.steps li').map((i, el) => $$(el).text().trim()).get().join('\n\n');
const message = `
【${recipeTitle}】
原料:
${ingredients}
步骤:
${steps}
更多详情请访问:${recipeUrl}
`;
await sender.reply({
type: 'text',
msg: message
});
if (recipeImg) {
await sender.reply({
type: 'image',
path: recipeImg
});
}
} catch (error) {
console.error(error);
await sender.reply('无法获取该菜谱的信息,请稍后再试。');
}
};