-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor.js
12 lines (11 loc) · 3.35 KB
/
monitor.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 任务名称
* name: VPS 库存监控
* 环境变量
* WHMCS_URLS 监控商品链接(包含 pid 的链接) 多个连接用英文分号(;)分割
* WHMCS_INTERVAL 监控频率,单位毫秒,默认为一分钟一次(60000)
* WHMCS_LOGS 是否打印详细日志
* 定时规则
* cron: 0 0 1 1 *
*/
const{URL:t}=require("url"),e=require("axios"),o=require("tough-cookie"),{wrapper:n}=require("axios-cookiejar-support"),s=require("cheerio"),r="0.1.0",{sendNotify:i}=require("./sendNotify.js"),a=process.env.WHMCS_URLS||"",c=process.env.WHMCS_INTERVAL||6e4,l=process.env.WHMCS_LOGS||!0,u=a.split(";"),p={},d=new o.CookieJar,g=n(e.create({jar:d})),m=e.create({baseURL:"https://vps.tsx.us.kg"});async function h(e,o){try{const n=new t(e);0===(await d.getCookies(`${n.protocol}//${n.host}`)).length&&await g.get(e);const{data:r}=await g.get(e),a=s.load(r),c=a("body").text().trim();c.includes("缺貨中")||c.includes("Out of Stock")?(l&&console.log(`${S()} 监控 ${o} 无货`),p[e]=!0):(l&&console.log(`${S()} 监控 ${o} 有货`),p[e]&&(e.includes("bwh")||e.includes("bandwagon")?(i(...await x(a,e)),console.log((await x(a,e))[1])):e.includes("dmit")?(i(...await y(a,e)),console.log((await y(a,e))[1])):(i(...await w(a,e)),console.log((await w(a,e))[1])),p[e]=!1))}catch(t){console.error("检查库存时出错:",t)}}async function w(t,e){const o=t("title").text().split("-")[1].trim(),n=t(".product-info .product-title").text(),s=t(".product-info p").eq(1).text(),r=[];t("select[name=billingcycle] option").each(((e,o)=>{r.push(t(o).text().trim())}));const i=r.join("\n");return[o+"补货通知",await $(n,e,i,s)]}async function x(t,e){const o=t("title").text().split("-")[0].trim(),n=t(".cartbox strong").text().trim(),s=t(".cartbox strong").parent(".cartbox").html().split("<br>").map((t=>t.trim())).filter((t=>""!==t)),r=s.findIndex((t=>t.startsWith("SSD:"))),i=s.slice(r,r+5).join("\n"),a=[];t("select[name=billingcycle] option").each(((e,o)=>{a.push(t(o).text().trim())}));const c=a.join("\n");return[o+" 补货通知",await $(n,e,c,i)]}async function y(t,e){const o=t("title").text().split("-")[1].trim(),n=t(".order-summary-box .product-title").text().trim(),s=[];t(".order-summary-box .order-summary-desc-item").each(((e,o)=>{s.push(t(o).text().trim())}));const r=s.join("\n"),i=t(".summary-totals").text();return[o+"补货通知",await $(n,e,i,r)]}async function $(t,e,o,n){const s=new URLSearchParams(e).get("pid");let r=e;try{const{data:t}=await m.post("/url",{url:e});t.url&&(r=`${t.url}&pid=${s}`)}catch{}return`\n🎁 名称\n${t}\n\n🔗 下单链接\n${r}\n\n💰 价格\n${o}\n\n📝 商品详情\n${n}\n`}function S(){const t=new Date;return`[${String(t.getHours()).padStart(2,"0")}:${String(t.getMinutes()).padStart(2,"0")}:${String(t.getSeconds()).padStart(2,"0")}]`}console.log("脚本最新动态: https://t.me/whmcs_helper\n"),m.post("/stats",{urls:a,version:r}).then((t=>{console.log("当前版本: "+r),t.data.latest?(console.log("本脚本已执行"+t.data.count+"次\n"),console.log("-------开始执行-------"),u.forEach((async(t,e)=>{p[t]=!0,t.includes("pid")?(console.log(`[获取到 ${u.length} 个地址] 开始监控第 ${e+1} 个地址`),h(t,e+1),setInterval((()=>h(t,e+1)),c)):console.log("请输入带有 pid 的 URL 地址")}))):(console.log("最新版本: "+t.data.version),console.log("脚本已更新,请重新拉取脚本!"))})).catch((t=>{console.log(t)}));