tornado_api.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. # TODO:名字检查----只保留三种符号(.-_),中文字符长度一,数字字符长度二
  64. @staticmethod
  65. def check_task(user_id):
  66. # TODO:检查是否同user_id的任务在跑中,有的话只保存任务.不做其他事情
  67. sql_session = db.DBSession()
  68. result = sql_tools.get_task_in_hand_num(user_id, sql_session)
  69. return result
  70. def save_task_info(self, user_id, ad_plan_list, sql_session, task_name):
  71. # 2.数据存入数据库
  72. if user_id is None or ad_plan_list is None:
  73. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  74. return
  75. # 2.1存计划数据
  76. for _ in ad_plan_list:
  77. ad_plan_name = _['title']
  78. ad_plan_typesetting_info = {'user_id': user_id, 'name': ad_plan_name,
  79. 'typesetting': json.dumps(_, ensure_ascii=False)}
  80. ad_plan_typesetting_inserte = sql_tools.save_ad_plan_typesetting_info(
  81. ad_plan_typesetting_info=ad_plan_typesetting_info,
  82. table_ad_plan_typesetting=ad_plan_typesetting_table)
  83. sql_session.execute(ad_plan_typesetting_inserte)
  84. sql_session.commit()
  85. for _ in ad_plan_list:
  86. for action_type in [layout_create_action, ad_plan_create_action]:
  87. object_name = _['title'] if action_type == ad_plan_create_action else \
  88. _['idea']['jump_type_page_type'][
  89. 'layout_name']
  90. action_info = {'user_id': user_id, 'service_name': _['service_name'],
  91. 'wechat_name': _['wechat_name'],
  92. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  93. 'status': 'todo'}
  94. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  95. table_action_record=action_record_table)
  96. sql_session.execute(record_insert)
  97. sql_session.commit()
  98. def post(self):
  99. sql_session = db.DBSession()
  100. log_ad = None
  101. try:
  102. request_dict = json.loads(self.request.body, encoding='utf-8')
  103. ad_plan_list = request_dict['plan_list']
  104. user_id = request_dict['user_id']
  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. except Exception as e:
  119. if log_ad:
  120. log_ad.driver.quit()
  121. logging.error(str(e))
  122. self.write('eror')
  123. raise
  124. finally:
  125. sql_session.commit()
  126. class get_ad_plan_local(BaseHandler):
  127. def get(self):
  128. user_id = self.get_argument('user_id', None)
  129. layout_name = self.get_argument('plan_name', None)
  130. sql_session = db.DBSession()
  131. if user_id is None:
  132. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  133. return
  134. # 落地页名字精确到毫秒,默认是全局唯一
  135. if layout_name:
  136. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  137. typesetting_name=layout_name)
  138. else:
  139. # TODO:之后修改一下,让其查询效率高点,like效率过低
  140. layout_name = ''
  141. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  142. typesetting_name=layout_name)
  143. result_ = []
  144. for i in range(len(result)):
  145. typesetting, name, create_time, update_time = result[i]
  146. _ = {}
  147. _['typesetting'] = json.loads(typesetting)
  148. _['ad_plan_name'] = name
  149. _['id'] = i
  150. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  151. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  152. result_.append(_)
  153. self.write({'status': {'msg': 'success', "RetCode": 200},
  154. 'local_ad_plan_info': result_})
  155. class create_ad_layout_local(BaseHandler):
  156. def post(self):
  157. # TODO:返回一个layout_name重复的一个信息
  158. request_dict = json.loads(self.request.body)
  159. user_id = request_dict['user_id']
  160. layout_typesetting = request_dict['layout_typesetting']
  161. layout_name = request_dict['layout_name']
  162. sql_session = db.DBSession()
  163. if user_id is None or layout_name is None or layout_typesetting is None:
  164. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  165. return
  166. # 落地页名字精确到毫秒,默认是全局唯一
  167. layout_typesetting_info = {'user_id': user_id, 'name': layout_name,
  168. 'typesetting': layout_typesetting}
  169. layout_typesetting_inserte = sql_tools.save_layout_typesetting_info(
  170. layout_typesetting_info=layout_typesetting_info,
  171. table_layout_typesetting=layout_typesetting_table)
  172. sql_session.execute(layout_typesetting_inserte)
  173. sql_session.commit()
  174. self.write({'status': {'msg': 'success', "RetCode": 200}})
  175. class get_ad_layout_local(BaseHandler):
  176. def get(self):
  177. user_id = self.get_argument('user_id', None)
  178. layout_name = self.get_argument('layout_name', None)
  179. sql_session = db.DBSession()
  180. if user_id is None:
  181. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  182. return
  183. # 落地页名字精确到毫秒,默认是全局唯一
  184. if layout_name:
  185. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  186. typesetting_name=layout_name)
  187. else:
  188. # TODO:之后修改一下,让其查询效率高点,like效率过低
  189. layout_name = ''
  190. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  191. typesetting_name=layout_name)
  192. result_ = []
  193. for i in range(len(result)):
  194. typesetting, name, create_time, update_time = result[i]
  195. _ = {}
  196. _['typesetting'] = json.loads(typesetting)
  197. _['layout_name'] = name
  198. _['id'] = i
  199. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  200. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  201. result_.append(_)
  202. self.write({'status': {'msg': 'success', "RetCode": 200},
  203. 'local_layout_info': result_})
  204. class get_scan_status(BaseHandler):
  205. # 获取到扫码状态
  206. def get(self):
  207. sql_session = db.DBSession()
  208. user_id = self.get_argument("user_id", None)
  209. status = sql_tools.get_scan_action_status(user_id, sql_session)
  210. if user_id is None:
  211. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  212. return
  213. self.write({'status': {'msg': 'success', "RetCode": 200},
  214. 'scan_action_status': status})
  215. # TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
  216. class ad_human_info(BaseHandler):
  217. # TODO:设置一下update---table,如果失败了sql_session需要关闭
  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. if not create_ad_plan.check_task(user_id=user_id):
  267. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, 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. self.write({'status': {'msg': '', "RetCode": 200}})
  289. else:
  290. # 1.查看是否在刷新,
  291. # 在刷新中,
  292. # 返回正在刷新
  293. # -------不管上面逻辑让他们多刷新几次
  294. # 不在刷新
  295. # 返回对应数据
  296. # 2.获取userid对应数据
  297. result = sql_tools.get_human_info(sql_session=sql_session,
  298. service_name=service_name, wechat_name=wechat_name)
  299. result = json.loads(result)
  300. if human_package_name:
  301. result = [_ for _ in result if human_package_name in _['name']]
  302. result_ = []
  303. for i in range(len(result)):
  304. _ = result[i]
  305. _['id'] = i
  306. result_.append(_)
  307. self.write({'status': {'msg': 'success', "RetCode": 200},
  308. 'human_info': result})
  309. except Exception as e:
  310. if log_ad:
  311. log_ad.driver.quit()
  312. logging.error(str(e))
  313. raise
  314. finally:
  315. sql_session.commit()
  316. class ad_wechat_info(BaseHandler):
  317. # 1.公众号相关信息获取
  318. def get(self):
  319. sql_session = db.DBSession()
  320. log_ad = None
  321. try:
  322. # TODO:添加分页,
  323. # 公众号,服务商,唯一id设计或者获取
  324. # 0.是否刷新
  325. # 1.获取userid,以及是否刷新
  326. user_id = self.get_argument("user_id", None)
  327. is_refresh = self.get_argument("is_refresh", None)
  328. if user_id is None or is_refresh is None:
  329. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  330. return
  331. if int(is_refresh) == 1:
  332. # 检查有无其他任务在处理中,有则等待
  333. if not create_ad_plan.check_task(user_id=user_id):
  334. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  335. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  336. user_id=user_id,
  337. time_sign=datetime.now().strftime(
  338. "%Y-%m-%d, %H:%M:%S"))
  339. # 行为记录
  340. action_type = refresh_wechat_action
  341. object_name = ''
  342. service_name = ''
  343. wechat_name = ''
  344. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  345. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  346. 'status': 'todo'}
  347. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  348. table_action_record=action_record_table)
  349. sql_session.execute(record_insert)
  350. sql_session.commit()
  351. threading.Thread(target=user_action.get_human_info,
  352. args=(
  353. user_id, log_ad, db, cookie_canuse, task_name)).start()
  354. else:
  355. self.write({'status': {'msg': '', "RetCode": 200}})
  356. else:
  357. result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
  358. result_list = []
  359. for _ in result:
  360. service_name, wechat_name = _
  361. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  362. self.write({'status': {'msg': 'success', "RetCode": 200},
  363. 'wechat_info': result_list})
  364. except Exception as e:
  365. if log_ad:
  366. log_ad.driver.quit()
  367. logging.error(str(e))
  368. raise
  369. finally:
  370. sql_session.commit()
  371. class delete_ad_layout(BaseHandler):
  372. def get(self):
  373. user_id = self.get_argument('user_id', None)
  374. layout_name = self.get_argument('layout_name', None)
  375. sql_session = db.DBSession()
  376. if user_id is None or layout_name is None:
  377. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  378. return
  379. # 落地页名字精确到毫秒,默认是全局唯一
  380. sql_tools.delete_layout_typesetting_vir(sql_session=sql_session, user_id=user_id,
  381. typesetting_name=layout_name)
  382. self.write({'status': {'msg': 'success', "RetCode": 200}})
  383. class delete_ad_plan(BaseHandler):
  384. def get(self):
  385. user_id = self.get_argument('user_id', None)
  386. plan_name = self.get_argument('plan_name', None)
  387. service_name = self.get_argument('service_name', None)
  388. wechat_name = self.get_argument('wechat_name', None)
  389. sql_session = db.DBSession()
  390. if user_id is None or plan_name is None:
  391. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  392. return
  393. # 落地页名字精确到毫秒,默认是全局唯一
  394. sql_tools.delete_ad_plan_typesetting_vir(sql_session=sql_session, user_id=user_id,
  395. typesetting_name=plan_name, wechat_name=wechat_name,
  396. service_name=service_name)
  397. self.write({'status': {'msg': 'success', "RetCode": 200}})
  398. class get_ad_wechat_service_name(BaseHandler):
  399. def get(self):
  400. user_id = self.get_argument('user_id', None)
  401. sql_session = db.DBSession()
  402. if user_id is None:
  403. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  404. return
  405. result = sql_tools.get_wechat_info_service_name(sql_session=sql_session, user_id=user_id)
  406. result_list = []
  407. for _ in result:
  408. service_name = _
  409. result_list.append({'service_name': service_name})
  410. self.write({'status': {'msg': 'success', "RetCode": 200},
  411. 'wechat_info': result_list})
  412. class get_ad_wechat_wechat_name(BaseHandler):
  413. def get(self):
  414. user_id = self.get_argument('user_id', None)
  415. service_name = self.get_argument('service_name', None)
  416. sql_session = db.DBSession()
  417. if user_id is None or service_name is None:
  418. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  419. return
  420. result = sql_tools.get_wechat_info_wechat_name(sql_session=sql_session, user_id=user_id,
  421. service_name=service_name)
  422. result_list = []
  423. for _ in result:
  424. service_name, wechat_name = _
  425. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  426. self.write({'status': {'msg': 'success', "RetCode": 200},
  427. 'wechat_info': result_list})
  428. class get_plan_action_record(BaseHandler):
  429. def get(self):
  430. user_id = self.get_argument('user_id', None)
  431. service_name = self.get_argument('service_name', None)
  432. wechat_name = self.get_argument('wechat_name', None)
  433. status = self.get_argument('status', None)
  434. plan_name = self.get_argument('plan_name', None)
  435. sql_session = db.DBSession()
  436. if user_id is None:
  437. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  438. return
  439. # 落地页名字精确到毫秒,默认是全局唯一
  440. result = sql_tools.get_plan_record(sql_session=sql_session, user_id=user_id,
  441. service_name=service_name, wechat_name=wechat_name,
  442. status=status, plan_name=plan_name)
  443. result_ = []
  444. for i in range(len(result)):
  445. user_id, name, service_name, wechat_name, create_time, status, typesetting, wechat_id_info = result[i]
  446. _ = {}
  447. _['typesetting'] = json.loads(typesetting)
  448. _['ad_plan_name'] = name
  449. _['id'] = i
  450. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  451. _['service_name'] = service_name
  452. _['wechat_name'] = wechat_name
  453. _['wechat_id_info'] = wechat_id_info
  454. _['status'] = status
  455. result_.append(_)
  456. self.write({'status': {'msg': 'success', "RetCode": 200},
  457. 'local_ad_plan_info': result_})
  458. class get_all_ad_task(BaseHandler):
  459. def get(self):
  460. user_id = self.get_argument('user_id', None)
  461. sql_session = db.DBSession()
  462. if user_id is None:
  463. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  464. return
  465. # 落地页名字精确到毫秒,默认是全局唯一
  466. result = sql_tools.get_ad_task(sql_session=sql_session, user_id=user_id)
  467. task_dict = {}
  468. localtion = ['wechat', '']
  469. for _ in result:
  470. task_name, status, task_status_num, create_time, typesetting = _
  471. typesetting = json.loads(typesetting)
  472. if typesetting['plan_base'][1] == 'pyq':
  473. localtion[1] = 'pyq'
  474. create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
  475. if task_name not in task_dict.keys():
  476. task_dict[task_name] = {}
  477. task_dict[task_name][status] = (task_status_num, create_time)
  478. result_ = []
  479. num = 0
  480. for k, v in task_dict.items():
  481. # TODO:修改为dict的sort
  482. sum_num = 0
  483. new_dict = {}
  484. create_time = None
  485. for k_, v_ in v.items():
  486. task_status_num, create_time = v_
  487. sum_num = sum_num + task_status_num
  488. new_dict[k_] = task_status_num
  489. status = 'todo' if 'todo' in new_dict.keys() else 'done'
  490. task_dict[k]['sum_num'] = sum_num
  491. new_dict['sum_num'] = sum_num
  492. result_.append(
  493. {'task_name': k, 'task_info': new_dict, 'create_time': create_time, 'channel': localtion[0],
  494. 'localtion': localtion[1], 'id': num, 'status': status})
  495. num = num + 1
  496. self.write({'status': {'msg': 'success', "RetCode": 200},
  497. 'local_ad_plan_info': result_})
  498. def heart_jump():
  499. # TODO:tornado 心跳检测,下周做----线程不断检查,线程生命周期60分钟
  500. pass
  501. def make_app():
  502. return tornado.web.Application([
  503. ("/get_all_ad_task", get_all_ad_task), # 获取所有任务状态,
  504. ("/create_ad_plan", create_ad_plan), #
  505. ("/get_ad_wechat_service_name", get_ad_wechat_service_name),
  506. ("/get_ad_wechat_wechat_name", get_ad_wechat_wechat_name),
  507. # ("/create_ad_plan_local", create_ad_plan_local),
  508. ("/create_ad_layout_local", create_ad_layout_local),
  509. ("/get_layout_local", get_ad_layout_local),
  510. ("/get_ad_plan_local", get_ad_plan_local),
  511. ("/delete_layout_local", delete_ad_layout),
  512. ("/delete_ad_plan_local", delete_ad_plan),
  513. ("/get_scan_status", get_scan_status),
  514. # ("/create_ad_layout_remote", create_ad_layout_remote),
  515. ("/ad_human_info", ad_human_info),
  516. ("/ad_wechat_info", ad_wechat_info),
  517. ("/get_plan_action_record", get_plan_action_record),
  518. ], debug=True, autoreload=True)
  519. if __name__ == "__main__":
  520. import logging
  521. logging.basicConfig(
  522. handlers=[
  523. logging.handlers.RotatingFileHandler('./tornado.log',
  524. maxBytes=10 * 1024 * 1024,
  525. backupCount=5,
  526. encoding='utf-8')
  527. , logging.StreamHandler() # 供输出使用
  528. ],
  529. level=logging.INFO,
  530. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  531. )
  532. handler = logging.FileHandler('tornado.log')
  533. logger = logging.getLogger()
  534. logger.addHandler(handler)
  535. logger.setLevel(logging.INFO)
  536. app = make_app()
  537. app.listen(8888)
  538. tornado.ioloop.IOLoop.current().start()