-
Notifications
You must be signed in to change notification settings - Fork 78
/
Oil.js
117 lines (100 loc) · 4.99 KB
/
Oil.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Surge Panel:https://raw.githubusercontent.com/githubdulong/Script/master/Surge/Oil.sgmodule
今日油价,仅限Surge Panel使用
*/
const params = getParams($argument);
const provinceName = params.provname || "湖南";
const apiKey = params.apikey; // 使用模块参数填写Apikey.申请地址:https://www.tianapi.com/apiview/104 (该接口普通会员每天赠送100次调用额度);
const apiUrls = [
`https://apis.tianapi.com/oilprice/index?key=${apiKey}&prov=${encodeURIComponent(provinceName)}`,
`https://apis.tianapi.com/oilprice/index?key=231de491563c35731436829ac52aad43&prov=${encodeURIComponent(provinceName)}`,
`https://apis.tianapi.com/oilprice/index?key=a2bc7a0e01be908881ff752677cf94b7&prov=${encodeURIComponent(provinceName)}`,
`https://apis.tianapi.com/oilprice/index?key=1bcc67c0114bc39a8818c8be12c2c9ac&prov=${encodeURIComponent(provinceName)}`,
`https://apis.tianapi.com/oilprice/index?key=3c5ee42145c852de4147264f25b858dc&prov=${encodeURIComponent(provinceName)}`,
`https://apis.tianapi.com/oilprice/index?key=d718b0f7c2b6d71cb3a9814e90bf847f&prov=${encodeURIComponent(provinceName)}`
];
let currentIndex = 0;
function testNextUrl() {
if (currentIndex >= apiUrls.length) {
console.log("所有URL都失败了");
$done();
return;
}
const apiUrl = apiUrls[currentIndex];
$httpClient.get(apiUrl, (error, response, data) => {
if (error) {
console.log(`URL ${currentIndex + 1} 出错: ${error}`);
currentIndex++;
testNextUrl();
} else {
handleResponse(data);
}
});
}
function handleResponse(data) {
const oilPriceData = JSON.parse(data);
if (oilPriceData.code === 200) {
const oilPriceInfo = oilPriceData.result;
const message = `0#柴油:${oilPriceInfo.p0}元 | 92汽油:${oilPriceInfo.p92}元\n95汽油:${oilPriceInfo.p95}元 | 98汽油:${oilPriceInfo.p98}元`;
// 获取 http://m.qiyoujiage.com 网页 HTML 内容并提取 tishiContent
$httpClient.get('http://m.qiyoujiage.com/', (error, response, data) => {
if (error) {
console.log(`获取HTML内容出错: ${error}`);
} else {
// 使用正则表达式从HTML中提取 var tishiContent 的内容
const tishiMatch = data.match(/var\s+tishiContent\s*=\s*"(.*?)"/);
if (tishiMatch) {
let tishiContent = tishiMatch[1];
const dateMatch = tishiContent.match(/(\d{1,2})月(\d{1,2})日/);
let formattedDate = "未知日期";
let logDate = "未知日期";
if (dateMatch) {
let [month, day] = [parseInt(dateMatch[1]), parseInt(dateMatch[2])];
logDate = `${month}月${day}日`;
formattedDate = `${month.toString().padStart(2, '0')}-${(day + 1).toString().padStart(2, '0')}`;
}
let adjustmentSymbols = "";
const adjustmentMatch = tishiContent.match(/(下调|下跌|上调|上涨)/);
let adjustmentAction = adjustmentMatch[1];
adjustmentSymbols = (adjustmentAction.includes("下")) ? "\u25BC\u25B3" : "\u25BD\u25B2";
const priceRangeMatch = tishiContent.match(/(\d+\.\d+)元\/升-(\d+\.\d+)元\/升/);
let priceAdjustment = "0.00~0.00元";
if (priceRangeMatch) {
priceAdjustment = `${priceRangeMatch[1]}~${priceRangeMatch[2]}`;
}
tishiContent = tishiContent.replace(/<br\s*\/?>/g, '\n');
// 日志记录部分
const currentTime = new Date().toISOString().replace('T', ' ').split('.')[0];
console.log(`${currentTime} \n◼︎ 今日油价\n${message}`);
console.log(`${currentTime} \n◼︎ 油价预告\n${tishiContent}`);
console.log(`${currentTime} [Script Completed]`);
const body = {
title: `${params.title} | ${formattedDate} ${adjustmentSymbols} ${priceAdjustment}`,
content: `${message}`,
provname: params.provname,
icon: params.icon,
"icon-color": params.color
};
$done(body);
} else {
console.log("提取`tishiContent`失败");
currentIndex++;
testNextUrl();
}
}
});
} else {
console.log(`请求失败,错误信息:${oilPriceData.msg}`);
currentIndex++;
testNextUrl();
}
}
function getParams(param) {
return Object.fromEntries(
param
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
testNextUrl();