off_line_controler.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import sys
  3. import time
  4. import schedule
  5. import multiprocessing
  6. sys.path.append(os.getcwd())
  7. from scheduler import SpiderHome
  8. class SpiderScheduler(object):
  9. """
  10. 线下爬虫调度器
  11. """
  12. SH = SpiderHome()
  13. @classmethod
  14. def protect_spider_timeout(cls, function, hour):
  15. """
  16. 守护进程,在程序启动后的某一个时段内守护爬虫进程
  17. :param function: 被守护的函数
  18. :param hour: 守护时长 / hour
  19. """
  20. run_time_limit = hour * 3600
  21. start_time = time.time()
  22. process = multiprocessing.Process(target=function)
  23. process.start()
  24. while True:
  25. if time.time() - start_time >= run_time_limit:
  26. process.terminate()
  27. break
  28. if not process.is_alive():
  29. process.terminate()
  30. time.sleep(60)
  31. os.system("adb forward --remove-all")
  32. process = multiprocessing.Process(target=function)
  33. process.start()
  34. time.sleep(60)
  35. @classmethod
  36. def run_xng_plus(cls, hour):
  37. """
  38. “小年糕+”推荐爬虫
  39. :param hour:
  40. """
  41. cls.protect_spider_timeout(function=cls.SH.run_xng_plus, hour=hour)
  42. @classmethod
  43. def run_zhuFuQuanZi(cls):
  44. print("hello")
  45. @classmethod
  46. def run_spss(cls, hour):
  47. """
  48. "视频刷刷"推荐爬虫
  49. :param hour:
  50. """
  51. cls.protect_spider_timeout(function=cls.SH.run_spss, hour=hour)
  52. @classmethod
  53. def run_zfhybf(cls, hour):
  54. """
  55. "祝福好运暴富"推荐爬虫
  56. :param hour:
  57. """
  58. cls.protect_spider_timeout(function=cls.SH.run_zfhybf, hour=hour)
  59. @classmethod
  60. def run_spss_id(cls, hour):
  61. cls.protect_spider_timeout(function=cls.SH.run_spss_id, hour=hour)
  62. @classmethod
  63. def run_xng_id(cls, hour):
  64. cls.protect_spider_timeout(function=cls.SH.run_xng_id, hour=hour)
  65. if __name__ == "__main__":
  66. SC = SpiderScheduler()
  67. SC.run_xng_plus(hour=100)
  68. # SC.run_spss(hour=5)
  69. # # schedule.every().day.at("20:06").do(SC.run_xng_plus, hour=1)
  70. # schedule.every().day.at("20:30").do(SC.run_spss, hour=1)
  71. #
  72. # schedule.every().day.at("18:30").do(SC.run_spss_id, hour=1)
  73. #
  74. # while True:
  75. # schedule.run_pending()