commit
This commit is contained in:
168
app.py
168
app.py
@@ -33,8 +33,8 @@ scheduler = ClickScheduler()
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
"""首页 - 重定向到数据概览"""
|
||||
return redirect('/dashboard.html')
|
||||
"""首页 - 重定向到新的单页应用"""
|
||||
return redirect('/app.html')
|
||||
|
||||
|
||||
@app.route('/health', methods=['GET'])
|
||||
@@ -196,14 +196,103 @@ def get_scheduler_status():
|
||||
|
||||
|
||||
# AdsPower 接口调试
|
||||
@app.route('/api/adspower/profiles', methods=['GET'])
|
||||
def adspower_list_profiles():
|
||||
"""查询Profile列表"""
|
||||
@app.route('/api/adspower/groups', methods=['GET'])
|
||||
def adspower_list_groups():
|
||||
"""查询分组列表"""
|
||||
try:
|
||||
from adspower_client import AdsPowerClient
|
||||
|
||||
group_name = request.args.get('group_name')
|
||||
page = request.args.get('page', 1, type=int)
|
||||
page_size = request.args.get('page_size', 2000, type=int)
|
||||
|
||||
client = AdsPowerClient()
|
||||
result = client.list_profiles()
|
||||
return jsonify({'success': True, 'data': result})
|
||||
result = client.list_groups(group_name=group_name, page=page, page_size=page_size)
|
||||
|
||||
if result:
|
||||
return jsonify({'success': True, 'data': result})
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '查询分组列表失败'}), 500
|
||||
except Exception as e:
|
||||
logger.error(f"AdsPower查询分组异常: {str(e)}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/adspower/group/env', methods=['GET'])
|
||||
def adspower_get_group_by_env():
|
||||
"""根据当前运行环境获取对应的分组ID"""
|
||||
try:
|
||||
from adspower_client import AdsPowerClient
|
||||
|
||||
client = AdsPowerClient()
|
||||
group_id = client.get_group_by_env()
|
||||
|
||||
if group_id:
|
||||
return jsonify({'success': True, 'data': {'group_id': group_id, 'env': Config.ENV}})
|
||||
else:
|
||||
return jsonify({'success': False, 'message': f'未找到对应环境的分组'}), 404
|
||||
except Exception as e:
|
||||
logger.error(f"AdsPower获取环境分组异常: {str(e)}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/adspower/profiles', methods=['GET'])
|
||||
def adspower_list_profiles():
|
||||
"""查询Profile列表(支持多个查询参数)"""
|
||||
try:
|
||||
from adspower_client import AdsPowerClient
|
||||
import json as json_module
|
||||
|
||||
# 获取查询参数
|
||||
group_id = request.args.get('group_id')
|
||||
page = request.args.get('page', 1, type=int)
|
||||
limit = request.args.get('limit', type=int) # 可选
|
||||
page_size = request.args.get('page_size', type=int) # 可选
|
||||
|
||||
# 数组参数(JSON格式)
|
||||
profile_id = request.args.get('profile_id')
|
||||
profile_no = request.args.get('profile_no')
|
||||
|
||||
# 解析JSON数组
|
||||
if profile_id:
|
||||
try:
|
||||
profile_id = json_module.loads(profile_id)
|
||||
except:
|
||||
profile_id = None
|
||||
|
||||
if profile_no:
|
||||
try:
|
||||
profile_no = json_module.loads(profile_no)
|
||||
except:
|
||||
profile_no = None
|
||||
|
||||
# 排序参数
|
||||
sort_type = request.args.get('sort_type')
|
||||
sort_order = request.args.get('sort_order')
|
||||
|
||||
# 如果没有指定group_id,尝试根据环境自动获取
|
||||
client = AdsPowerClient()
|
||||
if not group_id:
|
||||
group_id = client.get_group_by_env()
|
||||
if group_id:
|
||||
logger.info(f"自动获取到分组ID: {group_id}")
|
||||
|
||||
# 查询Profile列表
|
||||
result = client.list_profiles(
|
||||
group_id=group_id,
|
||||
page=page,
|
||||
page_size=page_size if page_size else 100,
|
||||
profile_id=profile_id,
|
||||
profile_no=profile_no,
|
||||
limit=limit,
|
||||
sort_type=sort_type,
|
||||
sort_order=sort_order
|
||||
)
|
||||
|
||||
if result:
|
||||
return jsonify({'success': True, 'data': result})
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '查询Profile列表失败'}), 500
|
||||
except Exception as e:
|
||||
logger.error(f"AdsPower查询Profile异常: {str(e)}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
@@ -338,6 +427,71 @@ def adspower_update_profile_v1():
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
# 数据库查询接口
|
||||
@app.route('/api/clicks', methods=['GET'])
|
||||
def get_all_clicks():
|
||||
"""获取所有点击记录"""
|
||||
try:
|
||||
from db_manager import ClickManager
|
||||
|
||||
site_id = request.args.get('site_id', type=int)
|
||||
limit = request.args.get('limit', 100, type=int)
|
||||
|
||||
click_mgr = ClickManager()
|
||||
|
||||
if site_id:
|
||||
clicks = click_mgr.get_clicks_by_site(site_id, limit=limit)
|
||||
else:
|
||||
# 获取所有点击记录
|
||||
conn = click_mgr.get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT * FROM ai_mip_click
|
||||
ORDER BY click_time DESC
|
||||
LIMIT ?
|
||||
""", (limit,))
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
clicks = [click_mgr._dict_from_row(row) for row in rows]
|
||||
|
||||
return jsonify({'success': True, 'data': clicks})
|
||||
except Exception as e:
|
||||
logger.error(f"查询点击记录异常: {str(e)}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/interactions', methods=['GET'])
|
||||
def get_all_interactions():
|
||||
"""获取所有互动记录"""
|
||||
try:
|
||||
from db_manager import InteractionManager
|
||||
|
||||
site_id = request.args.get('site_id', type=int)
|
||||
limit = request.args.get('limit', 100, type=int)
|
||||
|
||||
interaction_mgr = InteractionManager()
|
||||
|
||||
if site_id:
|
||||
interactions = interaction_mgr.get_interactions_by_site(site_id, limit=limit)
|
||||
else:
|
||||
# 获取所有互动记录
|
||||
conn = interaction_mgr.get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT * FROM ai_mip_interaction
|
||||
ORDER BY interaction_time DESC
|
||||
LIMIT ?
|
||||
""", (limit,))
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
interactions = [interaction_mgr._dict_from_row(row) for row in rows]
|
||||
|
||||
return jsonify({'success': True, 'data': interactions})
|
||||
except Exception as e:
|
||||
logger.error(f"查询互动记录异常: {str(e)}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info(f"启动MIP广告点击服务 - 环境: {Config.ENV}")
|
||||
logger.info(f"服务地址: http://{Config.SERVER_HOST}:{Config.SERVER_PORT}")
|
||||
|
||||
Reference in New Issue
Block a user