adjust file structure

This commit is contained in:
MasonLiu committed 2024-12-09 23:12:32 +08:00
1 parent 5797ddae06
commit 1e4e423e68
8 files changed
+7 -8

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/4hou.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
description TEXT,
pubDate DATETIME,
author TEXT
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/4hou.db')
cursor = conn.cursor()
for entry in data:
try:
# 解析 pubDate 字符串为 datetime 对象
pub_date = datetime.strptime(entry['pubDate'], '%a, %d %b %Y %H:%M:%S %z')
# 格式化 pubDate 为所需的格式
formatted_pub_date = pub_date.strftime('%Y-%m-%d %H:%M:%S')
except ValueError:
# 如果解析失败,使用原始 pubDate 字符串
formatted_pub_date = entry['pubDate']
cursor.execute('''
INSERT INTO articles (title, link, description, pubDate, author)
VALUES (?, ?, ?, ?, ?)
''', (entry['title'], entry['link'], entry['description'], formatted_pub_date, entry['author']))
conn.commit()
conn.close()
def get_4hou_json():
# 检查文件是否存在
if not os.path.exists('./JSON/4hou.json'):
raise FileNotFoundError(f"4hou.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/4hou.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"link": item.get("link", ""),
"description": item.get("description", ""),
"pubDate": item.get("pubDate", ""),
"author": item.get("author", "")
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/4hou.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/4hou.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"作者:{entry[5]}\n文章:{entry[1]}\n"
result += f"链接:{entry[2]}\n上传时间:{entry[4]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_4hou(e_hour):
if not os.path.exists('./db/4hou.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_4hou_data = get_4hou_json()
# 插入数据到数据库
insert_data(M_4hou_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return ""
if __name__ == "__main__":
reslts = Src_4hou(4)
if reslts != "":
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("嘶吼数据为空,跳过执行。")
+130
View File
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/anquanke.db')
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):
conn = sqlite3.connect('./db/anquanke.db')
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():
# 检查文件是否存在
if not os.path.exists('./JSON/anquanke.json'):
raise FileNotFoundError(f"anquanke.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/anquanke.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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
def select_articles(e_hour):
conn = sqlite3.connect('./db/anquanke.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/anquanke.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"作者:{entry[6]}\n来源:{entry[3]}\n文章:{entry[1]}\n"
result += f"链接:{entry[2]}\n上传时间:{entry[5]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_anquanke(e_hour):
if not os.path.exists('./db/anquanke.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_anquanke_data = get_anquanke_json()
# 插入数据到数据库
insert_data(M_anquanke_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_anquanke(4)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("安全客数据为空,跳过执行。")
+138
View File
@@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/doonsec.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
description TEXT,
pubDate DATETIME,
author TEXT
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/doonsec.db')
cursor = conn.cursor()
for entry in data:
try:
# 解析 pubDate 字符串为 datetime 对象
pub_date = datetime.strptime(entry['pubDate'], '%Y-%m-%dT%H:%M:%S')
# 格式化 pubDate 为所需的格式
formatted_pub_date = pub_date.strftime('%Y-%m-%d %H:%M:%S')
except ValueError:
# 如果解析失败,使用原始 pubDate 字符串
formatted_pub_date = entry['pubDate']
cursor.execute('''
INSERT INTO articles (title, link, description, pubDate, author)
VALUES (?, ?, ?, ?, ?)
''', (entry['title'], entry['link'], entry['description'], formatted_pub_date, entry['author']))
conn.commit()
conn.close()
def get_doonsec_json():
# 检查文件是否存在
if not os.path.exists('./JSON/doonsec.json'):
raise FileNotFoundError(f"doonsec.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/doonsec.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"link": item.get("link", ""),
"description": item.get("description", ""),
"pubDate": item.get("pubDate", ""),
"author": item.get("author", ""),
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/doonsec.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/doonsec.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"作者:{entry[5]}\n文章:{entry[1]}\n"
result += f"链接:[点此访问]({entry[2]})\n上传时间:{entry[4]}\n"
result += f"简介:{entry[3]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_doonsec(e_hour):
if not os.path.exists('./db/doonsec.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_doonsec_data = get_doonsec_json()
# 插入数据到数据库
insert_data(M_doonsec_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_doonsec(4)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("洞见微信安全数据为空,跳过执行。")
+137
View File
@@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/freebuf.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
description TEXT,
pubDate DATETIME,
category TEXT
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/freebuf.db')
cursor = conn.cursor()
for entry in data:
try:
# 解析 pubDate 字符串为 datetime 对象
pub_date = datetime.strptime(entry['pubDate'], '%a, %d %b %Y %H:%M:%S %z')
# 格式化 pubDate 为所需的格式
formatted_pub_date = pub_date.strftime('%Y-%m-%d %H:%M:%S')
except ValueError:
# 如果解析失败,使用原始 pubDate 字符串
formatted_pub_date = entry['pubDate']
cursor.execute('''
INSERT INTO articles (title, link, description, pubDate, category)
VALUES (?, ?, ?, ?, ?)
''', (entry['title'], entry['link'], entry['description'], formatted_pub_date, entry['category']))
conn.commit()
conn.close()
def get_freebuf_json():
# 检查文件是否存在
if not os.path.exists('./JSON/freebuf.json'):
raise FileNotFoundError(f"freebuf.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/freebuf.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"link": item.get("link", ""),
"description": item.get("description", ""),
"pubDate": item.get("pubDate", ""),
"category": item.get("category", "")
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/freebuf.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/freebuf.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"类型:{entry[5]}\n文章:{entry[1]}\n"
result += f"链接:{entry[2]}\n上传时间:{entry[4]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_freebuf(e_hour):
if not os.path.exists('./db/freebuf.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
freebuf_data = get_freebuf_json()
# 插入数据到数据库
insert_data(freebuf_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_freebuf(4)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("Freebuf数据为空,跳过执行。")
+129
View File
@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/qianxin.db')
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
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/qianxin.db')
cursor = conn.cursor()
for entry in data:
cursor.execute('''
INSERT INTO articles (title, guid, source, description, pubDate)
VALUES (?, ?, ?, ?, ?)
''', (entry['title'], entry['guid'], entry['source'], entry['description'], entry['pubDate']))
conn.commit()
conn.close()
def get_qianxin_json():
# 检查文件是否存在
if not os.path.exists('./JSON/qianxin.json'):
raise FileNotFoundError(f"qianxin.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/qianxin.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"source": item.get("source", "")
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/qianxin.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/qianxin.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"来源:{entry[3]}\n文章:{entry[1]}\n"
result += f"链接:{entry[2]}\n上传时间:{entry[5]}\n"
result += f"描述:{entry[4]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_qianxin(e_hour):
if not os.path.exists('./db/qianxin.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_qianxin_data = get_qianxin_json()
# 插入数据到数据库
insert_data(M_qianxin_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_qianxin(4)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("奇安信攻防社区数据为空,跳过执行。")
+135
View File
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
import email.utils
def create_database():
conn = sqlite3.connect('./db/seebug.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
category TEXT,
description TEXT,
pubDate DATETIME
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/seebug.db')
cursor = conn.cursor()
for entry in data:
# 解析并格式化时间
parsed_date = email.utils.parsedate_to_datetime(entry['pubDate'])
formatted_date = parsed_date.strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
INSERT INTO articles (title, link, category, description, pubDate)
VALUES (?, ?, ?, ?, ?)
''', (entry['title'], entry['link'], entry['category'], entry['description'], formatted_date))
conn.commit()
conn.close()
def get_seebug_json():
# 检查文件是否存在
if not os.path.exists('./JSON/seebug.json'):
raise FileNotFoundError(f"seebug.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/seebug.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"link": item.get("link", ""),
"description": item.get("description", ""),
"pubDate": item.get("pubDate", ""),
"category": item.get("category", "")
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/seebug.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
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():
conn = sqlite3.connect('./db/seebug.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"类型:{entry[3]}\n文章:{entry[1]}"
result += f"链接:{entry[2]}\n上传时间:{entry[5]}\n"
result += f"{entry[4]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_seebug(e_hour):
if not os.path.exists('./db/seebug.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_seebug_data = get_seebug_json()
# 插入数据到数据库
insert_data(M_seebug_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_seebug(100)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("Seebug社区数据为空,跳过执行。")
+134
View File
@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-
import json
import sqlite3
import os
from datetime import datetime, timedelta
from SendBot import SendToFeishu
def create_database():
conn = sqlite3.connect('./db/xianzhi.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
published DATETIME
)''')
conn.commit()
conn.close()
def insert_data(data):
conn = sqlite3.connect('./db/xianzhi.db')
cursor = conn.cursor()
for entry in data:
try:
# 解析 published 字符串为 datetime 对象
pub_date = datetime.strptime(entry['published'], '%Y-%m-%dT%H:%M:%S%z')
# 格式化 published 为所需的格式
formatted_pub_date = pub_date.strftime('%Y-%m-%d %H:%M:%S')
# print(formatted_pub_date)
except ValueError:
# 如果解析失败,使用原始 published 字符串
formatted_pub_date = entry['published']
cursor.execute('''
INSERT INTO articles (title, link, published)
VALUES (?, ?, ?)
''', (entry['title'], entry['link'], formatted_pub_date))
conn.commit()
conn.close()
def get_xianzhi_json():
# 检查文件是否存在
if not os.path.exists('./JSON/xianzhi.json'):
raise FileNotFoundError(f"xianzhi.json文件不存在,请检查程序是否运行正常!")
# 打开并读取JSON文件
with open('./JSON/xianzhi.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 假设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", ""),
"link": item.get("link", ""),
"published": item.get("published", "")
}
total_data.append(entry)
return total_data
def select_articles(e_hour):
conn = sqlite3.connect('./db/xianzhi.db')
cursor = conn.cursor()
# 获取当前日期和时间
now = datetime.now()
start_time = now - timedelta(hours=e_hour)
end_time = now
# 查询指定时间段内的数据
cursor.execute('''
SELECT * FROM articles
WHERE published 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():
conn = sqlite3.connect('./db/xianzhi.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM articles')
conn.commit()
conn.close()
def get_filtered_articles(entries):
result = ""
for entry in entries:
result += f"文章:{entry[1]}\n"
result += f"链接:{entry[2]}\n上传时间:{entry[3]}\n"
result += "-" * 40 + "\n" # 添加分隔线以便区分不同文章
return result
def Src_xianzhi(e_hour):
if not os.path.exists('./db/xianzhi.db'):
# 创建数据库和表
create_database()
# 清空表
clear_table()
# 获取 JSON 数据
M_xianzhi_data = get_xianzhi_json()
# 插入数据到数据库
insert_data(M_xianzhi_data)
# 查询指定时间段内的数据
filtered_articles = select_articles(e_hour)
# print(filtered_articles)
if filtered_articles:
results = get_filtered_articles(filtered_articles)
return results
else:
return False
if __name__ == "__main__":
reslts = Src_xianzhi(4)
if reslts != False:
print(reslts)
else:
# 如果为空,则跳过执行
print("-" * 40)
print("先知社区数据为空,跳过执行。")