|
@@ -1,4 +1,4 @@
|
|
|
-from wechat_action.sql_tools import save_wechat_cookies, save_human_info, delete_wechat_info, save_wechat_info
|
|
|
+from wechat_action.sql_tools import save_human_info, delete_wechat_info, save_wechat_info
|
|
|
from wechat_api.get_wechat_info import WechatApi
|
|
|
from wechat_action.create_ad_layout import CreateAd
|
|
|
from wechat_action.create_ad_plan import CreateAdPlan
|
|
@@ -7,21 +7,22 @@ from sqlalchemy import Table
|
|
|
from communication_tools import dingtalk
|
|
|
import json
|
|
|
import logging
|
|
|
-import time
|
|
|
+from datetime import datetime
|
|
|
|
|
|
layout_create_action = 'create_ad_layout'
|
|
|
ad_plan_create_action = 'create_ad_plan'
|
|
|
+refresh_wechat_action = 'refresh_wechat_info'
|
|
|
|
|
|
|
|
|
def run(user_id, log_ad, db, cookie_canuse):
|
|
|
- sql_session = db.DBSession()
|
|
|
- # 检查是否有已经开启的任务
|
|
|
- result = sql_tools.get_action_status(sql_session, user_id)
|
|
|
- # 有: 只将任务进行添加操作todo
|
|
|
- if result:
|
|
|
- action_info = {'user_id': user_id, 'service_name': ''}
|
|
|
-
|
|
|
- sql_tools.save_action_record()
|
|
|
+ # sql_session = db.DBSession()
|
|
|
+ # # 检查是否有已经开启的任务
|
|
|
+ # result = sql_tools.get_action_status(sql_session, user_id)
|
|
|
+ # # 有: 只将任务进行添加操作todo
|
|
|
+ # if result:
|
|
|
+ # action_info = {'user_id': user_id, 'service_name': ''}
|
|
|
+ #
|
|
|
+ # sql_tools.save_action_record()
|
|
|
# 无: 任务添加到数据库中doing
|
|
|
|
|
|
# 检查是否需要登录
|
|
@@ -29,6 +30,7 @@ def run(user_id, log_ad, db, cookie_canuse):
|
|
|
# 任务中间添加的----不断循环获取同userid,----doing,error,todo状态任务
|
|
|
|
|
|
# 关闭driver
|
|
|
+ pass
|
|
|
|
|
|
|
|
|
def cookie_acion(db, log_ad, cookie_canuse, user_id):
|
|
@@ -55,6 +57,23 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
action_record_table = Table('action_record', db.metadata,
|
|
|
autoload=True, autoload_with=db.engine)
|
|
|
sql_session = db.DBSession()
|
|
|
+ task_name = 'user_id: {user_id} time:{time_sign} action:create_plan'.format(user_id=user_id,
|
|
|
+ time_sign=datetime.now().strftime(
|
|
|
+ "%Y-%m-%d, %H:%M:%S"))
|
|
|
+ # 2.2存行为记录
|
|
|
+ for _ in ad_plan_list:
|
|
|
+ print(_)
|
|
|
+ for action_type in ['create_ad_plan', 'create_ad_layout']:
|
|
|
+ object_name = _['title'] if action_type == 'create_ad_plan' else _['idea']['jump_type_page_type'][
|
|
|
+ 'layout_name']
|
|
|
+ object_name = object_name.replace(' ', '')[:29]
|
|
|
+ action_info = {'user_id': user_id, 'service_name': _['service_name'], 'wechat_name': _['wechat_name'],
|
|
|
+ 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
|
|
|
+ 'status': 'todo'}
|
|
|
+ record_insert = sql_tools.save_action_record(action_record_info=action_info,
|
|
|
+ table_action_record=action_record_table)
|
|
|
+ sql_session.execute(record_insert)
|
|
|
+ sql_session.commit()
|
|
|
|
|
|
# cookies保存
|
|
|
cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
@@ -70,19 +89,19 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
|
|
|
sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
|
|
|
action_record_table,
|
|
|
- service_name, wechat_name)
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
|
|
|
else:
|
|
|
# 1.5无则创建落地页
|
|
|
create_ad_layout = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name)
|
|
|
- create_ad_layout.get_into_create_page()
|
|
|
layout_typesetting_dict = sql_tools.get_layout_typesetting(sql_session, user_id,
|
|
|
typesetting_name=layout_name)
|
|
|
+ print(layout_typesetting_dict)
|
|
|
layout_typesetting_dict = json.loads(layout_typesetting_dict)
|
|
|
res = create_ad_layout.create_layout(layout_typesetting_dict)
|
|
|
sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
|
|
|
action_record_table,
|
|
|
- service_name, wechat_name)
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
log_ad.refresh_driver()
|
|
|
# 3.创建计划
|
|
|
log_ad.select_ad_master(service_name, wechat_name)
|
|
@@ -93,12 +112,13 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
res = create_ad_plan.run()
|
|
|
# 4.更新action_record相关计划信息
|
|
|
sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name, action_record_table,
|
|
|
- service_name, wechat_name)
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
log_ad.refresh_driver()
|
|
|
# 每次运行微信相关操作,对微信相关信息进行刷新
|
|
|
get_human_info(user_id, log_ad, db, cookie_canuse)
|
|
|
except Exception as e:
|
|
|
- #TODO:直接进行截图
|
|
|
+ log_ad.driver.save_screenshot(
|
|
|
+ 'user_id:{}_time_{}_plan_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
|
|
|
dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
|
|
|
logging.error(e)
|
|
|
finally:
|
|
@@ -111,31 +131,69 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
|
|
|
def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
try:
|
|
|
- #TODO: 添加action_record对应数据记录
|
|
|
-
|
|
|
# 数据库
|
|
|
+ action_record_table = Table('action_record', db.metadata,
|
|
|
+ autoload=True, autoload_with=db.engine)
|
|
|
human_info_table = Table('human_info', db.metadata,
|
|
|
autoload=True, autoload_with=db.engine)
|
|
|
wechat_info_table = Table('wechat_info', db.metadata,
|
|
|
autoload=True, autoload_with=db.engine)
|
|
|
sql_session = db.DBSession()
|
|
|
|
|
|
+ # 行为记录
|
|
|
+ task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(user_id=user_id,
|
|
|
+ time_sign=datetime.now().strftime(
|
|
|
+ "%Y-%m-%d, %H:%M:%S"))
|
|
|
+
|
|
|
+ action_type = 'refresh_wechat_info'
|
|
|
+ action_info = {'user_id': user_id, 'service_name': '', 'wechat_name': '',
|
|
|
+ 'action_type': action_type, 'object_name': '', 'task_name': task_name,
|
|
|
+ 'status': 'todo'}
|
|
|
+ record_insert = sql_tools.save_action_record(action_record_info=action_info,
|
|
|
+ table_action_record=action_record_table)
|
|
|
+ sql_session.execute(record_insert)
|
|
|
+ sql_session.commit()
|
|
|
+
|
|
|
# 1.cookies保存
|
|
|
cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
- # 2.刷新人群包
|
|
|
-
|
|
|
- # 3.刷新微信 公众号 分层 信息
|
|
|
|
|
|
# wechat_info.每次都删除掉前面全部数据,进行更新
|
|
|
# human_info 进行全局更新
|
|
|
w_api = WechatApi(log_ad=log_ad)
|
|
|
- res_list = w_api.get_human_info()
|
|
|
- if res_list is None:
|
|
|
+ res_info = w_api.get_human_info()
|
|
|
+
|
|
|
+ if not res_info['sucess']:
|
|
|
dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
|
|
|
log_ad.driver.close()
|
|
|
+ object_name = ''
|
|
|
+ service_name = ''
|
|
|
+ wechat_name = ''
|
|
|
+ sql_tools.action_record(res_info, sql_session, refresh_wechat_action, user_id,
|
|
|
+ object_name, action_record_table,
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
return
|
|
|
+
|
|
|
+ # wechat info进行数据更新
|
|
|
+ # 1.删除所有数据
|
|
|
+ delete_wechat_info(sql_session=sql_session, user_id=user_id)
|
|
|
+ # 2.重新添加一遍相关数据
|
|
|
+ for _ in res_info['result_list']:
|
|
|
+ service_name = _['service_name']
|
|
|
+ wechat_name = _['wechat_name']
|
|
|
+ appid = _['appid']
|
|
|
+ wxname = _['wxname']
|
|
|
+ wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name,
|
|
|
+ 'wechat_name': wechat_name,
|
|
|
+ 'user_id': user_id}
|
|
|
+ wechat_insert = save_wechat_info(wechat_info=wechat_info,
|
|
|
+ table_wechat=wechat_info_table)
|
|
|
+
|
|
|
+ sql_session.execute(wechat_insert)
|
|
|
+ sql_session.commit()
|
|
|
+ logging.info('update wechat info')
|
|
|
+
|
|
|
# human info 相关数据进行更新
|
|
|
- for _ in res_list:
|
|
|
+ for _ in res_info['result_list']:
|
|
|
service_name = _['service_name']
|
|
|
wechat_name = _['wechat_name']
|
|
|
human_info = _['data']['list']
|
|
@@ -155,26 +213,17 @@ def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
sql_session.commit()
|
|
|
print('update human info')
|
|
|
|
|
|
- # wechat info进行数据更新
|
|
|
- # 1.删除所有数据
|
|
|
- delete_wechat_info(sql_session=sql_session, user_id=user_id)
|
|
|
- # 2.重新添加一遍相关数据
|
|
|
- for _ in res_list:
|
|
|
- service_name = _['service_name']
|
|
|
- wechat_name = _['wechat_name']
|
|
|
- appid = _['appid']
|
|
|
- wxname = _['wxname']
|
|
|
- wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name, 'wechat_name': wechat_name,
|
|
|
- 'user_id': user_id}
|
|
|
- wechat_insert = save_wechat_info(wechat_info=wechat_info,
|
|
|
- table_wechat=wechat_info_table)
|
|
|
-
|
|
|
- sql_session.execute(wechat_insert)
|
|
|
- sql_session.commit()
|
|
|
- print('update wechat info')
|
|
|
# 浏览器关闭
|
|
|
log_ad.driver.close()
|
|
|
+ object_name = ''
|
|
|
+ service_name = ''
|
|
|
+ wechat_name = ''
|
|
|
+ sql_tools.action_record(res_info, sql_session, refresh_wechat_action,
|
|
|
+ user_id, object_name, action_record_table,
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
except Exception as e:
|
|
|
+ log_ad.driver.save_screenshot(
|
|
|
+ 'user_id:{}_time_{}_wechat_info_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
|
|
|
dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
|
|
|
logging.error(e)
|
|
|
finally:
|