forked from fabius8/binanceAlert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinanceAlert.py
More file actions
77 lines (59 loc) · 2.6 KB
/
Copy pathbinanceAlert.py
File metadata and controls
77 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import time
from datetime import datetime
# API URL
api_url = "https://www.binance.com/bapi/composite/v1/public/cms/article/list/query?type=1&catalogId=48&pageNo=1&pageSize=20"
# 记录已经处理过的文章ID,避免重复提醒
processed_article_ids = set()
# 监控周期,单位:秒
monitor_interval = 5 # 每隔 60 秒查询一次
# 打印时加上时间戳
def log_with_time(message):
"""打印带时间戳的消息"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{current_time}] {message}")
# 初始化时获取现有的文章ID
def initialize_processed_articles():
"""初始化时获取当前所有文章ID,避免启动时提醒已有文章"""
try:
response = requests.get(api_url)
data = response.json()
if data["code"] == "000000" and data["data"]:
articles = data["data"]["catalogs"][0]["articles"]
for article in articles:
article_id = article["id"]
# 记录当前已有的所有文章ID,启动时不做提醒
processed_article_ids.add(article_id)
log_with_time("Initialization complete. Monitoring new articles...")
except Exception as e:
log_with_time(f"Error during initialization: {e}")
# 检查是否有包含 "Launchpool" 关键字的文章
def check_for_launchpool_articles():
"""检查API中是否有包含“Launchpool”的新文章"""
try:
response = requests.get(api_url)
data = response.json()
if data["code"] == "000000" and data["data"]:
articles = data["data"]["catalogs"][0]["articles"]
for article in articles:
article_id = article["id"]
title = article["title"]
# 如果文章ID之前没有处理过,并且标题中包含 "Launchpool"
if article_id not in processed_article_ids and "Launch" in title:
#if article_id not in processed_article_ids in title:
log_with_time(f"New Launchpool article found: {title}")
# 在此处可以扩展,例如发送邮件或其他通知
# 记录已处理过的文章ID
processed_article_ids.add(article_id)
except Exception as e:
log_with_time(f"Error fetching or processing data: {e}")
# 持续监控
def monitor():
"""启动监控程序"""
# 初始化,获取当前所有文章ID
initialize_processed_articles()
while True:
check_for_launchpool_articles()
time.sleep(monitor_interval)
if __name__ == "__main__":
monitor()