LOL赛事推送

查看 84|回复 9
作者:Yangzaipython   
[Python] 纯文本查看 复制代码import requests
import json
from datetime import datetime
import pytz
from collections import defaultdict
import os
from serverchan_sdk import sc_send
# 常量定义
TARGET_LEAGUES = {'LPL', 'LCK'}
CHINA_TZ = pytz.timezone("Asia/Shanghai")
GRAPHQL_URL = 'https://esports.op.gg/matches/graphql/__query__ListUpcomingMatchesBySerie'
SendKey = os.environ['SEND_KEY']
def utc_to_china(utc_str: str) -> datetime:
    """时间转换函数"""
    dt_utc = datetime.strptime(utc_str, "%Y-%m-%dT%H:%M:%S.000Z")
    dt_utc = dt_utc.replace(tzinfo=pytz.utc)
    return dt_utc.astimezone(CHINA_TZ)
def fetch_upcoming_matches():
    """请求比赛数据"""
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0',
    }
    payload = {
        'query': '''
        query {
            upcomingMatches {
                id
                name
                status
                scheduledAt
                tournament {
                    serie {
                        league {
                            shortName
                        }
                    }
                }
            }
        }
        '''
    }
    try:
        response = requests.post(GRAPHQL_URL, json=payload, headers=headers)
        response.raise_for_status()
        return response.json().get('data', {}).get('upcomingMatches', [])
    except requests.RequestException as e:
        print(f"[错误] 请求失败:{e}")
        return []
def filter_today_matches(matches):
    """处理并筛选比赛数据"""
    today = datetime.now(CHINA_TZ).date()
    result = defaultdict(list)
    for match in matches:
        try:
            league = match['tournament']['serie']['league']['shortName']
            if league not in TARGET_LEAGUES:
                continue
            match_time = utc_to_china(match['scheduledAt'])
            if match_time.date() == today:
                result[league].append((match['name'], match_time.strftime("%Y-%m-%d %H:%M:%S")))
        except (KeyError, TypeError):
            continue  # 安全跳过格式不完整的数据
    return result
def display_matches(match_data):
    """输出比赛信息"""
    content = ''
    for league, matches in match_data.items():
        # print(f"\n赛区:{league}")
        content += f"\n赛区:{league}"
        for name, time in matches:
            # print(f"比赛:{name}")
            # print(f"开始时间:{time}")
            content += f"\n比赛:{name}\n开始时间:{time}"
    return content
def generate_markdown(match_data):
    """生成 Markdown 格式内容"""
    date_str = datetime.now(CHINA_TZ).date()
    md = f"## 🏆 今日赛程({date_str})\n\n"
    region_flags = {
        "LCK": "🇰🇷",
        "LPL": "🇨🇳"
    }
    for region, games in match_data.items():
        flag = region_flags.get(region, "")
        md += f"### {flag} **{region} 赛区**\n"
        md += "| 时间(北京时间) | 对阵           |\n"
        md += "|------------------|----------------|\n"
        for name, b_time in games:
            time = datetime.strptime(b_time, "%Y-%m-%d %H:%M:%S").strftime("%H:%M")
            md += f"| {time}            | {name}      |\n"
        md += "\n---\n\n"
    md += "> ✅ 所有时间均为北京时间(UTC+8)\n"
    return md
def send_message(content):
    """发送消息"""
    try:
        # 发送请求
        response = sc_send(SendKey, "LOL赛事信息", content)
        # 检查响应状态码
        if response['code'] == 0:
            print("消息发送成功:", response)
        else:
            print("消息发送失败:", response.get('error'))
    except requests.exceptions.RequestException as e:
        print("请求异常:", e)
def main():
    """主程序"""
    matches = fetch_upcoming_matches()
    today_matches = filter_today_matches(matches)
    if today_matches:
        markdown_output = generate_markdown(today_matches)
        send_message(markdown_output)
if __name__ == "__main__":
    main()

时间, 赛区

Yangzaipython
OP
  

推送是用server酱,注册一个把sendkey填进去就可以了
真人不露脸   

哪个大佬能做进微信的消息推送里
qbz715412   


真人不露脸 发表于 2025-8-22 13:09
哪个大佬能做进微信的消息推送里

他这个就自带微信推送啊...
imxz   

可以改成推送酱的接口
baihat   

能拉取直播流就牛13了
ksda2684   

不懂就问 怎么用啊
ZZEDU   

太棒了,顶
AoSnow   

不错,看比赛摸鱼者狂喜
惺忪忪   

感谢分享
您需要登录后才可以回帖 登录 | 立即注册

返回顶部