惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
V
Visual Studio Blog
小众软件
小众软件
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
T
Tor Project blog
Jina AI
Jina AI
GbyAI
GbyAI
C
Comments on: Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
A
Arctic Wolf
有赞技术团队
有赞技术团队
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
Webroot Blog
Webroot Blog
C
Cisco Blogs
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
K
Kaspersky official blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
D
Docker
P
Palo Alto Networks Blog
The Register - Security
The Register - Security
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志

博客园 - 隐客

商品期权的保证金计算 写个chrome插件屏蔽某些视频,防止孩子看些不正常的视频 获取个股信息的东财数据 AI量化qlib学习笔记四:测试模型 AI量化qlib学习笔记三:训练模型 AI量化qlib学习笔记二:转换数据 AI量化qlib学习笔记 一:清洗数据 python 打包工具 随手写了街机一键发招的代码 使用pybind11封装c++的dll,供python调用 用py-spy对python线程查看cpu等资源 占用和消耗 通过selenium获取性能日志中的response的body Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件 vscode 扩展商店打不开的解决办法 通过1分钟生成其它线的bar配置文件 用numpy读取结构化二进制文件 微信pc防撤回修改笔记 关于在python中时间的转换 把本地vscode项目代码传到gitee上
python: 与通达信联动
隐客 · 2025-05-31 · via 博客园 - 隐客
import win32gui
import win32con
import win32api

import re
import win32clipboard
import time

MY_SYMBOLS = [
    "SHFE.rb", "SHFE.ru", "SHFE.fu", "SHFE.ag", "SHFE.cu", "SHFE.ss",
    "SHFE.ao", "SHFE.zn", "SHFE.ni", "SHFE.sn", "SHFE.al", "SHFE.pb",
    "SHFE.hc", "SHFE.sp", "INE.nr", "INE.sc", "DCE.y", "DCE.m", "DCE.p",
    "DCE.lh", "DCE.v", "DCE.pp", "DCE.eg", "DCE.eb", "DCE.pg", "DCE.jd",
    "DCE.jm", "DCE.c", "DCE.l", "CZCE.SA", "CZCE.OI", "CZCE.UR", "CZCE.CF",
    "CZCE.MA", "CZCE.TA", "CZCE.RM", "CZCE.FG", "CZCE.SR", "CZCE.CJ",
    "CZCE.PK", "CZCE.SM", "CZCE.SH", "GFEX.lc", "GFEX.si", "GFEX.ps"
]
def ensure_window_foreground(hwnd, max_retries=5, interval=0.2):
    """确保窗口成功前置的循环检测函数"""
    for attempt in range(max_retries):
        try:
            # 先最小化再恢复窗口(提高前置成功率)
            win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
            
            # 尝试设置前置窗口
            win32gui.SetForegroundWindow(hwnd)
            
            # 验证是否成功前置
            if win32gui.GetForegroundWindow() == hwnd:
                return True
                
        except Exception as e:
            print(f"窗口前置尝试 {attempt+1} 失败: {e}")
        
        time.sleep(interval)
    
    return False
def send_keys_to_window(class_name, keys):
    # 查找窗口句柄
    hwnd = win32gui.FindWindow(class_name, None)
    if not hwnd:
        print(f"未找到类名为 {class_name} 的窗口")
        return False
    if not ensure_window_foreground(hwnd):
        print("窗口前置失败")
        return False
    
    print("send " ,keys)
    time.sleep(0.3)
 
    
    # 发送键盘消息
    for char in keys:
        if char == '\n':  # 回车键
            win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0)
            win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0)
        else:
            # 发送普通字符
            win32api.keybd_event(ord(char.upper()), 0, 0, 0)
            win32api.keybd_event(ord(char.upper()), 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(0.05)  # 按键间隔
    
    return True

def format_contract_code(contract):
    """处理CZCE合约的特殊格式"""
    exchange, code = contract.split('.')
    if exchange == 'CZCE':
        # 匹配商品代码和数字部分
        match = re.match(r'([A-Za-z]+)(\d+)', code)
        if match:
            product = match.group(1)
            numbers = match.group(2)
            # 如果是3位数字,前面补2
            if len(numbers) == 3:
                numbers = '2' + numbers
            return f"{product}{numbers}"
    return code

def extract_first_bracket(text):
    match = re.search(r'\[([^]]+)\]', text)
    return match.group(1) if match else None

def is_valid_contract(code):
    # 提取基础合约代码(去除年月数字部分)
    base_code = re.sub(r'(\d{4}|\d{3})$', '', code)
    return base_code in MY_SYMBOLS

def monitor_clipboard(interval=1):
    last_content = ""
    try:
        while True:
            try:
                win32clipboard.OpenClipboard()
                if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
                    data = win32clipboard.GetClipboardData()
                    if isinstance(data, str) and data != last_content:
                        symbol = extract_first_bracket(data)
                        if symbol and is_valid_contract(symbol):
                            print(f"有效合约代码: {symbol}")      
                            formatted_code = format_contract_code(symbol)
                            send_keys_to_window("TdxW_MainFrame_Class", formatted_code + "\n")
                        last_content = data
            finally:
                win32clipboard.CloseClipboard()
            time.sleep(interval)
    except KeyboardInterrupt:
        print("监控已停止")

if __name__ == "__main__":
    print("合约代码监控启动 (Ctrl+C退出)")
    monitor_clipboard()