-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjin10tab.py
117 lines (99 loc) · 4.15 KB
/
jin10tab.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import tkinter as tk
from tkinter import ttk
import requests
from fake_useragent import UserAgent
import json
from datetime import datetime
'''
测试金十数据app
'''
class Jin10App:
def __init__(self, notebook_frame):
# notebook_frame 是 Notebook 中的一个选项卡
self.notebook_frame = notebook_frame
self.root = notebook_frame.master # 获取主窗口(Tk 实例)
# 创建框架用于包含文本框和滚动条
frame = tk.Frame(self.notebook_frame)
frame.pack(fill="both", expand=True)
# 添加滚动条
scrollbar = ttk.Scrollbar(frame)
scrollbar.pack(side="right", fill="y")
# 创建文本框用于展示新闻内容
self.text_area = tk.Text(frame, wrap="word", yscrollcommand=scrollbar.set, height=25, width=75)
self.text_area.pack(pady=10, padx=10, fill="both", expand=True)
# 将滚动条与文本框连接
scrollbar.config(command=self.text_area.yview)
# 配置标签颜色
self.text_area.tag_configure("new", foreground="red") # 新文本红色
self.text_area.tag_configure("normal", foreground="black") # 旧文本黑色
# 用于存储已经展示过的新闻
self.displayed_news_ids = set()
# 获取并展示一次初始数据
self.update_news()
def get_jin10_data2(self):
url = "https://flash-api.jin10.com/get_flash_list"
headers = {
"x-app-id": "SO1EJGmNgCtmpcPF",
"x-version": "1.0.0",
"referer": "https://www.jin10.com/",
}
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
queryParam = {
"max_time": current_time,
"channel": "-8200",
}
headers["User-Agent"] = UserAgent().random
resp = requests.get(url, headers=headers, params=queryParam)
if resp.status_code != 200: # 确保请求成功
print(f"Error: {resp.status_code}")
data = resp.json()['data']
content_list = []
# print(data)
for item in data:
if not item["data"].get("content",None):
continue
news_id = item["id"]
# print(item)
content = {
"内容": item["data"]["content"],
"时间": item["time"],
"id": news_id # 记录新闻的唯一 ID
}
content_list.append(content)
# 按时间倒序排列(数据已经是从最新到最旧,插入时保持这个顺序)
return content_list
def update_news(self):
# 获取新闻数据
news_data = self.get_jin10_data2()
print(f"\n资讯直播: {news_data[0]}\n")
# 逆序处理,以确保最旧的新闻在底部
for item in reversed(news_data):
news_id = item["id"]
if news_id not in self.displayed_news_ids:
self.displayed_news_ids.add(news_id)
news_text = f"【{item['时间']}】 - {item['内容']}\n\n"
# 插入新内容到顶部
self.text_area.insert("1.0", news_text)
# 给新插入的内容加上"new"标签
self.text_area.tag_add("new", "1.0", f"1.{len(news_text)}")
# 设置光标到最顶端
self.text_area.see("1.0")
# 设置已展示的新闻为正常颜色
self.text_area.tag_configure("normal", foreground="black")
# 确保之前的内容仍然是黑色
self.text_area.tag_add("normal", "2.0", tk.END)
# 继续定时更新
self.root.after(5000, self.update_news) # 每5秒更新一次
if __name__ == "__main__":
root = tk.Tk()
root.title("Jin10 新闻展示")
root.geometry("600x400")
# 创建 Notebook 组件
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
# 创建一个选项卡并将 Jin10App 的内容添加到选项卡中
news_frame = tk.Frame(notebook)
notebook.add(news_frame, text="最新新闻")
# 初始化 Jin10App 并将其绑定到 Notebook 的选项卡中
app = Jin10App(news_frame)
root.mainloop()