tornado_api.py 30 KB

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