user_action.py 11 KB

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