tornado_api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. from wechat_action.sql_models import DB
  2. from settings import using_config
  3. import tornado.log
  4. import tornado.ioloop
  5. import tornado.web
  6. from logging import handlers
  7. from wechat_action.login_ad import LogIn
  8. from wechat_action import sql_tools
  9. import threading
  10. from web_module import user_action
  11. from sqlalchemy import Table
  12. import json
  13. import pickle
  14. from datetime import datetime
  15. # TODO:需要添加上supervisor,来维护进程
  16. # TODO:有时间需要对tornado进行改进
  17. # TODO:需要有一套上线工具,来维持线上稳定
  18. db = DB(config=using_config)
  19. wechat_cookies_table = Table('wechat_cookies', db.metadata,
  20. autoload=True, autoload_with=db.engine)
  21. layout_typesetting_table = Table('layout_typesetting', db.metadata,
  22. autoload=True, autoload_with=db.engine)
  23. ad_plan_typesetting_table = Table('ad_plan_typesetting', db.metadata,
  24. autoload=True, autoload_with=db.engine)
  25. action_record_table = Table('action_record', db.metadata,
  26. autoload=True, autoload_with=db.engine)
  27. layout_create_action = 'create_ad_layout'
  28. ad_plan_create_action = 'create_ad_plan'
  29. refresh_wechat_action = 'refresh_wechat_info'
  30. # 1.实现本机服务
  31. # 2.实现线上docker-selenium服务
  32. class BaseHandler(tornado.web.RequestHandler):
  33. def options(self):
  34. pass
  35. def set_default_headers(self):
  36. self.set_header('Access-Control-Allow-Origin', '*')
  37. self.set_header('Access-Control-Allow-Headers', '*')
  38. self.set_header('Access-Control-Max-Age', 1000)
  39. self.set_header('Content-type', '*')
  40. self.set_header('Access-Control-Allow-Methods', '*')
  41. class create_ad_plan_local(BaseHandler):
  42. def post(self):
  43. request_dict = json.loads(self.request.body, encoding='utf-8')
  44. user_id = request_dict['user_id']
  45. ad_plan_list = request_dict['plan_list']
  46. sql_session = db.DBSession()
  47. if user_id is None or ad_plan_list is None:
  48. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  49. return
  50. # 落地页名字精确到毫秒,默认是全局唯一
  51. for _ in ad_plan_list:
  52. ad_plan_name = _['title']
  53. ad_plan_typesetting_info = {'user_id': user_id, 'name': ad_plan_name,
  54. 'typesetting': json.dumps(_, ensure_ascii=False)}
  55. ad_plan_typesetting_inserte = sql_tools.save_ad_plan_typesetting_info(
  56. ad_plan_typesetting_info=ad_plan_typesetting_info,
  57. table_ad_plan_typesetting=ad_plan_typesetting_table)
  58. sql_session.execute(ad_plan_typesetting_inserte)
  59. sql_session.commit()
  60. self.write({'status': {'msg': 'success', "RetCode": 200}})
  61. class create_ad_plan(BaseHandler):
  62. # TODO:只要tornado开着就不允许修改数据库,------想好之后上线如何操作
  63. @staticmethod
  64. def check_task(user_id):
  65. sql_session = db.DBSession()
  66. result = sql_tools.get_task_in_hand_num(user_id, sql_session)
  67. return result
  68. def save_task_info(self, user_id, ad_plan_list, sql_session, task_name):
  69. # 2.数据存入数据库
  70. if user_id is None or ad_plan_list is None:
  71. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  72. return
  73. # 2.1存计划数据
  74. for _ in ad_plan_list:
  75. ad_plan_name = _['title']
  76. ad_plan_typesetting_info = {'user_id': user_id, 'name': ad_plan_name,
  77. 'typesetting': json.dumps(_, ensure_ascii=False)}
  78. ad_plan_typesetting_inserte = sql_tools.save_ad_plan_typesetting_info(
  79. ad_plan_typesetting_info=ad_plan_typesetting_info,
  80. table_ad_plan_typesetting=ad_plan_typesetting_table)
  81. sql_session.execute(ad_plan_typesetting_inserte)
  82. sql_session.commit()
  83. for _ in ad_plan_list:
  84. for action_type in [layout_create_action, ad_plan_create_action]:
  85. object_name = _['title'] if action_type == ad_plan_create_action else \
  86. _['idea']['jump_type_page_type'][
  87. 'layout_name']
  88. action_info = {'user_id': user_id, 'service_name': _['service_name'],
  89. 'wechat_name': _['wechat_name'],
  90. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  91. 'status': 'todo'}
  92. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  93. table_action_record=action_record_table)
  94. sql_session.execute(record_insert)
  95. sql_session.commit()
  96. def post(self):
  97. sql_session = db.DBSession()
  98. log_ad = None
  99. try:
  100. request_dict = json.loads(self.request.body, encoding='utf-8')
  101. print(self.request.body)
  102. print(request_dict)
  103. ad_plan_list = request_dict['planList']
  104. user_id = request_dict['userId']
  105. # 2.2存行为记录
  106. task_name = 'user_id: {user_id} time:{time_sign} action:create_plan'.format(user_id=user_id,
  107. time_sign=datetime.now().strftime(
  108. "%Y-%m-%d, %H:%M:%S"))
  109. # 4.开始运行
  110. if not self.check_task(user_id=user_id):
  111. # 1.查看是否cookie可用
  112. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  113. self.save_task_info(user_id, ad_plan_list, sql_session, task_name)
  114. threading.Thread(target=user_action.carry_plan,
  115. args=(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name)).start()
  116. else:
  117. self.save_task_info(user_id, ad_plan_list, sql_session, task_name)
  118. self.write({'status': {'msg': 'success', "RetCode": 200}})
  119. except Exception as e:
  120. if log_ad:
  121. log_ad.driver.quit()
  122. logging.error(str(e))
  123. self.write('error')
  124. raise
  125. finally:
  126. sql_session.commit()
  127. class get_ad_plan_local(BaseHandler):
  128. def get(self):
  129. user_id = self.get_argument('user_id', None)
  130. layout_name = self.get_argument('plan_name', None)
  131. sql_session = db.DBSession()
  132. if user_id is None:
  133. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  134. return
  135. # 落地页名字精确到毫秒,默认是全局唯一
  136. if layout_name:
  137. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  138. typesetting_name=layout_name)
  139. else:
  140. # TODO:之后修改一下,让其查询效率高点,like效率过低
  141. layout_name = ''
  142. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  143. typesetting_name=layout_name)
  144. result_ = []
  145. for i in range(len(result)):
  146. typesetting, name, create_time, update_time = result[i]
  147. _ = {}
  148. _['typesetting'] = json.loads(typesetting)
  149. _['ad_plan_name'] = name
  150. _['id'] = i
  151. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  152. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  153. result_.append(_)
  154. self.write({'status': {'msg': 'success', "RetCode": 200},
  155. 'local_ad_plan_info': result_})
  156. class create_ad_layout_local(BaseHandler):
  157. def post(self):
  158. # TODO:返回一个layout_name重复的一个信息
  159. request_dict = json.loads(self.request.body)
  160. user_id = request_dict['user_id']
  161. layout_typesetting = request_dict['layout_typesetting']
  162. layout_name = request_dict['layout_name']
  163. sql_session = db.DBSession()
  164. if user_id is None or layout_name is None or layout_typesetting is None:
  165. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  166. return
  167. # 落地页名字精确到毫秒,默认是全局唯一
  168. layout_typesetting_info = {'user_id': user_id, 'name': layout_name,
  169. 'typesetting': layout_typesetting}
  170. layout_typesetting_inserte = sql_tools.save_layout_typesetting_info(
  171. layout_typesetting_info=layout_typesetting_info,
  172. table_layout_typesetting=layout_typesetting_table)
  173. sql_session.execute(layout_typesetting_inserte)
  174. sql_session.commit()
  175. self.write({'status': {'msg': 'success', "RetCode": 200}})
  176. class get_ad_layout_local(BaseHandler):
  177. def get(self):
  178. user_id = self.get_argument('user_id', None)
  179. layout_name = self.get_argument('layout_name', None)
  180. sql_session = db.DBSession()
  181. if user_id is None:
  182. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  183. return
  184. # 落地页名字精确到毫秒,默认是全局唯一
  185. if layout_name:
  186. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  187. typesetting_name=layout_name)
  188. else:
  189. # TODO:之后修改一下,让其查询效率高点,like效率过低
  190. layout_name = ''
  191. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  192. typesetting_name=layout_name)
  193. result_ = []
  194. for i in range(len(result)):
  195. typesetting, name, create_time, update_time = result[i]
  196. _ = {}
  197. _['typesetting'] = json.loads(typesetting)
  198. _['layout_name'] = name
  199. _['id'] = i
  200. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  201. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  202. result_.append(_)
  203. self.write({'status': {'msg': 'success', "RetCode": 200},
  204. 'local_layout_info': result_})
  205. class get_scan_status(BaseHandler):
  206. # 获取到扫码状态
  207. def get(self):
  208. sql_session = db.DBSession()
  209. user_id = self.get_argument("user_id", None)
  210. status = sql_tools.get_scan_action_status(user_id, sql_session)
  211. if user_id is None:
  212. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  213. return
  214. self.write({'status': {'msg': 'success', "RetCode": 200},
  215. 'scan_action_status': status})
  216. # TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
  217. class ad_human_info(BaseHandler):
  218. @staticmethod
  219. def refresh_wechat_cookies(tornado_web, user_id):
  220. # 1.返回二维码链接
  221. # ----1.查看cookie是否可用
  222. sql_session = db.DBSession()
  223. cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
  224. # 进行登录操作
  225. log_ad = LogIn(user_id=user_id)
  226. # 使driver可以使用
  227. cookie_canuse = False
  228. if cookie_db:
  229. cookie_db = pickle.loads(cookie_db)
  230. if not log_ad.wechat_cookies_check_alive(cookie_db):
  231. # cookie 不能使用
  232. wechat_code = log_ad.log_in()
  233. sql_tools.update_user_scan_action(user_id, sql_session)
  234. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  235. 'wechat_code': wechat_code})
  236. logging.info('cookie can not use')
  237. else:
  238. # cookie 可以继续使用
  239. cookie_canuse = True
  240. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  241. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  242. else:
  243. # cookie 不能使用
  244. wechat_code = log_ad.log_in()
  245. sql_tools.update_user_scan_action(user_id, sql_session)
  246. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  247. 'wechat_code': wechat_code})
  248. return log_ad, cookie_canuse
  249. # 1.人群包获取
  250. def get(self):
  251. sql_session = db.DBSession()
  252. log_ad = None
  253. try:
  254. # 0.是否刷新
  255. # 1.获取userid,以及是否刷新
  256. user_id = self.get_argument("user_id", None)
  257. human_package_name = self.get_argument('human_package_name', None)
  258. is_refresh = self.get_argument("is_refresh", None)
  259. wechat_name = self.get_argument('wechat_name', None)
  260. service_name = self.get_argument('service_name', None)
  261. if user_id is None or is_refresh is None or wechat_name is None or service_name is None:
  262. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  263. return
  264. # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
  265. if int(is_refresh) == 1:
  266. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  267. if not create_ad_plan.check_task(user_id=user_id):
  268. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  269. user_id=user_id,
  270. time_sign=datetime.now().strftime(
  271. "%Y-%m-%d, %H:%M:%S"))
  272. # 行为记录
  273. action_type = refresh_wechat_action
  274. object_name = ''
  275. service_name = ''
  276. wechat_name = ''
  277. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  278. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  279. 'status': 'todo'}
  280. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  281. table_action_record=action_record_table)
  282. sql_session.execute(record_insert)
  283. sql_session.commit()
  284. threading.Thread(target=user_action.get_human_info,
  285. args=(
  286. user_id, log_ad, db, cookie_canuse, task_name)).start()
  287. else:
  288. logging.info('任务有堆积')
  289. return
  290. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  291. else:
  292. # 1.查看是否在刷新,
  293. # 在刷新中,
  294. # 返回正在刷新
  295. # -------不管上面逻辑让他们多刷新几次
  296. # 不在刷新
  297. # 返回对应数据
  298. # 2.获取userid对应数据
  299. result = sql_tools.get_human_info(sql_session=sql_session,
  300. service_name=service_name, wechat_name=wechat_name)
  301. result = json.loads(result)
  302. if human_package_name:
  303. result = [_ for _ in result if human_package_name in _['name']]
  304. result_ = []
  305. for i in range(len(result)):
  306. _ = result[i]
  307. _['id'] = i
  308. result_.append(_)
  309. self.write({'status': {'msg': 'success', "RetCode": 200},
  310. 'human_info': result})
  311. except Exception as e:
  312. if log_ad:
  313. log_ad.driver.quit()
  314. logging.error(str(e))
  315. raise
  316. finally:
  317. sql_session.commit()
  318. class refresh_wechat_info(BaseHandler):
  319. # TODO:刷新以及创建,限时3分钟
  320. @staticmethod
  321. def refresh_wechat_cookies(tornado_web, user_id):
  322. # 1.返回二维码链接
  323. # ----1.查看cookie是否可用
  324. sql_session = db.DBSession()
  325. cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
  326. # 进行登录操作
  327. log_ad = LogIn(user_id=user_id)
  328. # 使driver可以使用
  329. cookie_canuse = False
  330. if cookie_db:
  331. cookie_db = pickle.loads(cookie_db)
  332. if not log_ad.wechat_cookies_check_alive(cookie_db):
  333. # cookie 不能使用
  334. wechat_code = log_ad.log_in()
  335. sql_tools.update_user_scan_action(user_id, sql_session)
  336. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  337. 'wechat_code': wechat_code})
  338. logging.info('cookie can not use')
  339. else:
  340. # cookie 可以继续使用
  341. cookie_canuse = True
  342. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  343. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  344. else:
  345. # cookie 不能使用
  346. wechat_code = log_ad.log_in()
  347. sql_tools.update_user_scan_action(user_id, sql_session)
  348. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  349. 'wechat_code': wechat_code})
  350. return log_ad, cookie_canuse
  351. def get(self):
  352. sql_session = db.DBSession()
  353. log_ad = None
  354. try:
  355. user_id = self.get_argument("user_id", None)
  356. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  357. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  358. user_id=user_id,
  359. time_sign=datetime.now().strftime(
  360. "%Y-%m-%d, %H:%M:%S"))
  361. # 行为记录
  362. action_type = refresh_wechat_action
  363. object_name = ''
  364. service_name = ''
  365. wechat_name = ''
  366. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  367. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  368. 'status': 'todo'}
  369. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  370. table_action_record=action_record_table)
  371. sql_session.execute(record_insert)
  372. sql_session.commit()
  373. if not create_ad_plan.check_task(user_id=user_id):
  374. threading.Thread(target=user_action.get_human_info,
  375. args=(
  376. user_id, log_ad, db, cookie_canuse, task_name)).start()
  377. else:
  378. return
  379. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  380. except:
  381. pass
  382. class ad_wechat_info(BaseHandler):
  383. # 1.公众号相关信息获取
  384. def get(self):
  385. sql_session = db.DBSession()
  386. log_ad = None
  387. try:
  388. # 0.是否刷新
  389. # 1.获取userid,以及是否刷新
  390. user_id = self.get_argument("userId", None)
  391. is_refresh = self.get_argument("isRefresh", None)
  392. if user_id is None or is_refresh is None:
  393. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  394. return
  395. if int(is_refresh) == 1:
  396. # 检查有无其他任务在处理中,有则等待
  397. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  398. if not create_ad_plan.check_task(user_id=user_id):
  399. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  400. user_id=user_id,
  401. time_sign=datetime.now().strftime(
  402. "%Y-%m-%d, %H:%M:%S"))
  403. # 行为记录
  404. action_type = refresh_wechat_action
  405. object_name = ''
  406. service_name = ''
  407. wechat_name = ''
  408. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  409. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  410. 'status': 'todo'}
  411. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  412. table_action_record=action_record_table)
  413. sql_session.execute(record_insert)
  414. sql_session.commit()
  415. threading.Thread(target=user_action.get_human_info,
  416. args=(
  417. user_id, log_ad, db, cookie_canuse, task_name)).start()
  418. else:
  419. return
  420. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  421. else:
  422. result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
  423. result_list = []
  424. for _ in result:
  425. service_name, wechat_name = _
  426. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  427. self.write({'status': {'msg': 'success', "RetCode": 200},
  428. 'wechat_info': result_list})
  429. except Exception as e:
  430. if log_ad:
  431. log_ad.driver.quit()
  432. logging.error(str(e))
  433. raise
  434. finally:
  435. sql_session.commit()
  436. class delete_ad_layout(BaseHandler):
  437. def get(self):
  438. user_id = self.get_argument('user_id', None)
  439. layout_name = self.get_argument('layout_name', None)
  440. sql_session = db.DBSession()
  441. if user_id is None or layout_name is None:
  442. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  443. return
  444. # 落地页名字精确到毫秒,默认是全局唯一
  445. sql_tools.delete_layout_typesetting_vir(sql_session=sql_session, user_id=user_id,
  446. typesetting_name=layout_name)
  447. self.write({'status': {'msg': 'success', "RetCode": 200}})
  448. class delete_ad_plan(BaseHandler):
  449. def get(self):
  450. user_id = self.get_argument('user_id', None)
  451. plan_name = self.get_argument('plan_name', None)
  452. service_name = self.get_argument('service_name', None)
  453. wechat_name = self.get_argument('wechat_name', None)
  454. sql_session = db.DBSession()
  455. if user_id is None or plan_name is None:
  456. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  457. return
  458. # 落地页名字精确到毫秒,默认是全局唯一
  459. sql_tools.delete_ad_plan_typesetting_vir(sql_session=sql_session, user_id=user_id,
  460. typesetting_name=plan_name, wechat_name=wechat_name,
  461. service_name=service_name)
  462. self.write({'status': {'msg': 'success', "RetCode": 200}})
  463. class get_ad_wechat_service_name(BaseHandler):
  464. def get(self):
  465. user_id = self.get_argument('user_id', None)
  466. sql_session = db.DBSession()
  467. if user_id is None:
  468. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  469. return
  470. result = sql_tools.get_wechat_info_service_name(sql_session=sql_session, user_id=user_id)
  471. result_list = []
  472. for _ in result:
  473. service_name = _
  474. result_list.append({'service_name': service_name})
  475. self.write({'status': {'msg': 'success', "RetCode": 200},
  476. 'wechat_info': result_list})
  477. class get_ad_wechat_wechat_name(BaseHandler):
  478. def get(self):
  479. user_id = self.get_argument('user_id', None)
  480. service_name = self.get_argument('service_name', None)
  481. sql_session = db.DBSession()
  482. if user_id is None or service_name is None:
  483. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  484. return
  485. result = sql_tools.get_wechat_info_wechat_name(sql_session=sql_session, user_id=user_id,
  486. service_name=service_name)
  487. result_list = []
  488. for _ in result:
  489. service_name, wechat_name = _
  490. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  491. self.write({'status': {'msg': 'success', "RetCode": 200},
  492. 'wechat_info': result_list})
  493. class get_plan_action_record(BaseHandler):
  494. def get(self):
  495. user_id = self.get_argument('user_id', None)
  496. service_name = self.get_argument('service_name', None)
  497. wechat_name = self.get_argument('wechat_name', None)
  498. status = self.get_argument('status', None)
  499. plan_name = self.get_argument('plan_name', None)
  500. sql_session = db.DBSession()
  501. if user_id is None:
  502. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  503. return
  504. # 落地页名字精确到毫秒,默认是全局唯一
  505. result = sql_tools.get_plan_record(sql_session=sql_session, user_id=user_id,
  506. service_name=service_name, wechat_name=wechat_name,
  507. status=status, plan_name=plan_name)
  508. result_ = []
  509. for i in range(len(result)):
  510. user_id, name, service_name, wechat_name, create_time, status, typesetting, wechat_id_info = result[i]
  511. _ = {}
  512. _['typesetting'] = json.loads(typesetting)
  513. _['ad_plan_name'] = name
  514. _['id'] = i
  515. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  516. _['service_name'] = service_name
  517. _['wechat_name'] = wechat_name
  518. _['wechat_id_info'] = wechat_id_info
  519. _['status'] = status
  520. result_.append(_)
  521. self.write({'status': {'msg': 'success', "RetCode": 200},
  522. 'local_ad_plan_info': result_})
  523. class get_all_ad_task(BaseHandler):
  524. def get(self):
  525. user_id = self.get_argument('user_id', None)
  526. sql_session = db.DBSession()
  527. if user_id is None:
  528. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  529. return
  530. # 落地页名字精确到毫秒,默认是全局唯一
  531. result = sql_tools.get_ad_task(sql_session=sql_session, user_id=user_id)
  532. task_dict = {}
  533. localtion = ['wechat', '']
  534. for _ in result:
  535. task_name, status, task_status_num, create_time, typesetting = _
  536. typesetting = json.loads(typesetting)
  537. if typesetting['plan_base'][1] == 'pyq':
  538. localtion[1] = 'pyq'
  539. create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
  540. if task_name not in task_dict.keys():
  541. task_dict[task_name] = {}
  542. task_dict[task_name][status] = (task_status_num, create_time)
  543. result_ = []
  544. num = 0
  545. for k, v in task_dict.items():
  546. # TODO:修改为dict的sort
  547. sum_num = 0
  548. new_dict = {}
  549. create_time = None
  550. for k_, v_ in v.items():
  551. task_status_num, create_time = v_
  552. sum_num = sum_num + task_status_num
  553. new_dict[k_] = task_status_num
  554. status = 'todo' if 'todo' in new_dict.keys() else 'done'
  555. task_dict[k]['sum_num'] = sum_num
  556. new_dict['sum_num'] = sum_num
  557. result_.append(
  558. {'task_name': k, 'task_info': new_dict, 'create_time': create_time, 'channel': localtion[0],
  559. 'localtion': localtion[1], 'id': num, 'status': status})
  560. num = num + 1
  561. self.write({'status': {'msg': 'success', "RetCode": 200},
  562. 'local_ad_plan_info': result_})
  563. def heart_jump():
  564. # TODO:tornado 心跳检测,下周做----线程不断检查,线程生命周期60分钟
  565. pass
  566. def make_app():
  567. return tornado.web.Application([
  568. ("/get_all_ad_task", get_all_ad_task), # 获取所有任务状态,
  569. ("/create_ad_plan", create_ad_plan), #
  570. ("/get_ad_wechat_service_name", get_ad_wechat_service_name),
  571. ("/get_ad_wechat_wechat_name", get_ad_wechat_wechat_name),
  572. # ("/create_ad_plan_local", create_ad_plan_local),
  573. ("/create_ad_layout_local", create_ad_layout_local),
  574. ("/get_layout_local", get_ad_layout_local),
  575. ("/get_ad_plan_local", get_ad_plan_local),
  576. ("/delete_layout_local", delete_ad_layout),
  577. ("/delete_ad_plan_local", delete_ad_plan),
  578. ("/get_scan_status", get_scan_status),
  579. # ("/create_ad_layout_remote", create_ad_layout_remote),
  580. ("/ad_human_info", ad_human_info),
  581. ("/ad_wechat_info", ad_wechat_info),
  582. ("/get_plan_action_record", get_plan_action_record),
  583. ], debug=True, autoreload=True)
  584. if __name__ == "__main__":
  585. import logging
  586. logging.basicConfig(
  587. handlers=[
  588. logging.handlers.RotatingFileHandler('./tornado.log',
  589. maxBytes=10 * 1024 * 1024,
  590. backupCount=5,
  591. encoding='utf-8')
  592. , logging.StreamHandler() # 供输出使用
  593. ],
  594. level=logging.INFO,
  595. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  596. )
  597. handler = logging.FileHandler('tornado.log')
  598. logger = logging.getLogger()
  599. logger.addHandler(handler)
  600. logger.setLevel(logging.INFO)
  601. app = make_app()
  602. app.listen(8888)
  603. tornado.ioloop.IOLoop.current().start()