Ver Fonte

v0.5
新增articles接口

罗俊辉 há 1 ano atrás
pai
commit
7176a2105f
5 ficheiros alterados com 215 adições e 2 exclusões
  1. 43 0
      applications/ai.py
  2. 2 1
      deal/__init__.py
  3. 62 0
      deal/articles_deal.py
  4. 12 1
      routes/vta_routes.py
  5. 96 0
      spider/wechatSogou.py

+ 43 - 0
applications/ai.py

@@ -2,8 +2,15 @@
 @author: luojunhui
 """
 import json
+
+
 import requests
 from openai import OpenAI
+from tencentcloud.common import credential
+from tencentcloud.common.profile.client_profile import ClientProfile
+from tencentcloud.common.profile.http_profile import HttpProfile
+from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
+from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
 
 
 def kimi_ai(prompt):
@@ -46,3 +53,39 @@ def metaSo(prompt, mode="深入"):
     }
     response = requests.post(url=url, json=body, headers=header)
     return response.json()['data']
+
+
+def tencent_ai(prompt):
+    """
+    腾讯混元aiTool
+    :param prompt:
+    :return:
+    """
+    secret_id = "AKIDDSelkqm1BCyulT0L7iXUFvXyCKNfkcS5"
+    secret_key = "XrJh4GLrNp7Lh4VrLsLp9NVj8W3ZgmiZ"
+    cred = credential.Credential(secret_id, secret_key)
+    httpProfile = HttpProfile()
+    httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
+    clientProfile = ClientProfile()
+    clientProfile.httpProfile = httpProfile
+    client = hunyuan_client.HunyuanClient(cred, "ap-beijing", clientProfile)
+    req = models.ChatCompletionsRequest()
+    params = {
+        "Model": "hunyuan-pro",
+        "Messages": [
+            {
+                "Role": "user",
+                "Content": prompt
+            }
+        ],
+        "Stream": False,
+        "StreamModeration": False,
+        "EnableEnhancement": True
+    }
+    try:
+        req.from_json_string(json.dumps(params))
+        resp = client.ChatCompletions(req)
+        resp_json = json.loads(str(resp))
+        return resp_json['Choices'][0]['Message']['Content']
+    except TencentCloudSDKException as err:
+        return {"error": str(err)}

+ 2 - 1
deal/__init__.py

@@ -3,4 +3,5 @@
 """
 from .videos_deal import RequestDeal
 from .publish_deal import PublishDeal
-from .db_deal import insert_text_mysql, get_text_by_id
+from .db_deal import insert_text_mysql, get_text_by_id
+from .articles_deal import ArticleGeneral

+ 62 - 0
deal/articles_deal.py

@@ -0,0 +1,62 @@
+"""
+@author: luojunhui
+生成文章&&搜索文章
+"""
+from applications.ai import tencent_ai, kimi_ai, metaSo
+
+
+class ArticleGeneral(object):
+    """
+    Generate article with AI
+    """
+
+    def __init__(self, params):
+        self.text = None
+        self.title = None
+        self.aic = None
+        self.params = params
+
+    def check_params(self):
+        """
+        校验params
+        :return:
+        """
+        try:
+            self.aic = self.params['aic']
+            self.title = self.params['title']
+            self.text = self.params['text']
+            return None
+        except AttributeError as err:
+            return {"error": "params error {}".format(err)}
+
+    def deal(self):
+        """
+        处理请求
+        :return:
+        """
+        params_error = self.check_params()
+        if params_error:
+            return params_error
+        else:
+            if self.aic == "tencent":
+                prompt = "请通过这个标题({}), 生成一篇文章".format(self.title)
+                response_text = tencent_ai(
+                    prompt=prompt
+                )
+                return {"text": response_text}
+            elif self.aic == "kimi":
+                if self.text:
+                    prompt = "请通过这个标题({})和这段正文({}), 生成一篇文章".format(self.title, self.text)
+                else:
+                    prompt = "请通过这个标题({}), 生成一篇文章".format(self.title)
+                response_text = kimi_ai(prompt)
+                return {"text": response_text}
+            elif self.aic == "meta":
+                if self.text:
+                    prompt = "请通过这个标题({})和这段正文({}), 生成一篇文章".format(self.title, self.text)
+                else:
+                    prompt = "请通过这个标题({}), 生成一篇文章".format(self.title)
+                response_text = metaSo(prompt)
+                return {"text": response_text}
+            else:
+                return {"error": "errors"}

