tornado_api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. ad_plan_list = request_dict['planList']
  102. user_id = request_dict['userId']
  103. # 2.2存行为记录
  104. task_name = 'user_id: {user_id} time:{time_sign} action:create_plan'.format(user_id=user_id,
  105. time_sign=datetime.now().strftime(
  106. "%Y-%m-%d, %H:%M:%S"))
  107. # 4.开始运行
  108. if not self.check_task(user_id=user_id):
  109. # 1.查看是否cookie可用
  110. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  111. self.save_task_info(user_id, ad_plan_list, sql_session, task_name)
  112. threading.Thread(target=user_action.carry_plan,
  113. args=(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name)).start()
  114. else:
  115. self.save_task_info(user_id, ad_plan_list, sql_session, task_name)
  116. self.write({'status': {'msg': 'success', "RetCode": 200}})
  117. except Exception as e:
  118. if log_ad:
  119. log_ad.driver.quit()
  120. logging.error(str(e))
  121. self.write('error')
  122. finally:
  123. sql_session.commit()
  124. class get_ad_plan_local(BaseHandler):
  125. def get(self):
  126. user_id = self.get_argument('user_id', None)
  127. layout_name = self.get_argument('plan_name', None)
  128. sql_session = db.DBSession()
  129. if user_id is None:
  130. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  131. return
  132. # 落地页名字精确到毫秒,默认是全局唯一
  133. if layout_name:
  134. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  135. typesetting_name=layout_name)
  136. else:
  137. # TODO:之后修改一下,让其查询效率高点,like效率过低
  138. layout_name = ''
  139. result = sql_tools.get_plan_typesetting_rough(sql_session=sql_session, user_id=user_id,
  140. typesetting_name=layout_name)
  141. result_ = []
  142. for i in range(len(result)):
  143. typesetting, name, create_time, update_time = result[i]
  144. _ = {}
  145. _['typesetting'] = json.loads(typesetting)
  146. _['ad_plan_name'] = name
  147. _['id'] = i
  148. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  149. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  150. result_.append(_)
  151. self.write({'status': {'msg': 'success', "RetCode": 200},
  152. 'local_ad_plan_info': result_})
  153. class create_ad_layout_local(BaseHandler):
  154. def post(self):
  155. # TODO:返回一个layout_name重复的一个信息
  156. request_dict = json.loads(self.request.body)
  157. user_id = request_dict['user_id']
  158. layout_typesetting = request_dict['layout_typesetting']
  159. layout_name = request_dict['layout_name']
  160. sql_session = db.DBSession()
  161. if user_id is None or layout_name is None or layout_typesetting is None:
  162. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  163. return
  164. # 落地页名字精确到毫秒,默认是全局唯一
  165. layout_typesetting_info = {'user_id': user_id, 'name': layout_name,
  166. 'typesetting': layout_typesetting}
  167. layout_typesetting_inserte = sql_tools.save_layout_typesetting_info(
  168. layout_typesetting_info=layout_typesetting_info,
  169. table_layout_typesetting=layout_typesetting_table)
  170. sql_session.execute(layout_typesetting_inserte)
  171. sql_session.commit()
  172. self.write({'status': {'msg': 'success', "RetCode": 200}})
  173. class get_ad_layout_local(BaseHandler):
  174. def get(self):
  175. user_id = self.get_argument('user_id', None)
  176. layout_name = self.get_argument('layout_name', None)
  177. sql_session = db.DBSession()
  178. if user_id is None:
  179. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  180. return
  181. # 落地页名字精确到毫秒,默认是全局唯一
  182. if layout_name:
  183. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  184. typesetting_name=layout_name)
  185. else:
  186. # TODO:之后修改一下,让其查询效率高点,like效率过低
  187. layout_name = ''
  188. result = sql_tools.get_layout_typesetting_rough(sql_session=sql_session, user_id=user_id,
  189. typesetting_name=layout_name)
  190. result_ = []
  191. for i in range(len(result)):
  192. typesetting, name, create_time, update_time = result[i]
  193. _ = {}
  194. _['typesetting'] = json.loads(typesetting)
  195. _['layout_name'] = name
  196. _['id'] = i
  197. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  198. _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
  199. result_.append(_)
  200. self.write({'status': {'msg': 'success', "RetCode": 200},
  201. 'local_layout_info': result_})
  202. class get_scan_status(BaseHandler):
  203. # 获取到扫码状态
  204. def get(self):
  205. sql_session = db.DBSession()
  206. user_id = self.get_argument("user_id", None)
  207. status = sql_tools.get_scan_action_status(user_id, sql_session)
  208. if user_id is None:
  209. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  210. return
  211. self.write({'status': {'msg': 'success', "RetCode": 200},
  212. 'scan_action_status': status})
  213. # TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
  214. class ad_human_info(BaseHandler):
  215. @staticmethod
  216. def refresh_wechat_cookies(tornado_web, user_id):
  217. # 1.返回二维码链接
  218. # ----1.查看cookie是否可用
  219. sql_session = db.DBSession()
  220. cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
  221. # 进行登录操作
  222. log_ad = LogIn(user_id=user_id)
  223. # 使driver可以使用
  224. cookie_canuse = False
  225. if cookie_db:
  226. cookie_db = pickle.loads(cookie_db)
  227. if not log_ad.wechat_cookies_check_alive(cookie_db):
  228. # cookie 不能使用
  229. wechat_code = log_ad.log_in()
  230. sql_tools.update_user_scan_action(user_id, sql_session)
  231. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  232. 'wechat_code': wechat_code})
  233. logging.info('cookie can not use')
  234. else:
  235. # cookie 可以继续使用
  236. cookie_canuse = True
  237. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  238. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  239. else:
  240. # cookie 不能使用
  241. wechat_code = log_ad.log_in()
  242. sql_tools.update_user_scan_action(user_id, sql_session)
  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. log_ad = None
  250. try:
  251. # 0.是否刷新
  252. # 1.获取userid,以及是否刷新
  253. user_id = self.get_argument("user_id", None)
  254. human_package_name = self.get_argument('human_package_name', None)
  255. is_refresh = self.get_argument("is_refresh", None)
  256. wechat_name = self.get_argument('wechat_name', None)
  257. service_name = self.get_argument('service_name', None)
  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. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  264. if not create_ad_plan.check_task(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, task_name)).start()
  284. else:
  285. logging.info('任务有堆积')
  286. return
  287. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  288. else:
  289. # 1.查看是否在刷新,
  290. # 在刷新中,
  291. # 返回正在刷新
  292. # -------不管上面逻辑让他们多刷新几次
  293. # 不在刷新
  294. # 返回对应数据
  295. # 2.获取userid对应数据
  296. result = sql_tools.get_human_info(sql_session=sql_session,
  297. service_name=service_name, wechat_name=wechat_name)
  298. result = json.loads(result)
  299. if human_package_name:
  300. result = [_ for _ in result if human_package_name in _['name']]
  301. result_ = []
  302. for i in range(len(result)):
  303. _ = result[i]
  304. _['id'] = i
  305. result_.append(_)
  306. self.write({'status': {'msg': 'success', "RetCode": 200},
  307. 'human_info': result})
  308. except Exception as e:
  309. if log_ad:
  310. log_ad.driver.quit()
  311. logging.error(str(e))
  312. finally:
  313. sql_session.commit()
  314. class refresh_wechat_info(BaseHandler):
  315. # TODO:刷新以及创建,限时3分钟
  316. @staticmethod
  317. def refresh_wechat_cookies(tornado_web, user_id):
  318. # 1.返回二维码链接
  319. # ----1.查看cookie是否可用
  320. sql_session = db.DBSession()
  321. cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
  322. # 进行登录操作
  323. log_ad = LogIn(user_id=user_id)
  324. # 使driver可以使用
  325. cookie_canuse = False
  326. if cookie_db:
  327. cookie_db = pickle.loads(cookie_db)
  328. if not log_ad.wechat_cookies_check_alive(cookie_db):
  329. # cookie 不能使用
  330. wechat_code = log_ad.log_in()
  331. sql_tools.update_user_scan_action(user_id, sql_session)
  332. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  333. 'wechat_code': wechat_code})
  334. logging.info('cookie can not use')
  335. else:
  336. # cookie 可以继续使用
  337. cookie_canuse = True
  338. log_ad.driver.get('https://a.weixin.qq.com/index.html')
  339. tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
  340. else:
  341. # cookie 不能使用
  342. wechat_code = log_ad.log_in()
  343. sql_tools.update_user_scan_action(user_id, sql_session)
  344. tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
  345. 'wechat_code': wechat_code})
  346. return log_ad, cookie_canuse
  347. def get(self):
  348. sql_session = db.DBSession()
  349. log_ad = None
  350. try:
  351. user_id = self.get_argument("user_id", None)
  352. log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
  353. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  354. user_id=user_id,
  355. time_sign=datetime.now().strftime(
  356. "%Y-%m-%d, %H:%M:%S"))
  357. # 行为记录
  358. action_type = refresh_wechat_action
  359. object_name = ''
  360. service_name = ''
  361. wechat_name = ''
  362. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  363. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  364. 'status': 'todo'}
  365. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  366. table_action_record=action_record_table)
  367. sql_session.execute(record_insert)
  368. sql_session.commit()
  369. if not create_ad_plan.check_task(user_id=user_id):
  370. threading.Thread(target=user_action.get_human_info,
  371. args=(
  372. user_id, log_ad, db, cookie_canuse, task_name)).start()
  373. else:
  374. return
  375. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  376. except:
  377. pass
  378. class ad_wechat_info(BaseHandler):
  379. # 1.公众号相关信息获取
  380. def get(self):
  381. sql_session = db.DBSession()
  382. log_ad = None
  383. try:
  384. # 0.是否刷新
  385. # 1.获取userid,以及是否刷新
  386. user_id = self.get_argument("userId", None)
  387. is_refresh = self.get_argument("isRefresh", None)
  388. if user_id is None or is_refresh is None:
  389. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  390. return
  391. if int(is_refresh) == 1:
  392. # 检查有无其他任务在处理中,有则等待
  393. log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
  394. if not create_ad_plan.check_task(user_id=user_id):
  395. task_name = 'user_id: {user_id} time:{time_sign} action:refresh_wechat_info'.format(
  396. user_id=user_id,
  397. time_sign=datetime.now().strftime(
  398. "%Y-%m-%d, %H:%M:%S"))
  399. # 行为记录
  400. action_type = refresh_wechat_action
  401. object_name = ''
  402. service_name = ''
  403. wechat_name = ''
  404. action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
  405. 'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
  406. 'status': 'todo'}
  407. record_insert = sql_tools.save_action_record(action_record_info=action_info,
  408. table_action_record=action_record_table)
  409. sql_session.execute(record_insert)
  410. sql_session.commit()
  411. threading.Thread(target=user_action.get_human_info,
  412. args=(
  413. user_id, log_ad, db, cookie_canuse, task_name)).start()
  414. else:
  415. return
  416. self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
  417. else:
  418. result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
  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. self.write({'status': {'msg': 'success', "RetCode": 200},
  424. 'wechat_info': result_list})
  425. except Exception as e:
  426. if log_ad:
  427. log_ad.driver.quit()
  428. logging.error(str(e))
  429. finally:
  430. sql_session.commit()
  431. class delete_ad_layout(BaseHandler):
  432. def get(self):
  433. user_id = self.get_argument('user_id', None)
  434. layout_name = self.get_argument('layout_name', None)
  435. sql_session = db.DBSession()
  436. if user_id is None or layout_name is None:
  437. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  438. return
  439. # 落地页名字精确到毫秒,默认是全局唯一
  440. sql_tools.delete_layout_typesetting_vir(sql_session=sql_session, user_id=user_id,
  441. typesetting_name=layout_name)
  442. self.write({'status': {'msg': 'success', "RetCode": 200}})
  443. class delete_ad_plan(BaseHandler):
  444. def get(self):
  445. user_id = self.get_argument('user_id', None)
  446. plan_name = self.get_argument('plan_name', None)
  447. service_name = self.get_argument('service_name', None)
  448. wechat_name = self.get_argument('wechat_name', None)
  449. sql_session = db.DBSession()
  450. if user_id is None or plan_name is None:
  451. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  452. return
  453. # 落地页名字精确到毫秒,默认是全局唯一
  454. sql_tools.delete_ad_plan_typesetting_vir(sql_session=sql_session, user_id=user_id,
  455. typesetting_name=plan_name, wechat_name=wechat_name,
  456. service_name=service_name)
  457. self.write({'status': {'msg': 'success', "RetCode": 200}})
  458. class get_ad_wechat_service_name(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. result = sql_tools.get_wechat_info_service_name(sql_session=sql_session, user_id=user_id)
  466. result_list = []
  467. for _ in result:
  468. service_name = _
  469. result_list.append({'service_name': service_name})
  470. self.write({'status': {'msg': 'success', "RetCode": 200},
  471. 'wechat_info': result_list})
  472. class get_ad_wechat_wechat_name(BaseHandler):
  473. def get(self):
  474. user_id = self.get_argument('user_id', None)
  475. service_name = self.get_argument('service_name', None)
  476. sql_session = db.DBSession()
  477. if user_id is None or service_name is None:
  478. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  479. return
  480. result = sql_tools.get_wechat_info_wechat_name(sql_session=sql_session, user_id=user_id,
  481. service_name=service_name)
  482. result_list = []
  483. for _ in result:
  484. service_name, wechat_name = _
  485. result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
  486. self.write({'status': {'msg': 'success', "RetCode": 200},
  487. 'wechat_info': result_list})
  488. class get_plan_action_record(BaseHandler):
  489. def get(self):
  490. user_id = self.get_argument('user_id', None)
  491. service_name = self.get_argument('service_name', None)
  492. wechat_name = self.get_argument('wechat_name', None)
  493. status = self.get_argument('status', None)
  494. plan_name = self.get_argument('plan_name', None)
  495. sql_session = db.DBSession()
  496. if user_id is None:
  497. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  498. return
  499. # 落地页名字精确到毫秒,默认是全局唯一
  500. result = sql_tools.get_plan_record(sql_session=sql_session, user_id=user_id,
  501. service_name=service_name, wechat_name=wechat_name,
  502. status=status, plan_name=plan_name)
  503. result_ = []
  504. for i in range(len(result)):
  505. user_id, name, service_name, wechat_name, create_time, status, typesetting, wechat_id_info = result[i]
  506. _ = {}
  507. _['typesetting'] = json.loads(typesetting)
  508. _['ad_plan_name'] = name
  509. _['id'] = i
  510. _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
  511. _['service_name'] = service_name
  512. _['wechat_name'] = wechat_name
  513. _['wechat_id_info'] = wechat_id_info
  514. _['status'] = status
  515. result_.append(_)
  516. self.write({'status': {'msg': 'success', "RetCode": 200},
  517. 'local_ad_plan_info': result_})
  518. class get_all_ad_task(BaseHandler):
  519. def get(self):
  520. user_id = self.get_argument('user_id', None)
  521. sql_session = db.DBSession()
  522. if user_id is None:
  523. self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
  524. return
  525. # 落地页名字精确到毫秒,默认是全局唯一
  526. result = sql_tools.get_ad_task(sql_session=sql_session, user_id=user_id)
  527. task_dict = {}
  528. localtion = ['wechat', '']
  529. for _ in result:
  530. task_name, status, task_status_num, create_time, typesetting = _
  531. typesetting = json.loads(typesetting)
  532. if typesetting['plan_base'][1] == 'pyq':
  533. localtion[1] = 'pyq'
  534. create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
  535. if task_name not in task_dict.keys():
  536. task_dict[task_name] = {}
  537. task_dict[task_name][status] = (task_status_num, create_time)
  538. result_ = []
  539. num = 0
  540. for k, v in task_dict.items():
  541. # TODO:修改为dict的sort
  542. sum_num = 0
  543. new_dict = {}
  544. create_time = None
  545. for k_, v_ in v.items():
  546. task_status_num, create_time = v_
  547. sum_num = sum_num + task_status_num
  548. new_dict[k_] = task_status_num
  549. status = 'todo' if 'todo' in new_dict.keys() else 'done'
  550. task_dict[k]['sum_num'] = sum_num
  551. new_dict['sum_num'] = sum_num
  552. result_.append(
  553. {'task_name': k, 'task_info': new_dict, 'create_time': create_time, 'channel': localtion[0],
  554. 'localtion': localtion[1], 'id': num, 'status': status})
  555. num = num + 1
  556. self.write({'status': {'msg': 'success', "RetCode": 200},
  557. 'local_ad_plan_info': result_})
  558. def heart_jump():
  559. # TODO:tornado 心跳检测,下周做----线程不断检查,线程生命周期60分钟
  560. pass
  561. def make_app():
  562. return tornado.web.Application([
  563. ("/get_all_ad_task", get_all_ad_task), # 获取所有任务状态,
  564. ("/create_ad_plan", create_ad_plan), #
  565. ("/get_ad_wechat_service_name", get_ad_wechat_service_name),
  566. ("/get_ad_wechat_wechat_name", get_ad_wechat_wechat_name),
  567. # ("/create_ad_plan_local", create_ad_plan_local),
  568. ("/create_ad_layout_local", create_ad_layout_local),
  569. ("/get_layout_local", get_ad_layout_local),
  570. ("/get_ad_plan_local", get_ad_plan_local),
  571. ("/delete_layout_local", delete_ad_layout),
  572. ("/delete_ad_plan_local", delete_ad_plan),
  573. ("/get_scan_status", get_scan_status),
  574. # ("/create_ad_layout_remote", create_ad_layout_remote),
  575. ("/ad_human_info", ad_human_info),
  576. ("/ad_wechat_info", ad_wechat_info),
  577. ("/get_plan_action_record", get_plan_action_record),
  578. ], debug=True, autoreload=True)
  579. if __name__ == "__main__":
  580. import logging
  581. logging.basicConfig(
  582. handlers=[
  583. logging.handlers.RotatingFileHandler('./tornado.log',
  584. maxBytes=10 * 1024 * 1024,
  585. backupCount=5,
  586. encoding='utf-8')
  587. , logging.StreamHandler() # 供输出使用
  588. ],
  589. level=logging.INFO,
  590. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  591. )
  592. handler = logging.FileHandler('tornado.log')
  593. logger = logging.getLogger()
  594. logger.addHandler(handler)
  595. logger.setLevel(logging.INFO)
  596. app = make_app()
  597. app.listen(8888)
  598. tornado.ioloop.IOLoop.current().start()