|
@@ -18,10 +18,11 @@ class FreshRSSDatabase:
|
|
|
self.conn = None
|
|
self.conn = None
|
|
|
self.keys = [
|
|
self.keys = [
|
|
|
{'web3新闻': 'web3|区块链|NFT|DeFi|NFT'},
|
|
{'web3新闻': 'web3|区块链|NFT|DeFi|NFT'},
|
|
|
- {'购物类新闻': '大疆|无人机|硬盘|鼠标|纸巾|穿越机'},
|
|
|
|
|
|
|
+ {'购物类新闻': '大疆|无人机|硬盘|鼠标|纸巾|穿越机|礼物'},
|
|
|
{'coin新闻': 'btc|eth|sui|degen'}
|
|
{'coin新闻': 'btc|eth|sui|degen'}
|
|
|
]
|
|
]
|
|
|
self.ellipsis = 300
|
|
self.ellipsis = 300
|
|
|
|
|
+ self.days = 3
|
|
|
|
|
|
|
|
def connect(self):
|
|
def connect(self):
|
|
|
"""连接到 PostgreSQL 数据库"""
|
|
"""连接到 PostgreSQL 数据库"""
|
|
@@ -45,11 +46,6 @@ class FreshRSSDatabase:
|
|
|
print("Database connection failed")
|
|
print("Database connection failed")
|
|
|
return None
|
|
return None
|
|
|
try:
|
|
try:
|
|
|
- # 计算过去一天的时间戳
|
|
|
|
|
- one_day_ago = datetime.datetime.now() - datetime.timedelta(days=1)
|
|
|
|
|
- # 将 datetime 对象转换为时间戳
|
|
|
|
|
- one_day_ago_timestamp = one_day_ago.timestamp()
|
|
|
|
|
-
|
|
|
|
|
cur = self.conn.cursor()
|
|
cur = self.conn.cursor()
|
|
|
conditions = [f"title ILIKE '%{keyword}%' AND content ILIKE '%{keyword}%'" for keyword in
|
|
conditions = [f"title ILIKE '%{keyword}%' AND content ILIKE '%{keyword}%'" for keyword in
|
|
|
keywords.split('|')]
|
|
keywords.split('|')]
|
|
@@ -112,6 +108,60 @@ class FreshRSSDatabase:
|
|
|
except smtplib.SMTPException as e:
|
|
except smtplib.SMTPException as e:
|
|
|
print("Error: 无法发送邮件", e)
|
|
print("Error: 无法发送邮件", e)
|
|
|
|
|
|
|
|
|
|
+ def query_and_process_key(self, key_name, keywords):
|
|
|
|
|
+ records = self.execute_query(keywords)
|
|
|
|
|
+ if records:
|
|
|
|
|
+ unique_records = {}
|
|
|
|
|
+ for record in records:
|
|
|
|
|
+ title = self.remove_all_html_tags(record[2]) # 获取标题
|
|
|
|
|
+ if title not in unique_records:
|
|
|
|
|
+ unique_records[title] = {
|
|
|
|
|
+ "title": title,
|
|
|
|
|
+ "content": self.remove_all_html_tags(record[4]),
|
|
|
|
|
+ "link": record[5],
|
|
|
|
|
+ "postdate": (datetime.datetime.utcfromtimestamp(record[7])
|
|
|
|
|
+ .strftime('%Y-%m-%d %H:%M:%S')) if record[7] else '',
|
|
|
|
|
+ "posttimestamp": record[7] or 0
|
|
|
|
|
+ }
|
|
|
|
|
+ return list(unique_records.values())
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ def prepare_to_send(self, data):
|
|
|
|
|
+ source_key = data.get('source_key')
|
|
|
|
|
+ keys = data.get('keys')
|
|
|
|
|
+ data_list = data.get('data')
|
|
|
|
|
+
|
|
|
|
|
+ filter_data = []
|
|
|
|
|
+
|
|
|
|
|
+ # 计算过去一天的时间戳
|
|
|
|
|
+ one_day_ago = datetime.datetime.now() - datetime.timedelta(days=self.days)
|
|
|
|
|
+ # 将 datetime 对象转换为时间戳
|
|
|
|
|
+ one_day_ago_timestamp = one_day_ago.timestamp()
|
|
|
|
|
+
|
|
|
|
|
+ for value in data_list:
|
|
|
|
|
+ if value['posttimestamp'] >= one_day_ago_timestamp:
|
|
|
|
|
+ filter_data.append(value)
|
|
|
|
|
+
|
|
|
|
|
+ sorted_list = sorted(filter_data, key=lambda x: x['posttimestamp'], reverse=True)
|
|
|
|
|
+
|
|
|
|
|
+ subject = 'RSS' + data.get('source_key')
|
|
|
|
|
+ title = source_key
|
|
|
|
|
+
|
|
|
|
|
+ key_data_total = len(data.get('data'))
|
|
|
|
|
+ text = '关键词:\n' + data.get('keys').replace('|', '\n') + '\n\n'
|
|
|
|
|
+ text += '一共搜索到: ' + str(key_data_total) + ' 条数据\n\n'
|
|
|
|
|
+ text += '*' * 80 + '\n'
|
|
|
|
|
+ for d in sorted_list:
|
|
|
|
|
+ text += '标题: ' + d.get('title') + '\n'
|
|
|
|
|
+ text += '内容: ' + d.get('content') + '\n'
|
|
|
|
|
+ text += '链接: ' + d.get('link') + '\n'
|
|
|
|
|
+ text += '发布日期: ' + d.get('postdate') + '\n'
|
|
|
|
|
+ text += '时间戳: ' + str(d.get('posttimestamp')) + '\n\n'
|
|
|
|
|
+ text += '*' * 80
|
|
|
|
|
+ text += '\n\n'
|
|
|
|
|
+
|
|
|
|
|
+ self.send_email(subject=subject, title=title, text=text)
|
|
|
|
|
+
|
|
|
def main(self):
|
|
def main(self):
|
|
|
# 执行查询
|
|
# 执行查询
|
|
|
loaded_data = {}
|
|
loaded_data = {}
|
|
@@ -137,41 +187,9 @@ class FreshRSSDatabase:
|
|
|
self.close()
|
|
self.close()
|
|
|
|
|
|
|
|
for source_key, data in loaded_data.items():
|
|
for source_key, data in loaded_data.items():
|
|
|
- subject = 'RSS' + data.get('source_key')
|
|
|
|
|
- title = source_key
|
|
|
|
|
-
|
|
|
|
|
- key_data_total = len(data.get('data'))
|
|
|
|
|
- text = '关键词:\n' + data.get('keys').replace('|', '\n') + '\n\n'
|
|
|
|
|
- text += '一共搜索到: ' + str(key_data_total) + ' 条数据\n\n'
|
|
|
|
|
- text += '*' * 80 + '\n'
|
|
|
|
|
- for d in data.get('data'):
|
|
|
|
|
- text += '标题: ' + d.get('title') + '\n'
|
|
|
|
|
- text += '内容: ' + d.get('content') + '\n'
|
|
|
|
|
- text += '链接: ' + d.get('link') + '\n'
|
|
|
|
|
- text += '发布日期: ' + d.get('postdate') + '\n'
|
|
|
|
|
- text += '时间戳: ' + str(d.get('posttimestamp')) + '\n\n'
|
|
|
|
|
- text += '*' * 80
|
|
|
|
|
- text += '\n\n'
|
|
|
|
|
-
|
|
|
|
|
- self.send_email(subject=subject, title=title, text=text)
|
|
|
|
|
|
|
+ self.prepare_to_send(data)
|
|
|
|
|
|
|
|
- def query_and_process_key(self, key_name, keywords):
|
|
|
|
|
- records = self.execute_query(keywords)
|
|
|
|
|
- if records:
|
|
|
|
|
- unique_records = {}
|
|
|
|
|
- for record in records:
|
|
|
|
|
- title = self.remove_all_html_tags(record[2]) # 获取标题
|
|
|
|
|
- if title not in unique_records:
|
|
|
|
|
- unique_records[title] = {
|
|
|
|
|
- "title": title,
|
|
|
|
|
- "content": self.remove_all_html_tags(record[4]),
|
|
|
|
|
- "link": record[5],
|
|
|
|
|
- "postdate": (datetime.datetime.utcfromtimestamp(record[7])
|
|
|
|
|
- .strftime('%Y-%m-%d %H:%M:%S')) if record[7] else '',
|
|
|
|
|
- "posttimestamp": record[7] or 0
|
|
|
|
|
- }
|
|
|
|
|
- return list(unique_records.values())
|
|
|
|
|
- return None
|
|
|
|
|
|
|
+ print('done!')
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|