user_action.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 import CreateAd
  4. from wechat_action import sql_tools
  5. from sqlalchemy import Table
  6. import json
  7. import time
  8. def run(user_id, log_ad, db, cookie_canuse):
  9. sql_session = db.DBSession()
  10. # 检查是否有已经开启的任务
  11. result = sql_tools.get_action_status(sql_session, user_id)
  12. # 有: 只将任务进行添加操作todo
  13. if result:
  14. action_info = {'user_id': user_id, 'service_name': ''}
  15. sql_tools.save_action_record()
  16. # 无: 任务添加到数据库中doing
  17. # 检查是否需要登录
  18. # 任务开始
  19. # 任务中间添加的----不断循环获取同userid,----doing,error,todo状态任务
  20. # 关闭driver
  21. def cookie_acion(db, log_ad, cookie_canuse, user_id):
  22. wechat_cookies_table = Table('wechat_cookies', db.metadata,
  23. autoload=True, autoload_with=db.engine)
  24. sql_session = db.DBSession()
  25. # 等待页面加载完成
  26. log_ad.log_in_wait()
  27. # 1.保存cookie,
  28. if not cookie_canuse:
  29. wechat_cookies = log_ad.wechat_cookie_pickle()
  30. update_res = wechat_cookies_table.update() \
  31. .where(wechat_cookies_table.c.user_id == user_id) \
  32. .values(cookies=wechat_cookies)
  33. update_res = sql_session.execute(update_res)
  34. sql_session.commit()
  35. if update_res.rowcount == 0:
  36. wechat_cookies_info = {'user_id': user_id, 'cookies': wechat_cookies}
  37. wechat_insert = save_wechat_cookies(wechat_cookies_info=wechat_cookies_info,
  38. table_wechat_cookies=wechat_cookies_table)
  39. sql_session.execute(wechat_insert)
  40. sql_session.commit()
  41. print('update wechat cookies')
  42. # TODO:这里都是线程调度的函数,设定线程生命周期最长60分钟
  43. def get_human_info(user_id, log_ad, db, cookie_canuse):
  44. # 数据库
  45. human_info_table = Table('human_info', db.metadata,
  46. autoload=True, autoload_with=db.engine)
  47. wechat_info_table = Table('wechat_info', db.metadata,
  48. autoload=True, autoload_with=db.engine)
  49. sql_session = db.DBSession()
  50. # TODO:log_ad 在这个线程结束之后并没有自动关闭,需要设置一下log_ad这个类删除,
  51. # 然后看一下多次请求之后,线程数量,
  52. # 1.cookies保存
  53. cookie_acion(db, log_ad, cookie_canuse, user_id)
  54. # 2.刷新人群包
  55. # 3.刷新微信 公众号 分层 信息
  56. # wechat_info.每次都删除掉前面全部数据,进行更新
  57. # human_info 进行全局更新
  58. w_api = WechatApi(log_ad=log_ad)
  59. res_list = w_api.get_human_info()
  60. # human info 相关数据进行更新
  61. for _ in res_list:
  62. service_name = _['service_name']
  63. wechat_name = _['wechat_name']
  64. human_info = _['data']['list']
  65. update_res = human_info_table.update() \
  66. .where(human_info_table.c.service_name == service_name) \
  67. .where(human_info_table.c.wechat_name == wechat_name) \
  68. .values(human_info=human_info)
  69. update_res = sql_session.execute(update_res)
  70. sql_session.commit()
  71. if update_res.rowcount == 0:
  72. human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
  73. human_insert = save_human_info(human_info=human_info,
  74. table_human=human_info_table)
  75. sql_session.execute(human_insert)
  76. sql_session.commit()
  77. print('update human info')
  78. # wechat info进行数据更新
  79. # 1.删除所有数据
  80. delete_wechat_info(sql_session=sql_session, user_id=user_id)
  81. # 2.重新添加一遍相关数据
  82. for _ in res_list:
  83. service_name = _['service_name']
  84. wechat_name = _['wechat_name']
  85. wechat_info = {'service_name': service_name, 'wechat_name': wechat_name, 'user_id': user_id}
  86. wechat_insert = save_wechat_info(wechat_info=wechat_info,
  87. table_wechat=wechat_info_table)
  88. sql_session.execute(wechat_insert)
  89. sql_session.commit()
  90. print('update wechat info')
  91. # 浏览器关闭
  92. log_ad.driver.close()
  93. def create_layout(user_id, layout_name, wechat_json, log_ad, db, cookie_canuse):
  94. # TODO:
  95. # 1.任务有不断中途新增,该如何操作.------只管
  96. # 2.落地页创建一半失败了怎么办
  97. # 4.cookie记录每次刷新都会失效,需要改进
  98. action_record_table = Table('action_record', db.metadata,
  99. autoload=True, autoload_with=db.engine)
  100. sql_session = db.DBSession()
  101. # 等待页面加载完成
  102. # 1.cookies保存
  103. cookie_acion(db, log_ad, cookie_canuse, user_id)
  104. # 获取到对应layout
  105. typesetting_json = sql_tools.get_layout_typesetting(sql_session=sql_session, user_id=user_id,
  106. typesetting_name=layout_name)
  107. if not typesetting_json:
  108. return
  109. print(typesetting_json)
  110. typesetting_dict = json.loads(typesetting_json)
  111. # action 进行对应记录
  112. wechat_json = json.loads(wechat_json)
  113. for _ in wechat_json:
  114. action_info = {'user_id': user_id, 'service_name': _['service_name'], 'wechat_name': _['wechat_name'],
  115. 'action_type': json.dumps({'action_type': 'layout_create', 'object_name': layout_name}),
  116. 'status': 'todo'}
  117. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  118. table_action_record=action_record_table)
  119. sql_session.execute(record_insert)
  120. sql_session.commit()
  121. for _ in wechat_json:
  122. service_name = _['service_name']
  123. wechat_name = _['wechat_name']
  124. log_ad.select_ad_master(service_name, wechat_name)
  125. res = CreateAd(login_ad=log_ad, user_id=user_id).create_layout(typesetting_dict)
  126. print(res)
  127. if not res['sucess']:
  128. now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  129. try:
  130. log_ad.driver.save_screenshot('{layout_name}-{now_time}-{service_name}-{wechat_name}.png'.format(
  131. layout_name=layout_name,
  132. now_time=now_time,
  133. service_name=service_name,
  134. wechat_name=wechat_name))
  135. except:
  136. pass
  137. # 截图,传回错误信息
  138. status = 'done' if res['sucess'] else 'error'
  139. action_type = json.dumps({'action_type': 'layout_create', 'object_name': layout_name})
  140. # TODO:线上落地页名字进行设置
  141. update_res = action_record_table.update() \
  142. .where(action_record_table.c.service_name == service_name) \
  143. .where(action_record_table.c.wechat_name == wechat_name) \
  144. .where(action_record_table.c.user_id == user_id) \
  145. .where(action_record_table.c.action_type == action_type) \
  146. .values({
  147. action_record_table.c.status: status,
  148. action_record_table.c.result: res['result_info']
  149. })
  150. # values 添加两个列
  151. sql_session.execute(update_res)
  152. sql_session.commit()
  153. log_ad.refresh_driver()
  154. # 成功一个record,更新一个record
  155. log_ad.refresh_driver()
  156. def create_ad_plan():
  157. pass
  158. if __name__ == "__main__":
  159. pass