jack 11 kuukautta sitten
vanhempi
sitoutus
41818a3454
3 muutettua tiedostoa jossa 23 lisäystä ja 11 poistoa
  1. 10 1
      ai_news/Readme.md
  2. 10 10
      ai_news/ai_news.py
  3. 3 0
      ai_news/config.json

+ 10 - 1
ai_news/Readme.md

@@ -1 +1,10 @@
-pip install httpx beautifulsoup ollama matrix_client
+## Installation
+pip install httpx beautifulsoup ollama matrix_client
+
+## Usage
+python main.py
+
+## Configuration
+1. Generate config for the first run of the project.
+2. Modify configuration
+3. run the project

+ 10 - 10
ai_news/ai_news.py

@@ -38,7 +38,7 @@ class OllamaChat(object):
 
 
 class MatrixBot:
-    def __init__(self, user, password):
+    def __init__(self, user, password, key):
         self.base_url = "https://matrix.erhe.top"
         self.user = user
         self.password = password
@@ -78,7 +78,8 @@ class AINEWS:
             default_config = {
                 "example": {
                     "use_browser": 0,
-                    "ai_host": 'http://127.0.0.1:11434',
+                    "ai_host": 'http://127.0.0.1:11434(不需要此功能留空)',
+                    "message_bot_key": '填入matrix的key(不需要此功能留空)',
                     "target_url_list": ['目标网站'],
                     "role": "AI的角色, 例如: 你是一个聊天机器人",
                     "prompt_words": "提示词: 帮我总结, 用中文回复"
@@ -114,7 +115,7 @@ class AINEWS:
             exit(0)
 
         k = config[key]
-        return k['target_url_list'], k['prompt_words'], k['role'], k['use_browser'], k['ai_host']
+        return k['target_url_list'], k['prompt_words'], k['role'], k['use_browser'], k['ai_host'], k['message_bot_key']
 
     async def get_htmls(self, urls):
         # 获取 HTML
@@ -214,7 +215,7 @@ class AINEWS:
             await asyncio.sleep(0.5)  # 使用异步 sleep
         await page.evaluate("window.scrollTo({top: 0, behavior: 'smooth'})")
 
-    def process_data(self, result_text, prompt_words, role):
+    def process_data(self, result_text, prompt_words, role, ai_host):
         # 整理获取的数据, 返回准备发送的数据
         process_send = []
         O = OllamaChat(ai_host)
@@ -233,7 +234,7 @@ class AINEWS:
                 process_send.append(response_context)
         return process_send
 
-    def main(self, target_url_list, prompt_words, role, use_browser, ai_host):
+    def main(self, target_url_list, prompt_words, role, use_browser, ai_host, message_bot_key):
         # 获取所有的网页html内容
         if use_browser:
             result_text = asyncio.run(self.get_htmls_with_browser(target_url_list))
@@ -249,12 +250,11 @@ class AINEWS:
             exit(0)
 
         # # 如果只需要保存爬取数据, 不使用 AI, 注释下面
-        # # 创建消息bot实例
-        # bot = MatrixBot('message-bot', 'aaaAAA111!!!')
-        #
         # # 处理发送 text 数据
         # process_send = self.process_data(result_text, prompt_words, role, ai_host)
         #
+        # # 创建消息bot实例
+        # bot = MatrixBot('message-bot', 'aaaAAA111!!!', message_bot_key)
         # # 发送消息
         # for process_text in process_send:
         #     bot.send_message(process_text)
@@ -265,7 +265,7 @@ if __name__ == "__main__":
     ainews.create_config_if_not_exists()
 
     for key in key_list:
-        target_url_list, prompt_words, role, use_browser, ai_host = ainews.load_config(key)
+        target_url_list, prompt_words, role, use_browser, ai_host, message_bot_key = ainews.load_config(key)
         print(f'关键词 {key} 共有 {len(target_url_list)} 个网址')
-        ainews.main(target_url_list, prompt_words, role, use_browser, ai_host)
+        ainews.main(target_url_list, prompt_words, role, use_browser, ai_host, message_bot_key)
     print('done!')

+ 3 - 0
ai_news/config.json

@@ -2,6 +2,7 @@
   "example": {
     "use_browser": 0,
     "ai_host": "http://127.0.0.1:11434",
+    "message_bot_key": "",
     "target_url_list": [],
     "prompt_words": "",
     "role": ""
@@ -9,6 +10,7 @@
   "web3": {
     "use_browser": 1,
     "ai_host": "http://home.erhe.link:36001",
+    "message_bot_key": "!CgWvWEnLbKYvhXLvil:chat.abeginner.cn",
     "target_url_list": [
       "https://wublock123.com",
       "https://m.odaily.news/newsflash",
@@ -26,6 +28,7 @@
   "A": {
     "use_browser": 1,
     "ai_host": "http://home.erhe.link:36001",
+    "message_bot_key": "",
     "target_url_list": [
       "https://www.eastmoney.com/",
       "https://www.jrj.com.cn/",