ProxyRefreshSchedule.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: ProxyRefreshSchedule.py
  6. Description : 代理定时刷新
  7. Author : JHao
  8. date: 2016/12/4
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/4: 代理定时刷新
  12. 2017/03/06: 使用LogHandler添加日志
  13. 2017/04/26: raw_proxy_queue验证通过但useful_proxy_queue中已经存在的代理不在放入
  14. -------------------------------------------------
  15. """
  16. import sys
  17. import time
  18. import logging
  19. from threading import Thread
  20. # 使用后台调度,不使用阻塞式~
  21. from apscheduler.schedulers.background import BackgroundScheduler as Sch
  22. sys.path.append('../')
  23. from Util.utilFunction import validUsefulProxy
  24. from Manager.ProxyManager import ProxyManager
  25. from Util.LogHandler import LogHandler
  26. __author__ = 'JHao'
  27. logging.basicConfig()
  28. class ProxyRefreshSchedule(ProxyManager):
  29. """
  30. 代理定时刷新
  31. """
  32. def __init__(self):
  33. ProxyManager.__init__(self)
  34. self.log = LogHandler('refresh_schedule')
  35. def validProxy(self):
  36. """
  37. 验证raw_proxy_queue中的代理, 将可用的代理放入useful_proxy_queue
  38. :return:
  39. """
  40. self.db.changeTable(self.raw_proxy_queue)
  41. raw_proxy_item = self.db.pop()
  42. self.log.info('ProxyRefreshSchedule: %s start validProxy' % time.ctime())
  43. # 计算剩余代理,用来减少重复计算
  44. remaining_proxies = self.getAll()
  45. while raw_proxy_item:
  46. raw_proxy = raw_proxy_item.get('proxy')
  47. if isinstance(raw_proxy, bytes):
  48. # 兼容Py3
  49. raw_proxy = raw_proxy.decode('utf8')
  50. if (raw_proxy not in remaining_proxies) and validUsefulProxy(raw_proxy):
  51. self.db.changeTable(self.useful_proxy_queue)
  52. self.db.put(raw_proxy)
  53. self.log.info('ProxyRefreshSchedule: %s validation pass' % raw_proxy)
  54. else:
  55. self.log.info('ProxyRefreshSchedule: %s validation fail' % raw_proxy)
  56. self.db.changeTable(self.raw_proxy_queue)
  57. raw_proxy_item = self.db.pop()
  58. remaining_proxies = self.getAll()
  59. self.log.info('ProxyRefreshSchedule: %s validProxy complete' % time.ctime())
  60. def refreshPool():
  61. pp = ProxyRefreshSchedule()
  62. pp.validProxy()
  63. def batch_refresh(process_num=30):
  64. # 检验新代理
  65. pl = []
  66. for num in range(process_num):
  67. proc = Thread(target=refreshPool, args=())
  68. pl.append(proc)
  69. for num in range(process_num):
  70. pl[num].daemon = True
  71. pl[num].start()
  72. for num in range(process_num):
  73. pl[num].join()
  74. def fetch_all():
  75. p = ProxyRefreshSchedule()
  76. # 获取新代理
  77. p.refresh()
  78. def run():
  79. sch = Sch()
  80. sch.add_job(fetch_all, 'interval', minutes=5) # 每5分钟抓取一次
  81. sch.add_job(batch_refresh, "interval", minutes=1) # 每分钟检查一次
  82. sch.start()
  83. fetch_all()
  84. while True:
  85. time.sleep(1)
  86. if __name__ == '__main__':
  87. run()