tornado_api.py 27 KB

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