V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KelleyV9
V2EX  ›  V2EX

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

  •  1
     
  •   KelleyV9 · 21 天前 · 489 次点击

    引用 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

    KelleyV9
        1
    KelleyV9  
    OP
       21 天前
    后台运行命令:python -u test.py > /var/log/v2ex.log 2>&1 &
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   976 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:43 · PVG 03:43 · LAX 12:43 · JFK 15:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.