修正ssl连接功能;修改默认参数。

This commit is contained in:
LiuEnder
2026-03-10 12:20:36 +08:00
parent ad8bbc715e
commit 245625ae7d
3 changed files with 35 additions and 27 deletions

View File

@@ -1,31 +1,20 @@
//我需要一个定时获取浏览器中黄金价格并推送到微信的程序
//网站网址https://mybank.icbc.com.cn/icbc/newperbank/perbank3/gold/goldaccrual_query_out.jsp
//黄金价格在页面中,需要获取到黄金价格并推送到微信
//黄金价格的selector为#activeprice_080020000521
//黄金价格的单位为:元/克
//黄金价格的更新时间为每半小时检测一次变化量小于1元/克则不推送。
//根据你的判断使用合适的算法计算价格趋势。
//根据你的判断使用外部json储存或其他方式储存数据。
//一个数组记录三十天的收盘价格用于计算七日价格变化量七天价格趋势十四天价格趋势二十八天价格趋势趋势类型1.上涨 2.下跌 3.持平。
//推送格式为当前黄金价格为xx元/克一小时变化量为xx元/克七日价格变化量为xx元/克七日价格趋势为xx十四日价格趋势为xx二十八日价格趋势为xx。
//该脚本需要使用NodeJS编写并使用Puppeteer库来获取页面内容。同时留出一个数组用作特殊价格时的设置当黄金价格达到该价格时推送提醒。数组格式为[价格1,价格2,价格3,价格4,价格5,价格6,价格7]
//特殊提醒格式为特殊提醒黄金价格达到xx元/克七日价格趋势xx。
//需要使用ServerChan来推送信息ServerChan的sendkey使用环境变量变量名为PUSH_KEYT
//程序每天由crontab定时启动启动时间由外部的青龙面板设置设置为每天晚上2230后关闭。
// 此下为信息推送用程序例子
// import {scSend} from 'serverchan-sdk';
// const response = await scSend('sendkey', 'title', 'desp', { tags: '黄金价格' });
// console.log('Response:', response);
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const cheerio = require('cheerio');
const https = require('https');
const crypto = require('crypto');
const TARGET_URL = 'https://mybank.icbc.com.cn/icbc/newperbank/perbank3/gold/goldaccrual_query_out.jsp';
const PRICE_SELECTOR = '#activeprice_080020000521';
const DATA_FILE = path.join(__dirname, 'gold_data.json');
// 自定义 HTTPS Agent允许不安全证书并启用旧版 SSL 重协商兼容
const insecureHttpsAgent = new https.Agent({
rejectUnauthorized: false,
secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT || 0,
});
// ServerChan Key 配置:优先使用直接设置,否则使用环境变量
// 支持多个key使用分号;)分割
const SERVER_CHAN_KEY_DIRECT = ''; // 直接设置 ServerChan Key有值时禁用环境变量多个key用分号分割
@@ -39,9 +28,9 @@ const SPECIAL_PRICE_TARGETS = [];
const CONFIG = {
changeThresholdYuan: 1.0,
changeWindowMinutes: 60,
sampleIntervalMinutes: 2,
sampleIntervalMinutes: 1,
pricePrecisionDigits: 2,
enableTimedPush: true, // 定时推送开关(变化量触发和收盘推送)
enableTimedPush: false, // 定时推送开关(变化量触发和收盘推送)
enableSpecialAlert: true, // 特殊价格提醒开关
pushIntervalMinutes: 60, // 推送间隔(分钟),防止频繁推送
forcePushTest: false, // 测试强制推送:为 true 时每次运行必推送一次并立即退出
@@ -138,6 +127,7 @@ async function fetchCurrentPrice() {
console.log('[gold] fetching html to parse price...');
const resp = await axios.get(TARGET_URL, {
timeout: 60000,
httpsAgent: insecureHttpsAgent,
headers: {
// 部分站点会根据 UA 返回不同内容,给一个常见 UA 更稳
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',

21
package-lock.json generated
View File

@@ -9,8 +9,9 @@
"version": "1.0.0",
"dependencies": {
"axios": "^1.13.6",
"cheerio": "^1.1.2",
"serverchan-sdk": "^1.0.0"
"cheerio": "^1.2.0",
"npm-check-updates": "^19.6.3",
"serverchan-sdk": "^1.0.6"
}
},
"node_modules/asynckit": {
@@ -22,6 +23,7 @@
"version": "1.13.6",
"resolved": "https://r.cnpmjs.org/axios/-/axios-1.13.6.tgz",
"integrity": "sha512-ChTCHMouEe2kn713WHbQGcuYrr6fXTBiu460OTwWrWob16g1bXn4vtz07Ope7ewMozJAnEquLk5lWQWtBig9DQ==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.11",
"form-data": "^4.0.5",
@@ -49,6 +51,7 @@
"version": "1.2.0",
"resolved": "https://r.cnpmjs.org/cheerio/-/cheerio-1.2.0.tgz",
"integrity": "sha512-WDrybc/gKFpTYQutKIK6UvfcuxijIZfMfXaYm8NMsPQxSYvf+13fXUJ4rztGGbJcBQ/GF55gvrZ0Bc0bj/mqvg==",
"license": "MIT",
"dependencies": {
"cheerio-select": "^2.1.0",
"dom-serializer": "^2.0.0",
@@ -476,6 +479,20 @@
}
}
},
"node_modules/npm-check-updates": {
"version": "19.6.3",
"resolved": "https://r.cnpmjs.org/npm-check-updates/-/npm-check-updates-19.6.3.tgz",
"integrity": "sha512-VAt9Bp26eLaymZ0nZyh5n/by+YZIuegXlvWR0yv1zBqd984f8VnEnBbn+1lS3nN5LyEjn62BJ+yYgzNSpb6Gzg==",
"license": "Apache-2.0",
"bin": {
"ncu": "build/cli.js",
"npm-check-updates": "build/cli.js"
},
"engines": {
"node": ">=20.0.0",
"npm": ">=8.12.1"
}
},
"node_modules/nth-check": {
"version": "2.1.1",
"resolved": "https://r.cnpmjs.org/nth-check/-/nth-check-2.1.1.tgz",

View File

@@ -2,13 +2,14 @@
"name": "gold-price-monitor",
"version": "1.0.0",
"description": "A script to monitor ICBC gold price and push notifications via ServerChan.",
"main": "GoldPriceV6cooldown.js",
"main": "GoldPrice.js",
"scripts": {
"start": "node GoldPriceV6cooldown.js"
"start": "node GoldPrice.js"
},
"dependencies": {
"axios": "^1.13.6",
"cheerio": "^1.1.2",
"serverchan-sdk": "^1.0.0"
"cheerio": "^1.2.0",
"npm-check-updates": "^19.6.3",
"serverchan-sdk": "^1.0.6"
}
}