abuyun_server.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/local/bin/python3
  2. # -*- coding:utf-8 -*-
  3. # @Time : 2018/5/18 3:18 PM
  4. # @Author : Swing
  5. from tornado.web import RequestHandler
  6. import abuyun_renew
  7. import statistics
  8. import tornado.ioloop
  9. import email_util
  10. import traceback
  11. import json
  12. import loop_task
  13. class MainHandler(RequestHandler):
  14. def get(self, *args, **kwargs):
  15. self.write('Hello world')
  16. def post(self):
  17. self.write('Hello world')
  18. class OpenChrome(RequestHandler):
  19. def get(self, *args, **kwargs):
  20. abuyun_renew.open_chrome()
  21. self.write('Hello world')
  22. def post(self):
  23. self.write('Hello world')
  24. class ReCharge(RequestHandler):
  25. def get(self, *args, **kwargs):
  26. try:
  27. abuyun_renew.recharge(is_launch_spider=True)
  28. loop_task.loop_start()
  29. self.finish(Response().to_json())
  30. except:
  31. email_util.send_email('Abuyun renew error', traceback.format_exc())
  32. print('Daily statistic err! reason: ' + traceback.format_exc())
  33. self.finish(Response(False, traceback.format_exc()).to_json())
  34. class DailyStatistic(RequestHandler):
  35. def get(self, *args, **kwargs):
  36. try:
  37. statistics.daily_statistic()
  38. self.finish(Response().to_json())
  39. except:
  40. email_util.send_email('Daily statistic err', traceback.format_exc())
  41. print('Daily statistic err! reason: ' + traceback.format_exc())
  42. self.finish(Response(False, traceback.format_exc()).to_json())
  43. class LoopTaskStart(RequestHandler):
  44. def get(self, *args, **kwargs):
  45. try:
  46. loop_task.loop_start()
  47. self.finish(Response().to_json())
  48. except:
  49. print(traceback.format_exc())
  50. class LoopTaskStop(RequestHandler):
  51. def get(self, *args, **kwargs):
  52. try:
  53. loop_task.loop_stop()
  54. self.finish(Response().to_json())
  55. except:
  56. print(traceback.format_exc())
  57. class Response(object):
  58. def __init__(self, success=True, message='ok'):
  59. self.success = success
  60. self.message = message
  61. def to_json(self):
  62. return json.dumps(self, default=lambda obj: obj.__dict__, sort_keys=True, indent=4)
  63. application = tornado.web.Application([
  64. (r"/openChrome", OpenChrome),
  65. (r'/reCharge', ReCharge),
  66. (r'/statistic', DailyStatistic),
  67. (r'/loopStart', LoopTaskStart),
  68. (r'/loopStop', LoopTaskStop),
  69. ])
  70. if __name__ == "__main__":
  71. application.listen(8888)
  72. # print('Tornado start at port: 8888')
  73. tornado.ioloop.IOLoop.instance().start()