123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- from wechat_action.sql_tools import save_wechat_cookies, 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
- from wechat_action import sql_tools
- from sqlalchemy import Table
- import json
- import time
- layout_create_action = 'layout_create'
- ad_plan_create_action = 'ad_plan_create'
- 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
- 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:
- 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()
- print('update wechat cookies')
- def carry_plan():
- #TODO:action_record涉及到的地方都需要改动
- 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']
- 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)
- 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']
- 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()
- # 等待页面加载完成
- # 1.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:
- action_info = {'user_id': user_id, 'service_name': _['service_name'], 'wechat_name': _['wechat_name'],
- 'action_type': json.dumps({'action_type': layout_create_action, 'object_name': layout_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()
- 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
- 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:
- # TODO:有时间看看 create_layout与create_ad_plan是否需要合并
- # TODO:action_type 名字设置
- action_info = {'user_id': user_id, 'service_name': _['service_name'], 'wechat_name': _['wechat_name'],
- 'action_type': json.dumps({'action_type': 'ad_plan_create', 'object_name': ad_plan_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()
- 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).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()
- # 成功一个record,更新一个record
- log_ad.driver.quit()
- if __name__ == "__main__":
- pass
|