+ 12 - 1
routes/vta_routes.py

@@ -3,7 +3,7 @@
 """
 from quart import Blueprint, jsonify, request
 
-from deal import RequestDeal, insert_text_mysql, get_text_by_id, PublishDeal
+from deal import RequestDeal, insert_text_mysql, get_text_by_id, PublishDeal, ArticleGeneral
 from applications.functions import whisper
 
 
@@ -70,6 +70,17 @@ def VTARoutes(mysql_client):
         res = P.deal()
         return jsonify(res)
 
+    @bp.route('/article', methods=["POST"])
+    async def generate_text():
+        """
+        生成文本
+        :return:
+        """
+        params = await request.get_json()
+        A = ArticleGeneral(params=params)
+        res = A.deal()
+        return jsonify(res)
+
     return bp
 
 

+ 96 - 0
spider/wechatSogou.py

@@ -0,0 +1,96 @@
+"""
+@author: luojunhui
+"""
+import time
+import random
+import requests
+from lxml import etree
+from fake_useragent import FakeUserAgent
+from gne import GeneralNewsExtractor
+from selenium import webdriver
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.chrome.options import Options
+from webdriver_manager.chrome import ChromeDriverManager
+
+
+def selenium_text(url):
+    # 配置 Chrome 选项
+    chrome_options = Options()
+    chrome_options.add_argument('--headless')  # 无头模式
+    # chrome_options.add_argument('--disable-gpu')
+    # chrome_options.add_argument('--no-sandbox')
+    # chrome_options.add_argument('--disable-dev-shm-usage')
+    # chrome_options.add_argument('--disable-blink-features=AutomationControlled')
+    # chrome_options.add_argument(
+    #     f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')
+    # chrome_options.add_argument('--incognito')
+    window_width = random.randint(800, 1200)
+    window_height = random.randint(600, 800)
+
+
+    # chrome_options.add_argument('--proxy-server=http://t17772369458618:5zqcjkmy@q796.kdltps.com:15818/')
+    service = Service(ChromeDriverManager().install())
+    driver = webdriver.Chrome(service=service, options=chrome_options)
+    driver.set_window_size(window_width, window_height)
+    driver.get(url)
+    page_text = driver.page_source
+    driver.quit()
+    return page_text
+
+
+def tunnel_proxies():
+    # 隧道域名:端口号
+    tunnel = "q796.kdltps.com:15818"
+    # 用户名密码方式
+    username = "t17772369458618"
+    password = "5zqcjkmy"
+    proxies = {
+        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
+        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
+    }
+    return proxies
+
+
+def extract(url):
+    """
+    ttt
+    :param url:
+    :return:
+    """
+    html_text = selenium_text(url)
+    extractor = GeneralNewsExtractor()
+    result = extractor.extract(html_text)
+    print(result)
+
+
+def sogou_wechat(keyword):
+    """
+    :param keyword:
+    :return:
+    """
+    url = "https://weixin.sogou.com/weixin?type=2&query={}".format(keyword)
+    print(url)
+    headers = {
+        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
+        'Accept-Language': 'en,zh;q=0.9,zh-CN;q=0.8',
+        'Cache-Control': 'max-age=0',
+        'Connection': 'keep-alive',
+        'Cookie': '',
+        'Referer': 'https://weixin.sogou.com/weixin',
+        'Upgrade-Insecure-Requests': '1',
+        'User-Agent': FakeUserAgent().chrome
+    }
+
+    response = requests.request("GET", url, headers=headers, proxies=tunnel_proxies())
+    e_tree = etree.HTML(response.text)
+
+    xpath = r'//ul[@class="news-list"]/li/div/a/@href'
+
+    url_list = e_tree.xpath(xpath)
+    url_list = ["https://weixin.sogou.com/" + i for i in url_list]
+    for url in url_list:
+        print(url)
+        extract(url)
+
+
+sogou_wechat("人民日报")