tornado_api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. 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. @staticmethod
  218. def refresh_wechat_cookies(tornado_web, user_id):
  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. sql_tools.update_user_scan_action(user_id, sql_session)
  233. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  234. 'wechat_code': wechat_code})
  235. logging.info('cookie can not use')
  236. else:
  237. # cookie 可以继续使用
  238. cookie_canuse = True
  239. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  240. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  241. else:
  242. # cookie 不能使用
  243. wechat_code = log_ad.log_in()
  244. sql_tools.update_user_scan_action(user_id, sql_session)
  245. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  246. 'wechat_code': wechat_code})
  247. return log_ad, cookie_canuse
  248. # 1.人群包获取
  249. def get(self):
  250. sql_session = db.DBSession()
  251. log_ad = None
  252. try:
  253. # 0.是否刷新
  254. # 1.获取userid,以及是否刷新
  255. user_id = self.get_argument("user_id", None)
  256. human_package_name = self.get_argument('human_package_name', None)
  257. is_refresh = self.get_argument("is_refresh", None)
  258. wechat_name = self.get_argument('wechat_name', None)
  259. service_name = self.get_argument('service_name', None)
  260. if user_id is None or is_refresh is None or wechat_name is None or service_name is None:
  261. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  262. return
  263. # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
  264. if int(is_refresh) == 1:
  265. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  266. if not create_ad_plan.check_task(user_id=user_id):
  267. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  268. user_id=user_id,
  269. time_sign=datetime.now().strftime(
  270. "%Y-%m-%d, %H:%M:%S"))
  271. # 行为记录
  272. action_type = refresh_wechat_action
  273. object_name = ''
  274. service_name = ''
  275. wechat_name = ''
  276. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  277. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  278. 'status': 'todo'}
  279. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  280. table_action_record=action_record_table)
  281. sql_session.execute(record_insert)
  282. sql_session.commit()
  283. threading.Thread(target=user_action.get_human_info,
  284. args=(
  285. user_id, log_ad, db, cookie_canuse, task_name)).start()
  286. else:
  287. logging.info('任务有堆积')
  288. return
  289. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  290. else:
  291. # 1.查看是否在刷新,
  292. # 在刷新中,
  293. # 返回正在刷新
  294. # -------不管上面逻辑让他们多刷新几次
  295. # 不在刷新
  296. # 返回对应数据
  297. # 2.获取userid对应数据
  298. result = sql_tools.get_human_info(sql_session=sql_session,
  299. service_name=service_name, wechat_name=wechat_name)
  300. result = json.loads(result)
  301. if human_package_name:
  302. result = [_ for _ in result if human_package_name in _['name']]
  303. result_ = []
  304. for i in range(len(result)):
  305. _ = result[i]
  306. _['id'] = i
  307. result_.append(_)
  308. self.write({'status': {'msg': 'success', "RetCode": 200},
  309. 'human_info': result})
  310. except Exception as e:
  311. if log_ad:
  312. log_ad.driver.quit()
  313. logging.error(str(e))
  314. finally:
  315. sql_session.commit()
  316. class refresh_wechat_info(BaseHandler):
  317. # TODO:刷新以及创建,限时3分钟
  318. @staticmethod
  319. def refresh_wechat_cookies(tornado_web, user_id):
  320. # 1.返回二维码链接
  321. # ----1.查看cookie是否可用
  322. sql_session = db.DBSession()
  323. cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
  324. # 进行登录操作
  325. log_ad = LogIn(user_id=user_id)
  326. # 使driver可以使用
  327. cookie_canuse = False
  328. if cookie_db:
  329. cookie_db = pickle.loads(cookie_db)
  330. if not log_ad.wechat_cookies_check_alive(cookie_db):
  331. # cookie 不能使用
  332. wechat_code = log_ad.log_in()
  333. sql_tools.update_user_scan_action(user_id, sql_session)
  334. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  335. 'wechat_code': wechat_code})
  336. logging.info('cookie can not use')
  337. else:
  338. # cookie 可以继续使用
  339. cookie_canuse = True
  340. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  341. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  342. else:
  343. # cookie 不能使用
  344. wechat_code = log_ad.log_in()
  345. sql_tools.update_user_scan_action(user_id, sql_session)
  346. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  347. 'wechat_code': wechat_code})
  348. return log_ad, cookie_canuse
  349. def get(self):
  350. sql_session = db.DBSession()
  351. log_ad = None
  352. try:
  353. user_id = self.get_argument("user_id", None)
  354. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  355. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  356. user_id=user_id,
  357. time_sign=datetime.now().strftime(
  358. "%Y-%m-%d, %H:%M:%S"))
  359. # 行为记录
  360. action_type = refresh_wechat_action
  361. object_name = ''
  362. service_name = ''
  363. wechat_name = ''
  364. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  365. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  366. 'status': 'todo'}
  367. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  368. table_action_record=action_record_table)
  369. sql_session.execute(record_insert)
  370. sql_session.commit()
  371. if not create_ad_plan.check_task(user_id=user_id):
  372. threading.Thread(target=user_action.get_human_info,
  373. args=(
  374. user_id, log_ad, db, cookie_canuse, task_name)).start()
  375. else:
  376. return
  377. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  378. except:
  379. pass
  380. class ad_wechat_info(BaseHandler):
  381. # 1.公众号相关信息获取
  382. def get(self):
  383. sql_session = db.DBSession()
  384. log_ad = None
  385. try:
  386. # 0.是否刷新
  387. # 1.获取userid,以及是否刷新
  388. user_id = self.get_argument("userId", None)
  389. is_refresh = self.get_argument("isRefresh", None)
  390. if user_id is None or is_refresh is None:
  391. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  392. return
  393. if int(is_refresh) == 1:
  394. # 检查有无其他任务在处理中,有则等待
  395. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  396. if not create_ad_plan.check_task(user_id=user_id):
  397. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  398. user_id=user_id,
  399. time_sign=datetime.now().strftime(
  400. "%Y-%m-%d, %H:%M:%S"))
  401. # 行为记录
  402. action_type = refresh_wechat_action
  403. object_name = ''
  404. service_name = ''
  405. wechat_name = ''
  406. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  407. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  408. 'status': 'todo'}
  409. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  410. table_action_record=action_record_table)
  411. sql_session.execute(record_insert)
  412. sql_session.commit()
  413. threading.Thread(target=user_action.get_human_info,
  414. args=(
  415. user_id, log_ad, db, cookie_canuse, task_name)).start()
  416. else:
  417. return
  418. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  419. else:
  420. result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
  421. result_list = []
  422. for _ in result:
  423. service_name, wechat_name = _
  424. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  425. self.write({'status': {'msg': 'success', "RetCode": 200},
  426. 'wechat_info': result_list})
  427. except Exception as e:
  428. if log_ad:
  429. log_ad.driver.quit()
  430. logging.error(str(e))
  431. finally:
  432. sql_session.commit()
  433. class delete_ad_layout(BaseHandler):
  434. def get(self):
  435. user_id = self.get_argument('user_id', None)
  436. layout_name = self.get_argument('layout_name', None)
  437. sql_session = db.DBSession()
  438. if user_id is None or layout_name is None:
  439. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  440. return
  441. # 落地页名字精确到毫秒,默认是全局唯一
  442. sql_tools.delete_layout_typesetting_vir(sql_session=sql_session, user_id=user_id,
  443. typesetting_name=layout_name)
  444. self.write({'status': {'msg': 'success', "RetCode": 200}})
  445. class delete_ad_plan(BaseHandler):
  446. def get(self):
  447. user_id = self.get_argument('user_id', None)
  448. plan_name = self.get_argument('plan_name', None)
  449. service_name = self.get_argument('service_name', None)
  450. wechat_name = self.get_argument('wechat_name', None)
  451. sql_session = db.DBSession()
  452. if user_id is None or plan_name is None:
  453. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  454. return
  455. # 落地页名字精确到毫秒,默认是全局唯一
  456. sql_tools.delete_ad_plan_typesetting_vir(sql_session=sql_session, user_id=user_id,
  457. typesetting_name=plan_name, wechat_name=wechat_name,
  458. service_name=service_name)
  459. self.write({'status': {'msg': 'success', "RetCode": 200}})
  460. class get_ad_wechat_service_name(BaseHandler):
  461. def get(self):
  462. user_id = self.get_argument('user_id', None)
  463. sql_session = db.DBSession()
  464. if user_id is None:
  465. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  466. return
  467. result = sql_tools.get_wechat_info_service_name(sql_session=sql_session, user_id=user_id)
  468. result_list = []
  469. for _ in result:
  470. service_name = _
  471. result_list.append({'service_name': service_name})
  472. self.write({'status': {'msg': 'success', "RetCode": 200},
  473. 'wechat_info': result_list})
  474. class get_ad_wechat_wechat_name(BaseHandler):
  475. def get(self):
  476. user_id = self.get_argument('user_id', None)
  477. service_name = self.get_argument('service_name', None)
  478. sql_session = db.DBSession()
  479. if user_id is None or service_name is None:
  480. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  481. return
  482. result = sql_tools.get_wechat_info_wechat_name(sql_session=sql_session, user_id=user_id,
  483. service_name=service_name)
  484. result_list = []
  485. for _ in result:
  486. service_name, wechat_name = _
  487. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  488. self.write({'status': {'msg': 'success', "RetCode": 200},
  489. 'wechat_info': result_list})
  490. class get_plan_action_record(BaseHandler):
  491. def get(self):
  492. user_id = self.get_argument('user_id', None)
  493. service_name = self.get_argument('service_name', None)
  494. wechat_name = self.get_argument('wechat_name', None)
  495. status = self.get_argument('status', None)
  496. plan_name = self.get_argument('plan_name', None)
  497. sql_session = db.DBSession()
  498. if user_id is None:
  499. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  500. return
  501. # 落地页名字精确到毫秒,默认是全局唯一
  502. result = sql_tools.get_plan_record(sql_session=sql_session, user_id=user_id,
  503. service_name=service_name, wechat_name=wechat_name,
  504. status=status, plan_name=plan_name)
  505. result_ = []
  506. for i in range(len(result)):
  507. user_id, name, service_name, wechat_name, create_time, status, typesetting, wechat_id_info = result[i]
  508. _ = {}
  509. _['typesetting'] = json.loads(typesetting)
  510. _['ad_plan_name'] = name
  511. _['id'] = i
  512. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  513. _['service_name'] = service_name
  514. _['wechat_name'] = wechat_name
  515. _['wechat_id_info'] = wechat_id_info
  516. _['status'] = status
  517. result_.append(_)
  518. self.write({'status': {'msg': 'success', "RetCode": 200},
  519. 'local_ad_plan_info': result_})
  520. class get_all_ad_task(BaseHandler):
  521. def get(self):
  522. user_id = self.get_argument('user_id', None)
  523. sql_session = db.DBSession()
  524. if user_id is None:
  525. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  526. return
  527. # 落地页名字精确到毫秒,默认是全局唯一
  528. result = sql_tools.get_ad_task(sql_session=sql_session, user_id=user_id)
  529. task_dict = {}
  530. localtion = ['wechat', '']
  531. for _ in result:
  532. task_name, status, task_status_num, create_time, typesetting = _
  533. typesetting = json.loads(typesetting)
  534. if typesetting['plan_base'][1] == 'pyq':
  535. localtion[1] = 'pyq'
  536. create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
  537. if task_name not in task_dict.keys():
  538. task_dict[task_name] = {}
  539. task_dict[task_name][status] = (task_status_num, create_time)
  540. result_ = []
  541. num = 0
  542. for k, v in task_dict.items():
  543. # TODO:修改为dict的sort
  544. sum_num = 0
  545. new_dict = {}
  546. create_time = None
  547. for k_, v_ in v.items():
  548. task_status_num, create_time = v_
  549. sum_num = sum_num + task_status_num
  550. new_dict[k_] = task_status_num
  551. status = 'todo' if 'todo' in new_dict.keys() else 'done'
  552. task_dict[k]['sum_num'] = sum_num
  553. new_dict['sum_num'] = sum_num
  554. result_.append(
  555. {'task_name': k, 'task_info': new_dict, 'create_time': create_time, 'channel': localtion[0],
  556. 'localtion': localtion[1], 'id': num, 'status': status})
  557. num = num + 1
  558. self.write({'status': {'msg': 'success', "RetCode": 200},
  559. 'local_ad_plan_info': result_})
  560. def heart_jump():
  561. # TODO:tornado 心跳检测,下周做----线程不断检查,线程生命周期60分钟
  562. pass
  563. def make_app():
  564. return tornado.web.Application([
  565. ("/get_all_ad_task", get_all_ad_task), # 获取所有任务状态,
  566. ("/create_ad_plan", create_ad_plan), #
  567. ("/get_ad_wechat_service_name", get_ad_wechat_service_name),
  568. ("/get_ad_wechat_wechat_name", get_ad_wechat_wechat_name),
  569. # ("/create_ad_plan_local", create_ad_plan_local),
  570. ("/create_ad_layout_local", create_ad_layout_local),
  571. ("/get_layout_local", get_ad_layout_local),
  572. ("/get_ad_plan_local", get_ad_plan_local),
  573. ("/delete_layout_local", delete_ad_layout),
  574. ("/delete_ad_plan_local", delete_ad_plan),
  575. ("/get_scan_status", get_scan_status),
  576. # ("/create_ad_layout_remote", create_ad_layout_remote),
  577. ("/ad_human_info", ad_human_info),
  578. ("/ad_wechat_info", ad_wechat_info),
  579. ("/get_plan_action_record", get_plan_action_record),
  580. ], debug=True, autoreload=True)
  581. if __name__ == "__main__":
  582. import logging
  583. logging.basicConfig(
  584. handlers=[
  585. logging.handlers.RotatingFileHandler('./tornado.log',
  586. maxBytes=10 * 1024 * 1024,
  587. backupCount=5,
  588. encoding='utf-8')
  589. , logging.StreamHandler() # 供输出使用
  590. ],
  591. level=logging.INFO,
  592. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  593. )
  594. handler = logging.FileHandler('tornado.log')
  595. logger = logging.getLogger()
  596. logger.addHandler(handler)
  597. logger.setLevel(logging.INFO)
  598. app = make_app()
  599. app.listen(8888)
  600. tornado.ioloop.IOLoop.current().start()