1 Star 0 Fork 0

王佳琦/CSDN评论查询

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CSDNComment.py 3.56 KB
一键复制 编辑 原始数据 按行查看 历史
王佳琦 提交于 1年前 . v1
import json
import os
import requests
from datetime import datetime, timedelta
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
class CsdnCommentsManager:
HISTORY_FILE = 'history.json'
@staticmethod
def load_history():
if os.path.exists(CsdnCommentsManager.HISTORY_FILE):
with open(CsdnCommentsManager.HISTORY_FILE, 'r') as file:
return json.load(file)
return []
@staticmethod
def save_history(history):
with open(CsdnCommentsManager.HISTORY_FILE, 'w') as file:
json.dump(history, file)
@staticmethod
def update_history(history, username):
if username in history:
history.remove(username)
history.insert(0, username)
CsdnCommentsManager.save_history(history)
@staticmethod
def get_comments_count(username, date):
api_url = f"https://blog.csdn.net/community/home-api/v1/comment-list?page=1&size=220&type=out&noMore=false&username={username}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"
}
response = requests.get(api_url, headers=headers)
if response.status_code != 200:
print(f"请求出错: {response.status_code}")
return -1
try:
data = response.json()
comments = data['data']['list']
target_date_comments = [comment for comment in comments if comment['createTime'].split()[0] == date]
return len(target_date_comments)
except Exception as e:
print(f"发生错误: {e}")
return -1
def fetch_comments(combobox):
username = combobox.get()
today_date = datetime.now().strftime('%Y-%m-%d')
today_count = CsdnCommentsManager.get_comments_count(username, today_date)
yesterday_date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
yesterday_count = CsdnCommentsManager.get_comments_count(username, yesterday_date)
if today_count == -1 or yesterday_count == -1:
messagebox.showerror("Error", "请求出错或解析出错")
else:
output_text.set(f"{username} 今天的评论数量为: {today_count}\n"
f"{username} 昨天的评论数量为: {yesterday_count}\n"
f"{username} 最近2天的评论总数量为: {yesterday_count + today_count}")
history = CsdnCommentsManager.load_history()
CsdnCommentsManager.update_history(history, username)
combobox['values'] = history
def center_window(root, width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size)
# 创建主窗口
root = tk.Tk()
root.title("CSDN 评论计数器")
root.geometry("400x400")
center_window(root, 400, 400) # 调整窗口大小以适应你的界面布局
# 创建提示标签
label = tk.Label(root, text="在此输入账号:")
label.pack(pady=10)
# 创建下拉菜单
history = CsdnCommentsManager.load_history()
combobox = ttk.Combobox(root, values=history)
if len(history) != 0:
combobox.current(0)
combobox.pack(pady=10)
# 创建按钮
button = tk.Button(root, text="查询评论数量", command=lambda: fetch_comments(combobox))
button.pack(pady=10)
# 创建输出区域
output_text = tk.StringVar()
output_label = tk.Label(root, textvariable=output_text, justify="left")
output_label.pack(pady=10)
# 运行主循环
root.mainloop()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ljt0023/csdn-comment-query.git
git@gitee.com:ljt0023/csdn-comment-query.git
ljt0023
csdn-comment-query
CSDN评论查询
master

搜索帮助