132 lines
5.6 KiB
Python
132 lines
5.6 KiB
Python
import os
|
||
import json
|
||
import aiohttp
|
||
import asyncio
|
||
import re
|
||
from config import Config
|
||
from db_manager import DatabaseManager
|
||
|
||
class AIContentAnalyzer:
|
||
def __init__(self):
|
||
self.api_key = Config.DEEPSEEK_API_KEY
|
||
self.api_url = Config.DEEPSEEK_API_URL
|
||
self.db_manager = DatabaseManager()
|
||
|
||
async def analyze_content(self, title, url=None):
|
||
print(f"\n开始AI分析内容:{title}")
|
||
prompt = f"""请完成以下任务:
|
||
|
||
1. 分析标题:{title}
|
||
判断这是游戏、软件还是APP。如果是APP,请进一步判断是Android应用还是APP游戏。
|
||
请严格按照以下类型返回:
|
||
- 如果是手机游戏,返回"APP游戏"
|
||
- 如果是Android应用,返回"Android应用"
|
||
- 如果是苹果IOS,返回"IOS"
|
||
- 如果是电脑游戏,返回"游戏"
|
||
- 如果是电脑软件,返回"软件"
|
||
|
||
2. 优化标题格式:
|
||
如果是游戏,改写为'喜加一[如steam,epic]游戏 游戏名称'的格式。
|
||
如果是字体软件,改写为'字体名称+字体类型'的格式。
|
||
其他软件,改写为'软件名称+软件类型(如视频编辑器软件、修图软件等)'的格式。
|
||
|
||
3. 生成软件评测:
|
||
请对这个软件或游戏进行一个专业的评测和介绍,字数限制在500个字符内,用一段话介绍即可。请按以下格式返回:
|
||
|
||
类型:[软件/游戏/APP类型]
|
||
标题:[格式化后的标题]
|
||
评测:[软件评测内容]"""
|
||
|
||
print("正在调用Deepseek AI进行内容分析...")
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Authorization": f"Bearer {self.api_key}"
|
||
}
|
||
data = {
|
||
"model": "deepseek-chat",
|
||
"messages": [
|
||
{
|
||
"role": "system",
|
||
"content": "你是一位限时免费游戏和软件资源分享博客的博主。"
|
||
},
|
||
{
|
||
"role": "user",
|
||
"content": prompt
|
||
}
|
||
],
|
||
"temperature": 0,
|
||
"max_tokens": 1000
|
||
}
|
||
|
||
try:
|
||
async with aiohttp.ClientSession() as session:
|
||
async with session.post(self.api_url, headers=headers, json=data) as response:
|
||
if response.status == 200:
|
||
result = await response.json()
|
||
content = result['choices'][0]['message']['content']
|
||
print("Deepseek AI响应成功,正在解析结果...")
|
||
else:
|
||
error_text = await response.text()
|
||
raise Exception(f"API调用失败:{error_text}")
|
||
print(f"AI响应内容:{content}")
|
||
|
||
# 优化解析逻辑,支持多种格式
|
||
lines = [line.strip() for line in content.split('\n') if line.strip()]
|
||
result = {}
|
||
|
||
# 初始化默认值
|
||
result['type'] = '软件'
|
||
result['category_id'] = 2
|
||
result['parent_category_id'] = None
|
||
result['title'] = title
|
||
result['review'] = ''
|
||
|
||
for line in lines:
|
||
# 移除序号前缀(如果存在)
|
||
line = re.sub(r'^\d+.\s*', '', line.strip())
|
||
|
||
if line.lower().startswith('类型:') or line.lower().startswith('类型:'):
|
||
content_type = line.split(':')[-1].split(':')[-1].strip('[]')
|
||
# 根据AI返回的类型确定WordPress分类ID
|
||
if 'ios' in content_type.lower():
|
||
result['type'] = 'IOS'
|
||
result['category_id'] = 434
|
||
result['parent_category_id'] = 3
|
||
elif 'app游戏' in content_type.lower():
|
||
result['type'] = '手机游戏'
|
||
result['category_id'] = 56
|
||
result['parent_category_id'] = 3
|
||
elif 'android' in content_type.lower():
|
||
result['type'] = 'Android应用'
|
||
result['category_id'] = 57
|
||
result['parent_category_id'] = 3
|
||
elif '游戏' in content_type.lower():
|
||
result['type'] = '游戏'
|
||
result['category_id'] = 1
|
||
else:
|
||
result['type'] = '软件'
|
||
result['category_id'] = 2
|
||
elif line.lower().startswith('标题:') or line.lower().startswith('标题:'):
|
||
result['title'] = line.split(':')[-1].split(':')[-1].strip('[]')
|
||
elif line.lower().startswith('评测:') or line.lower().startswith('评测:'):
|
||
result['review'] = line.split(':')[-1].split(':')[-1].strip('[]')
|
||
|
||
print("\nAI分析结果:")
|
||
print(f"- 内容类型:{result['type']} (分类ID: {result['category_id']})")
|
||
print(f"- 优化标题:{result['title']}")
|
||
print(f"- 评测内容:{result['review']}")
|
||
|
||
if url:
|
||
await self.db_manager.add_processed_article(
|
||
url=url,
|
||
title=title,
|
||
content_type=result['type'],
|
||
optimized_title=result['title']
|
||
)
|
||
|
||
return result
|
||
|
||
except Exception as e:
|
||
error_msg = f"AI分析失败: {str(e)}"
|
||
print(f"\n错误:{error_msg}")
|
||
raise Exception(error_msg) |