|
@@ -8,123 +8,96 @@ from communication_tools import dingtalk
|
|
|
import json
|
|
|
import logging
|
|
|
from datetime import datetime
|
|
|
+from wechat_action.login_ad import LogIn
|
|
|
|
|
|
+# 定位为:接受请求之后对应的线程处理
|
|
|
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()
|
|
|
- # 无: 任务添加到数据库中doing
|
|
|
-
|
|
|
- # 检查是否需要登录
|
|
|
- # 任务开始
|
|
|
- # 任务中间添加的----不断循环获取同userid,----doing,error,todo状态任务
|
|
|
-
|
|
|
- # 关闭driver
|
|
|
- pass
|
|
|
-
|
|
|
-
|
|
|
-def cookie_acion(db, log_ad, cookie_canuse, user_id):
|
|
|
- wechat_cookies_table = Table('wechat_cookies', db.metadata,
|
|
|
- autoload=True, autoload_with=db.engine)
|
|
|
- sql_session = db.DBSession()
|
|
|
- # 等待页面加载完成
|
|
|
- log_ad.log_in_wait()
|
|
|
- # 1.保存cookie,
|
|
|
- if not cookie_canuse:
|
|
|
- log_ad.cookies_save(log_ad)
|
|
|
- print('update wechat cookies')
|
|
|
-
|
|
|
-
|
|
|
-def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
- # TODO:与get_human_info的交互需要改动
|
|
|
+def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
|
|
|
# TODO:失败后 ,状态为:创建中 ----进行对应删除
|
|
|
|
|
|
# 创建中的数据需要进行删除
|
|
|
-
|
|
|
- # TODO:
|
|
|
- # 1.任务有不断中途新增,该如何操作.------只管
|
|
|
- # 2.落地页创建一半失败了怎么办
|
|
|
+ sql_session = db.DBSession()
|
|
|
|
|
|
try:
|
|
|
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
|
|
|
- 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)
|
|
|
-
|
|
|
+ if not cookie_canuse:
|
|
|
+ log_ad.cookies_save(log_ad)
|
|
|
for _ in ad_plan_list:
|
|
|
service_name = _['service_name']
|
|
|
wechat_name = _['wechat_name']
|
|
|
- # 1.检查1.落地页是否创建过了
|
|
|
- log_ad.select_ad_master(service_name, wechat_name)
|
|
|
- # 现在默认layout_name在30个字符以内
|
|
|
- layout_name = _['idea']['jump_type_page_type']['layout_name']
|
|
|
- if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
|
|
|
- res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
|
|
|
- sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
|
|
|
+ plan_name = _['title']
|
|
|
+ try:
|
|
|
+ # 1.检查1.落地页是否创建过了
|
|
|
+ log_ad.select_ad_master(service_name, wechat_name)
|
|
|
+ # 现在默认layout_name在30个字符以内
|
|
|
+ layout_name = _['idea']['jump_type_page_type']['layout_name']
|
|
|
+ if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
|
|
|
+ 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, task_name)
|
|
|
+
|
|
|
+ else:
|
|
|
+ # 1.5无则创建落地页
|
|
|
+ try:
|
|
|
+ create_ad_layout = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name)
|
|
|
+ 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, task_name)
|
|
|
+ except Exception as e:
|
|
|
+ res = {'sucess': False, 'result_info': str(e)}
|
|
|
+ sql_tools.action_record(res, sql_session, layout_create_action, user_id, plan_name,
|
|
|
+ action_record_table,
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
+ continue
|
|
|
+ log_ad.refresh_driver()
|
|
|
+ # 3.创建计划
|
|
|
+ log_ad.select_ad_master(service_name, wechat_name)
|
|
|
+ # plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
|
|
|
+ # typesetting_name=plan_name)
|
|
|
+ create_ad_plan = CreateAdPlan(login_ad=log_ad, task=_, service_name=service_name,
|
|
|
+ wechat_name=wechat_name)
|
|
|
+ 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, task_name)
|
|
|
-
|
|
|
- else:
|
|
|
- # 1.5无则创建落地页
|
|
|
- create_ad_layout = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name)
|
|
|
- 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,
|
|
|
+ logging.info('创建计划任务结束')
|
|
|
+ log_ad.refresh_driver()
|
|
|
+ except Exception as e:
|
|
|
+ log_ad.driver.save_screenshot(
|
|
|
+ 'user_id:{}_time_{}_plan_name:{}_plan_error.png'.format(user_id, datetime.now().strftime(
|
|
|
+ "%Y-%m-%d, %H:%M:%S"), plan_name))
|
|
|
+ logging.error(str(e))
|
|
|
+ log_ad.refresh_driver()
|
|
|
+ res = {'sucess': False, 'result_info': str(e)}
|
|
|
+ sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name,
|
|
|
action_record_table,
|
|
|
service_name, wechat_name, task_name)
|
|
|
- log_ad.refresh_driver()
|
|
|
- # 3.创建计划
|
|
|
- log_ad.select_ad_master(service_name, wechat_name)
|
|
|
- plan_name = _['title']
|
|
|
- # plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
|
|
|
- # typesetting_name=plan_name)
|
|
|
- create_ad_plan = CreateAdPlan(login_ad=log_ad, task=_, service_name=service_name, wechat_name=wechat_name)
|
|
|
- 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, task_name)
|
|
|
- logging.info('创建计划任务结束')
|
|
|
- log_ad.refresh_driver()
|
|
|
- # 每次运行微信相关操作,对微信相关信息进行刷新
|
|
|
- get_human_info(user_id, log_ad, db, cookie_canuse)
|
|
|
+ raise
|
|
|
+
|
|
|
+ # 每次运行微信相关操作,对微信相关信息进行刷新
|
|
|
+ get_human_info(user_id, log_ad, db, cookie_canuse, task_name)
|
|
|
+
|
|
|
+ check_task_in_hand(user_id, db, log_ad)
|
|
|
+
|
|
|
except Exception as e:
|
|
|
- log_ad.driver.save_screenshot(
|
|
|
- 'user_id:{}_time_{}_plan_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
|
|
|
- raise
|
|
|
+ sql_tools.update_task_status_error(sql_session=sql_session, user_id=user_id, task_name=task_name)
|
|
|
dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
|
|
|
logging.error(e)
|
|
|
+ raise
|
|
|
finally:
|
|
|
try:
|
|
|
print('任务结束')
|
|
@@ -133,33 +106,24 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
pass
|
|
|
|
|
|
|
|
|
-def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
- try:
|
|
|
- # 数据库
|
|
|
- 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()
|
|
|
+def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
|
|
|
+ # 数据库
|
|
|
+ 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()
|
|
|
|
|
|
+ # 行为记录对应参数
|
|
|
+ object_name = ''
|
|
|
+ service_name = ''
|
|
|
+ wechat_name = ''
|
|
|
+ try:
|
|
|
# 1.cookies保存
|
|
|
- cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
+ if not cookie_canuse:
|
|
|
+ log_ad.cookies_save(log_ad)
|
|
|
|
|
|
# wechat_info.每次都删除掉前面全部数据,进行更新
|
|
|
# human_info 进行全局更新
|
|
@@ -169,9 +133,6 @@ def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
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)
|
|
@@ -215,21 +176,26 @@ def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
|
|
|
sql_session.execute(human_insert)
|
|
|
sql_session.commit()
|
|
|
- print('update human info')
|
|
|
+ logging.info('update human 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)
|
|
|
+ check_task_in_hand(user_id=user_id, db=db, log_ad=log_ad)
|
|
|
except Exception as e:
|
|
|
+ res_info = {'sucess': False, 'result_info': str(e)}
|
|
|
+ sql_tools.action_record(res_info, sql_session, refresh_wechat_action,
|
|
|
+ user_id, object_name, action_record_table,
|
|
|
+ service_name, wechat_name, task_name)
|
|
|
+
|
|
|
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)
|
|
|
+ raise
|
|
|
finally:
|
|
|
try:
|
|
|
print('任务结束')
|
|
@@ -238,5 +204,26 @@ def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
pass
|
|
|
|
|
|
|
|
|
+def check_task_in_hand(user_id, db, log_ad):
|
|
|
+ sql_session = db.DBSession()
|
|
|
+ lines = sql_tools.get_task_in_hand_limit_one(user_id=user_id, sql_session=sql_session)
|
|
|
+
|
|
|
+ task_name = ''
|
|
|
+ ad_plan_list = []
|
|
|
+ action_type = ''
|
|
|
+ for _ in lines:
|
|
|
+ typesetting, wechat_name, service_name, action_type, task_name = _
|
|
|
+ ad_plan_list.append(json.loads(typesetting))
|
|
|
+ if action_type == refresh_wechat_action:
|
|
|
+ # 刷新log_ad,以免log_ad超过生命周期
|
|
|
+ log_ad.driver.quit()
|
|
|
+ log_ad = LogIn(user_id=user_id)
|
|
|
+ get_human_info(user_id, log_ad, db, True, task_name)
|
|
|
+ if action_type == ad_plan_create_action:
|
|
|
+ log_ad.driver.quit()
|
|
|
+ log_ad = LogIn(user_id=user_id)
|
|
|
+ carry_plan(user_id, ad_plan_list, log_ad, db, True, task_name)
|
|
|
+
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
pass
|