|
@@ -4,7 +4,9 @@ from wechat_action.create_ad_layout import CreateAd
|
|
|
from wechat_action.create_ad_plan import CreateAdPlan
|
|
|
from wechat_action import sql_tools
|
|
|
from sqlalchemy import Table
|
|
|
+from communication_tools import dingtalk
|
|
|
import json
|
|
|
+import logging
|
|
|
import time
|
|
|
|
|
|
layout_create_action = 'create_ad_layout'
|
|
@@ -37,39 +39,32 @@ def cookie_acion(db, log_ad, cookie_canuse, user_id):
|
|
|
log_ad.log_in_wait()
|
|
|
# 1.保存cookie,
|
|
|
if not cookie_canuse:
|
|
|
- wechat_cookies = log_ad.wechat_cookie_pickle()
|
|
|
-
|
|
|
- update_res = wechat_cookies_table.update() \
|
|
|
- .where(wechat_cookies_table.c.user_id == user_id) \
|
|
|
- .values(cookies=wechat_cookies)
|
|
|
- update_res = sql_session.execute(update_res)
|
|
|
- sql_session.commit()
|
|
|
- if update_res.rowcount == 0:
|
|
|
- wechat_cookies_info = {'user_id': user_id, 'cookies': wechat_cookies}
|
|
|
- wechat_insert = save_wechat_cookies(wechat_cookies_info=wechat_cookies_info,
|
|
|
- table_wechat_cookies=wechat_cookies_table)
|
|
|
-
|
|
|
- sql_session.execute(wechat_insert)
|
|
|
- sql_session.commit()
|
|
|
+ log_ad.cookies_save(log_ad)
|
|
|
print('update wechat cookies')
|
|
|
|
|
|
|
|
|
def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
# TODO:action_record涉及到的地方都需要改动
|
|
|
+ # 创建中的数据需要进行删除
|
|
|
|
|
|
- action_record_table = Table('action_record', db.metadata,
|
|
|
- autoload=True, autoload_with=db.engine)
|
|
|
- sql_session = db.DBSession()
|
|
|
+ # TODO:
|
|
|
+ # 1.任务有不断中途新增,该如何操作.------只管
|
|
|
+ # 2.落地页创建一半失败了怎么办
|
|
|
|
|
|
- # cookies保存
|
|
|
- cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
try:
|
|
|
+ action_record_table = Table('action_record', db.metadata,
|
|
|
+ autoload=True, autoload_with=db.engine)
|
|
|
+ sql_session = db.DBSession()
|
|
|
+
|
|
|
+ # cookies保存
|
|
|
+ cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
+
|
|
|
for _ in ad_plan_list:
|
|
|
service_name = _['service_name']
|
|
|
wechat_name = _['wechat_name']
|
|
|
# 1.检查1.落地页是否创建过了
|
|
|
log_ad.select_ad_master(service_name, wechat_name)
|
|
|
- # TODO:现在默认layout_name在30个字符以内
|
|
|
+ # 现在默认layout_name在30个字符以内
|
|
|
layout_name = _['idea']['jump_type_page_type']['layout_name'].replace(' ', '')[:29]
|
|
|
if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
|
|
|
res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
|
|
@@ -90,7 +85,6 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
service_name, wechat_name)
|
|
|
log_ad.refresh_driver()
|
|
|
# 3.创建计划
|
|
|
- # TODO:添加计划是否创建检查,现默认计划没有重复
|
|
|
log_ad.select_ad_master(service_name, wechat_name)
|
|
|
plan_name = _['title'].replace(' ', '')[:29]
|
|
|
# plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
|
|
@@ -101,9 +95,12 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name, action_record_table,
|
|
|
service_name, wechat_name)
|
|
|
log_ad.refresh_driver()
|
|
|
- # TODO:添加final ,来关闭chrome
|
|
|
+ # 每次运行微信相关操作,对微信相关信息进行刷新
|
|
|
+ get_human_info(user_id, log_ad, db, cookie_canuse)
|
|
|
except Exception as e:
|
|
|
- print(e)
|
|
|
+ #TODO:直接进行截图
|
|
|
+ dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
|
|
|
+ logging.error(e)
|
|
|
finally:
|
|
|
try:
|
|
|
print('任务结束')
|
|
@@ -112,191 +109,80 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
|
|
|
pass
|
|
|
|
|
|
|
|
|
-# TODO:这里都是线程调度的函数,设定线程生命周期最长60分钟
|
|
|
-
|
|
|
def get_human_info(user_id, log_ad, db, cookie_canuse):
|
|
|
- # 数据库
|
|
|
- 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()
|
|
|
-
|
|
|
- # TODO:log_ad 在这个线程结束之后并没有自动关闭,需要设置一下log_ad这个类删除,
|
|
|
- # 然后看一下多次请求之后,线程数量,
|
|
|
-
|
|
|
- # TODO:需要添加公众号信息
|
|
|
-
|
|
|
- # 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()
|
|
|
-
|
|
|
- # human info 相关数据进行更新
|
|
|
- for _ in res_list:
|
|
|
- service_name = _['service_name']
|
|
|
- wechat_name = _['wechat_name']
|
|
|
- human_info = _['data']['list']
|
|
|
+ try:
|
|
|
+ #TODO: 添加action_record对应数据记录
|
|
|
|
|
|
- update_res = human_info_table.update() \
|
|
|
- .where(human_info_table.c.service_name == service_name) \
|
|
|
- .where(human_info_table.c.wechat_name == wechat_name) \
|
|
|
- .values(human_info=human_info)
|
|
|
- update_res = sql_session.execute(update_res)
|
|
|
- sql_session.commit()
|
|
|
- if update_res.rowcount == 0:
|
|
|
- human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
|
|
|
- human_insert = save_human_info(human_info=human_info,
|
|
|
- table_human=human_info_table)
|
|
|
+ # 数据库
|
|
|
+ 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()
|
|
|
+
|
|
|
+ # 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:
|
|
|
+ dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
|
|
|
+ log_ad.driver.close()
|
|
|
+ return
|
|
|
+ # human info 相关数据进行更新
|
|
|
+ for _ in res_list:
|
|
|
+ service_name = _['service_name']
|
|
|
+ wechat_name = _['wechat_name']
|
|
|
+ human_info = _['data']['list']
|
|
|
|
|
|
- sql_session.execute(human_insert)
|
|
|
+ update_res = human_info_table.update() \
|
|
|
+ .where(human_info_table.c.service_name == service_name) \
|
|
|
+ .where(human_info_table.c.wechat_name == wechat_name) \
|
|
|
+ .values(human_info=human_info)
|
|
|
+ update_res = sql_session.execute(update_res)
|
|
|
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']
|
|
|
- wechat_info = {'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()
|
|
|
-
|
|
|
-
|
|
|
-def create_layout(user_id, layout_name, wechat_json, log_ad, db, cookie_canuse):
|
|
|
- # TODO:
|
|
|
- # 1.任务有不断中途新增,该如何操作.------只管
|
|
|
- # 2.落地页创建一半失败了怎么办
|
|
|
- # 4.cookie记录每次刷新都会失效,需要改进
|
|
|
-
|
|
|
- action_record_table = Table('action_record', db.metadata,
|
|
|
- autoload=True, autoload_with=db.engine)
|
|
|
- sql_session = db.DBSession()
|
|
|
- # 等待页面加载完成
|
|
|
-
|
|
|
- # cookies保存
|
|
|
- cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
-
|
|
|
- # 获取到对应layout
|
|
|
- typesetting_json = sql_tools.get_layout_typesetting(sql_session=sql_session, user_id=user_id,
|
|
|
- typesetting_name=layout_name)
|
|
|
- if not typesetting_json:
|
|
|
- return
|
|
|
- print(typesetting_json)
|
|
|
- typesetting_dict = json.loads(typesetting_json)
|
|
|
- typesetting_dict[-4] = {'layout_name': layout_name}
|
|
|
- # action 进行对应记录
|
|
|
- wechat_json = json.loads(wechat_json)
|
|
|
-
|
|
|
- for _ in wechat_json:
|
|
|
- service_name = _['service_name']
|
|
|
- wechat_name = _['wechat_name']
|
|
|
- # 检查落地页之前有无创建过
|
|
|
- layout_num = sql_tools.check_layout_alive(sql_session, service_name, wechat_name, layout_name)
|
|
|
- if layout_num:
|
|
|
- continue
|
|
|
- log_ad.select_ad_master(service_name, wechat_name)
|
|
|
- res = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name).create_layout(
|
|
|
- typesetting_dict)
|
|
|
- print(res)
|
|
|
- if not res['sucess']:
|
|
|
- now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
|
|
- try:
|
|
|
- log_ad.driver.save_screenshot(
|
|
|
- 'create_ad_layout-{layout_name}-{now_time}-{service_name}-{wechat_name}.png'.format(
|
|
|
- layout_name=layout_name,
|
|
|
- now_time=now_time,
|
|
|
- service_name=service_name,
|
|
|
- wechat_name=wechat_name))
|
|
|
- except:
|
|
|
- pass
|
|
|
- # TODO:截图,传回错误信息
|
|
|
- action_type = layout_create_action
|
|
|
- # TODO:进行对应数据修改
|
|
|
- sql_tools.action_record(res, sql_session, action_type, user_id, layout_name, action_record_table, service_name,
|
|
|
- wechat_name)
|
|
|
- log_ad.refresh_driver()
|
|
|
-
|
|
|
- # 成功一个record,更新一个record
|
|
|
- log_ad.driver.quit()
|
|
|
-
|
|
|
-
|
|
|
-def create_ad_plan(user_id, ad_plan_name, wechat_json, log_ad, db, cookie_canuse):
|
|
|
- # TODO:
|
|
|
- # 1.任务有不断中途新增,该如何操作.------只管
|
|
|
- # 2.落地页创建一半失败了怎么办
|
|
|
- # 4.cookie记录每次刷新都会失效,需要改进
|
|
|
-
|
|
|
- action_record_table = Table('action_record', db.metadata,
|
|
|
- autoload=True, autoload_with=db.engine)
|
|
|
- sql_session = db.DBSession()
|
|
|
- action_type = ad_plan_create_action
|
|
|
- # 等待页面加载完成
|
|
|
-
|
|
|
- # 1.cookies保存
|
|
|
- cookie_acion(db, log_ad, cookie_canuse, user_id)
|
|
|
-
|
|
|
- # 获取到对应layout
|
|
|
- typesetting_json = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
|
|
|
- typesetting_name=ad_plan_name)
|
|
|
- if not typesetting_json:
|
|
|
- return
|
|
|
- print(typesetting_json)
|
|
|
- typesetting_dict = json.loads(typesetting_json)
|
|
|
- typesetting_dict['title'] = ad_plan_name
|
|
|
- # action 进行对应记录
|
|
|
- wechat_json = json.loads(wechat_json)
|
|
|
-
|
|
|
- for _ in wechat_json:
|
|
|
- service_name = _['service_name']
|
|
|
- wechat_name = _['wechat_name']
|
|
|
- # 检查这个名字是否已经创建过
|
|
|
- layout_name = typesetting_dict['idea']['jump_type-page_type']
|
|
|
- if not sql_tools.check_layout_alive(sql_session, service_name, wechat_name, layout_name):
|
|
|
- res = {'sucess': False, 'result_info': '公众号对应落地页未创建'}
|
|
|
- sql_tools.action_record(res, sql_session, action_type, user_id, ad_plan_name, action_record_table,
|
|
|
- service_name,
|
|
|
- wechat_name)
|
|
|
- continue
|
|
|
- if sql_tools.check_plan_alive(sql_session, service_name, wechat_name, ad_plan_name):
|
|
|
- continue
|
|
|
- log_ad.select_ad_master(service_name, wechat_name)
|
|
|
- res = CreateAdPlan(login_ad=log_ad, task=typesetting_dict, service_name=service_name,
|
|
|
- wechat_name=wechat_name).run()
|
|
|
- print(res)
|
|
|
- if not res['sucess']:
|
|
|
- now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
|
|
- try:
|
|
|
- log_ad.driver.save_screenshot(
|
|
|
- 'create_ad_plan-{plan_name}-{now_time}-{service_name}-{wechat_name}.png'.format(
|
|
|
- plan_name=ad_plan_name,
|
|
|
- now_time=now_time,
|
|
|
- service_name=service_name,
|
|
|
- wechat_name=wechat_name))
|
|
|
- except:
|
|
|
- pass
|
|
|
- # 截图,传回错误信息
|
|
|
-
|
|
|
- sql_tools.action_record(res, sql_session, action_type, user_id, ad_plan_name, action_record_table, service_name,
|
|
|
- wechat_name)
|
|
|
- log_ad.refresh_driver()
|
|
|
+ if update_res.rowcount == 0:
|
|
|
+ human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
|
|
|
+ human_insert = save_human_info(human_info=human_info,
|
|
|
+ table_human=human_info_table)
|
|
|
+
|
|
|
+ sql_session.execute(human_insert)
|
|
|
+ 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)
|
|
|
|
|
|
- # 成功一个record,更新一个record
|
|
|
- log_ad.driver.quit()
|
|
|
+ sql_session.execute(wechat_insert)
|
|
|
+ sql_session.commit()
|
|
|
+ print('update wechat info')
|
|
|
+ # 浏览器关闭
|
|
|
+ log_ad.driver.close()
|
|
|
+ except Exception as e:
|
|
|
+ dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
|
|
|
+ logging.error(e)
|
|
|
+ finally:
|
|
|
+ try:
|
|
|
+ print('任务结束')
|
|
|
+ log_ad.driver.quit()
|
|
|
+ except:
|
|
|
+ pass
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|