user_action.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from wechat_action.sql_tools import save_wechat_cookies, save_human_info, delete_wechat_info, save_wechat_info
  2. from wechat_api.get_wechat_info import WechatApi
  3. from wechat_action.create_ad_layout import CreateAd
  4. from wechat_action.create_ad_plan import CreateAdPlan
  5. from wechat_action import sql_tools
  6. from sqlalchemy import Table
  7. from communication_tools import dingtalk
  8. import json
  9. import logging
  10. import time
  11. layout_create_action = 'create_ad_layout'
  12. ad_plan_create_action = 'create_ad_plan'
  13. def run(user_id, log_ad, db, cookie_canuse):
  14. sql_session = db.DBSession()
  15. # 检查是否有已经开启的任务
  16. result = sql_tools.get_action_status(sql_session, user_id)
  17. # 有: 只将任务进行添加操作todo
  18. if result:
  19. action_info = {'user_id': user_id, 'service_name': ''}
  20. sql_tools.save_action_record()
  21. # 无: 任务添加到数据库中doing
  22. # 检查是否需要登录
  23. # 任务开始
  24. # 任务中间添加的----不断循环获取同userid,----doing,error,todo状态任务
  25. # 关闭driver
  26. def cookie_acion(db, log_ad, cookie_canuse, user_id):
  27. wechat_cookies_table = Table('wechat_cookies', db.metadata,
  28. autoload=True, autoload_with=db.engine)
  29. sql_session = db.DBSession()
  30. # 等待页面加载完成
  31. log_ad.log_in_wait()
  32. # 1.保存cookie,
  33. if not cookie_canuse:
  34. log_ad.cookies_save(log_ad)
  35. print('update wechat cookies')
  36. def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
  37. # TODO:action_record涉及到的地方都需要改动
  38. # 创建中的数据需要进行删除
  39. # TODO:
  40. # 1.任务有不断中途新增,该如何操作.------只管
  41. # 2.落地页创建一半失败了怎么办
  42. try:
  43. action_record_table = Table('action_record', db.metadata,
  44. autoload=True, autoload_with=db.engine)
  45. sql_session = db.DBSession()
  46. # cookies保存
  47. cookie_acion(db, log_ad, cookie_canuse, user_id)
  48. for _ in ad_plan_list:
  49. service_name = _['service_name']
  50. wechat_name = _['wechat_name']
  51. # 1.检查1.落地页是否创建过了
  52. log_ad.select_ad_master(service_name, wechat_name)
  53. # 现在默认layout_name在30个字符以内
  54. layout_name = _['idea']['jump_type_page_type']['layout_name'].replace(' ', '')[:29]
  55. if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
  56. res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
  57. sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
  58. action_record_table,
  59. service_name, wechat_name)
  60. else:
  61. # 1.5无则创建落地页
  62. create_ad_layout = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name)
  63. create_ad_layout.get_into_create_page()
  64. layout_typesetting_dict = sql_tools.get_layout_typesetting(sql_session, user_id,
  65. typesetting_name=layout_name)
  66. layout_typesetting_dict = json.loads(layout_typesetting_dict)
  67. res = create_ad_layout.create_layout(layout_typesetting_dict)
  68. sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
  69. action_record_table,
  70. service_name, wechat_name)
  71. log_ad.refresh_driver()
  72. # 3.创建计划
  73. log_ad.select_ad_master(service_name, wechat_name)
  74. plan_name = _['title'].replace(' ', '')[:29]
  75. # plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
  76. # typesetting_name=plan_name)
  77. create_ad_plan = CreateAdPlan(login_ad=log_ad, task=_, service_name=service_name, wechat_name=wechat_name)
  78. res = create_ad_plan.run()
  79. # 4.更新action_record相关计划信息
  80. sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name, action_record_table,
  81. service_name, wechat_name)
  82. log_ad.refresh_driver()
  83. # 每次运行微信相关操作,对微信相关信息进行刷新
  84. get_human_info(user_id, log_ad, db, cookie_canuse)
  85. except Exception as e:
  86. #TODO:直接进行截图
  87. dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
  88. logging.error(e)
  89. finally:
  90. try:
  91. print('任务结束')
  92. log_ad.driver.quit()
  93. except:
  94. pass
  95. def get_human_info(user_id, log_ad, db, cookie_canuse):
  96. try:
  97. #TODO: 添加action_record对应数据记录
  98. # 数据库
  99. human_info_table = Table('human_info', db.metadata,
  100. autoload=True, autoload_with=db.engine)
  101. wechat_info_table = Table('wechat_info', db.metadata,
  102. autoload=True, autoload_with=db.engine)
  103. sql_session = db.DBSession()
  104. # 1.cookies保存
  105. cookie_acion(db, log_ad, cookie_canuse, user_id)
  106. # 2.刷新人群包
  107. # 3.刷新微信 公众号 分层 信息
  108. # wechat_info.每次都删除掉前面全部数据,进行更新
  109. # human_info 进行全局更新
  110. w_api = WechatApi(log_ad=log_ad)
  111. res_list = w_api.get_human_info()
  112. if res_list is None:
  113. dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
  114. log_ad.driver.close()
  115. return
  116. # human info 相关数据进行更新
  117. for _ in res_list:
  118. service_name = _['service_name']
  119. wechat_name = _['wechat_name']
  120. human_info = _['data']['list']
  121. update_res = human_info_table.update() \
  122. .where(human_info_table.c.service_name == service_name) \
  123. .where(human_info_table.c.wechat_name == wechat_name) \
  124. .values(human_info=human_info)
  125. update_res = sql_session.execute(update_res)
  126. sql_session.commit()
  127. if update_res.rowcount == 0:
  128. human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
  129. human_insert = save_human_info(human_info=human_info,
  130. table_human=human_info_table)
  131. sql_session.execute(human_insert)
  132. sql_session.commit()
  133. print('update human info')
  134. # wechat info进行数据更新
  135. # 1.删除所有数据
  136. delete_wechat_info(sql_session=sql_session, user_id=user_id)
  137. # 2.重新添加一遍相关数据
  138. for _ in res_list:
  139. service_name = _['service_name']
  140. wechat_name = _['wechat_name']
  141. appid = _['appid']
  142. wxname = _['wxname']
  143. wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name, 'wechat_name': wechat_name,
  144. 'user_id': user_id}
  145. wechat_insert = save_wechat_info(wechat_info=wechat_info,
  146. table_wechat=wechat_info_table)
  147. sql_session.execute(wechat_insert)
  148. sql_session.commit()
  149. print('update wechat info')
  150. # 浏览器关闭
  151. log_ad.driver.close()
  152. except Exception as e:
  153. dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
  154. logging.error(e)
  155. finally:
  156. try:
  157. print('任务结束')
  158. log_ad.driver.quit()
  159. except:
  160. pass
  161. if __name__ == "__main__":
  162. pass