Normal view

There are new articles available, click to refresh the page.
Yesterday — 13 August 2025Main stream

Python 监控$v2ex 价格并微信通知

By: KelleyV9
13 August 2025 at 14:42
KelleyV9:

引用 vip5000521 这个老哥发的,优化了一版本可以进行微信通知,将下面代码保存成如 test.py 然后执行 python test.py 以下就好了。

# -*- coding: utf-8 -*-
import requests
import time
import json

# 配置部分
URL = "https://api.phantom.app/price/v1/solana:101/address/9raUVuzeWUk53co63M4WXLWPWE4Xc6Lpn7RS9dnkpump"
TARGET_PRICE = 0.013  # 监控价格
CHECK_INTERVAL = 30  # 检查间隔(秒)

# 微信推送配置(使用 Server 酱服务)
WECHAT_PUSH_ENABLED = True  # 设为 False 禁用微信推送
SERVERCHAN_KEY = "你的 Server 酱 SCKEY"  # 替换为你的实际 key

def send_wechat_notification(title, message):
    """发送微信通知"""
    if not WECHAT_PUSH_ENABLED:
        return
        
    url = f"https://sctapi.ftqq.com/{SERVERCHAN_KEY}.send"
    data = {
        "title": title,
        "desp": message
    }
    try:
        response = requests.post(url, data=data)
        response.raise_for_status()
        print("微信通知发送成功")
    except requests.RequestException as e:
        print(f"微信通知发送失败: {e}")

def check_price():
    try:
        response = requests.get(URL)
        response.raise_for_status()
        data = response.json()
        
        current_price = data.get("price")
        if current_price is None:
            print("返回数据中未找到 price 字段")
            return False
            
        print(json.dumps(data, indent=2, ensure_ascii=False))
        
        if current_price <= TARGET_PRICE:
            msg = f"请注意: 当前价格 {current_price} 低于或等于目标价格 {TARGET_PRICE}"
            print(msg)
            send_wechat_notification("价格监控提醒", msg)
            
        return True
        
    except requests.RequestException as e:
        print(f"请求出错: {e}")
        return False
    except ValueError:
        print("返回内容不是合法的 JSON 格式")
        return False
    except Exception as e:
        print(f"发生未知错误: {e}")
        return False

def main():
    print("价格监控程序启动...")
    print(f"监控目标价格: {TARGET_PRICE}")
    print(f"检查间隔: {CHECK_INTERVAL}秒")
    
    while True:
        check_price()
        time.sleep(CHECK_INTERVAL)

if __name__ == "__main__":
    main()

使用说明

  1. 你需要注册 Server 酱服务( https://sct.ftqq.com/)获取 SCKEY

  2. 将代码中的"你的 Server 酱 SCKEY"替换为你自己的 key

  3. 如果需要禁用微信推送,将 WECHAT_PUSH_ENABLED 设为 False

❌
❌