tornado_api.py 27 KB

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