2024-12-06 16:32:34 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2024-12-04 00:08:44 +08:00
|
|
|
|
import json
|
|
|
|
|
import sqlite3
|
|
|
|
|
import os
|
|
|
|
|
from datetime import datetime, timedelta
|
2024-12-09 23:52:49 +08:00
|
|
|
|
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
|
|
|
|
def create_database():
|
2025-01-02 17:11:11 +08:00
|
|
|
|
conn = sqlite3.connect('./resources/db/anquanke.db')
|
2024-12-04 00:08:44 +08:00
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
title TEXT,
|
|
|
|
|
guid TEXT,
|
|
|
|
|
source TEXT,
|
|
|
|
|
description TEXT,
|
|
|
|
|
pubDate DATETIME,
|
|
|
|
|
author TEXT
|
|
|
|
|
)''')
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
def insert_data(data):
|
2025-01-02 17:11:11 +08:00
|
|
|
|
conn = sqlite3.connect('./resources/db/anquanke.db')
|
2024-12-04 00:08:44 +08:00
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
for entry in data:
|
|
|
|
|
cursor.execute('''
|
|
|
|
|
INSERT INTO articles (title, guid, source, description, pubDate, author)
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
|
|
|
''', (entry['title'], entry['guid'], entry['source'], entry['description'], entry['pubDate'], entry['author']))
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
def get_anquanke_json():
|
|
|
|
|
# 检查文件是否存在
|
2025-01-02 17:11:11 +08:00
|
|
|
|
if not os.path.exists('./resources/JSON/anquanke.json'):
|
2025-01-24 17:36:53 +08:00
|
|
|
|
data = []
|
|
|
|
|
# logger.error(f"anquanke.json文件不存在,请检查爬虫程序是否运行正常!")
|
|
|
|
|
print(f"anquanke.json文件不存在,请检查爬虫程序是否运行正常!")
|
|
|
|
|
else:
|
|
|
|
|
# 打开并读取JSON文件
|
|
|
|
|
with open('./resources/JSON/anquanke.json', 'r', encoding='utf-8') as file:
|
|
|
|
|
content = file.read()
|
|
|
|
|
if not content:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
data = json.load(file)
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
|
|
|
|
# 假设data是一个包含多个JSON对象的列表
|
|
|
|
|
if not isinstance(data, list):
|
|
|
|
|
raise ValueError("JSON文件格式错误,请检查common.py是否异常!")
|
|
|
|
|
|
|
|
|
|
# 提取所需字段并编号
|
|
|
|
|
total_data = []
|
|
|
|
|
for index, item in enumerate(data, start=1):
|
|
|
|
|
entry = {
|
|
|
|
|
"id": index,
|
|
|
|
|
"title": item.get("title", ""),
|
|
|
|
|
"guid": item.get("guid", ""),
|
|
|
|
|
"description": item.get("description", ""),
|
|
|
|
|
"pubDate": item.get("pubDate", ""),
|
|
|
|
|
"author": item.get("author", ""),
|
|
|
|
|
"source": item.get("source", "")
|
|
|
|
|
}
|
|
|
|
|
total_data.append(entry)
|
|
|
|
|
|
|
|
|
|
return total_data
|
|
|
|
|
|
2024-12-04 17:21:26 +08:00
|
|
|
|
def select_articles(e_hour):
|
2025-01-02 17:11:11 +08:00
|
|
|
|
conn = sqlite3.connect('./resources/db/anquanke.db')
|
2024-12-04 00:08:44 +08:00
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
# 获取当前日期和时间
|
|
|
|
|
now = datetime.now()
|
2024-12-04 17:21:26 +08:00
|
|
|
|
start_time = now - timedelta(hours=e_hour)
|
|
|
|
|
end_time = now
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
|
|
|
|
# 查询指定时间段内的数据
|
|
|
|
|
cursor.execute('''
|
|
|
|
|
SELECT * FROM articles
|
|
|
|
|
WHERE pubDate BETWEEN ? AND ?
|
|
|
|
|
''', (start_time.strftime('%Y-%m-%d %H:%M:%S'), end_time.strftime('%Y-%m-%d %H:%M:%S')))
|
|
|
|
|
|
|
|
|
|
results = cursor.fetchall()
|
|
|
|
|
conn.close()
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def clear_table():
|
2025-01-02 17:11:11 +08:00
|
|
|
|
conn = sqlite3.connect('./resources/db/anquanke.db')
|
2024-12-04 00:08:44 +08:00
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute('DELETE FROM articles')
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
2025-01-02 17:11:11 +08:00
|
|
|
|
def record_md(result, filename="./resources/history/sec_news.md"):
|
2024-12-11 23:15:59 +08:00
|
|
|
|
# 读取现有内容
|
|
|
|
|
if os.path.exists(filename):
|
|
|
|
|
with open(filename, 'r', encoding='utf-8') as file:
|
|
|
|
|
existing_content = file.read()
|
|
|
|
|
else:
|
|
|
|
|
existing_content = ""
|
|
|
|
|
|
|
|
|
|
# 将新内容插入到现有内容的开头
|
|
|
|
|
new_content = result + existing_content
|
|
|
|
|
|
|
|
|
|
# 写回文件
|
|
|
|
|
with open(filename, 'w', encoding='utf-8') as file:
|
|
|
|
|
file.write(new_content)
|
2024-12-10 11:49:15 +08:00
|
|
|
|
|
2025-01-22 14:03:36 +08:00
|
|
|
|
def get_filtered_articles(entries):
|
|
|
|
|
result_long = ""
|
|
|
|
|
result_short = ""
|
2024-12-10 11:49:15 +08:00
|
|
|
|
record = ""
|
2025-01-22 14:03:36 +08:00
|
|
|
|
short_results = []
|
|
|
|
|
|
2024-12-04 00:08:44 +08:00
|
|
|
|
for entry in entries:
|
2025-01-22 14:03:36 +08:00
|
|
|
|
# 构建长文本结果
|
|
|
|
|
result_long += f"文章:[{entry[1]}]({entry[2]})\n作者:{entry[6]}\n来源:{entry[3]}\n"
|
|
|
|
|
result_long += f"上传时间:{entry[5]}\n"
|
|
|
|
|
result_long += "\n" + "-" * 40 + "\n" # 添加分隔线以便区分不同文章
|
|
|
|
|
|
|
|
|
|
# 构建短文本结果并进行分块处理
|
|
|
|
|
current_entry = (
|
2025-01-22 15:11:15 +08:00
|
|
|
|
f"文章:[{entry[1]}]({entry[2]})\n"
|
|
|
|
|
f"上传时间:{entry[5]}\n"
|
2025-01-22 14:03:36 +08:00
|
|
|
|
"\n" + "-" * 3 + "\n" # 添加分隔线以便区分不同文章
|
|
|
|
|
)
|
|
|
|
|
temp_result = result_short + current_entry
|
|
|
|
|
if len(temp_result.encode('utf-8')) > 4096:
|
|
|
|
|
short_results.append(result_short)
|
|
|
|
|
result_short = current_entry
|
|
|
|
|
else:
|
|
|
|
|
result_short = temp_result
|
2024-12-10 11:49:15 +08:00
|
|
|
|
|
2024-12-25 18:40:20 +08:00
|
|
|
|
record += f"#### 文章:[{entry[1]}]({entry[2]})\n"
|
2024-12-10 11:49:15 +08:00
|
|
|
|
record += f"**作者**:{entry[6]}\n"
|
|
|
|
|
record += f"**来源**:{entry[3]}\n"
|
|
|
|
|
record += f"**上传时间**:{entry[5]}\n"
|
|
|
|
|
record += "\n" + "-" * 40 + "\n" # 添加分隔线以便区分不同文章
|
2025-01-22 14:03:36 +08:00
|
|
|
|
|
|
|
|
|
# 处理最后一个结果
|
|
|
|
|
if result_short:
|
|
|
|
|
short_results.append(result_short)
|
|
|
|
|
|
2024-12-10 11:49:15 +08:00
|
|
|
|
record_md(record)
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
2025-01-22 14:03:36 +08:00
|
|
|
|
return result_long, short_results
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
2025-01-22 14:03:36 +08:00
|
|
|
|
def Src_anquanke(e_hour):
|
2025-01-02 17:11:11 +08:00
|
|
|
|
if not os.path.exists('./resources/db/anquanke.db'):
|
2024-12-04 00:08:44 +08:00
|
|
|
|
# 创建数据库和表
|
|
|
|
|
create_database()
|
|
|
|
|
|
|
|
|
|
# 清空表
|
|
|
|
|
clear_table()
|
|
|
|
|
|
|
|
|
|
# 获取 JSON 数据
|
|
|
|
|
M_anquanke_data = get_anquanke_json()
|
|
|
|
|
|
|
|
|
|
# 插入数据到数据库
|
|
|
|
|
insert_data(M_anquanke_data)
|
|
|
|
|
|
|
|
|
|
# 查询指定时间段内的数据
|
2024-12-04 17:21:26 +08:00
|
|
|
|
filtered_articles = select_articles(e_hour)
|
2024-12-04 00:08:44 +08:00
|
|
|
|
# print(filtered_articles)
|
|
|
|
|
|
|
|
|
|
if filtered_articles:
|
2025-01-22 14:03:36 +08:00
|
|
|
|
result_long, short_results = get_filtered_articles(filtered_articles)
|
|
|
|
|
return result_long, short_results
|
2024-12-04 00:08:44 +08:00
|
|
|
|
else:
|
2024-12-05 12:20:37 +08:00
|
|
|
|
return False
|
2024-12-04 00:08:44 +08:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-01-22 14:03:36 +08:00
|
|
|
|
results = Src_anquanke(4)
|
|
|
|
|
if results != False:
|
|
|
|
|
result_long, short_results = results
|
|
|
|
|
|
|
|
|
|
# 打印长文本结果
|
|
|
|
|
print("长文本结果:")
|
|
|
|
|
print(result_long)
|
|
|
|
|
print("\n" + "-" * 40 + "\n")
|
|
|
|
|
|
|
|
|
|
# 打印分块的短文本结果
|
|
|
|
|
print("分块的短文本结果:")
|
|
|
|
|
for short_result in short_results:
|
|
|
|
|
print(short_result)
|
|
|
|
|
print("\n" + "-" * 40 + "\n")
|
2024-12-05 12:20:37 +08:00
|
|
|
|
else:
|
|
|
|
|
# 如果为空,则跳过执行
|
|
|
|
|
print("-" * 40)
|
2025-01-22 14:03:36 +08:00
|
|
|
|
print("安全客数据为空,跳过执行。")
|