fetch.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: fetchScheduler
  5. Description :
  6. Author : JHao
  7. date: 2019/8/6
  8. -------------------------------------------------
  9. Change Activity:
  10. 2019/08/06:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. from handler.logHandler import LogHandler
  15. from handler.proxyHandler import ProxyHandler
  16. from fetcher.proxyFetcher import ProxyFetcher
  17. from handler.configHandler import ConfigHandler
  18. class Fetcher(object):
  19. name = "fetcher"
  20. def __init__(self):
  21. self.log = LogHandler(self.name)
  22. self.conf = ConfigHandler()
  23. self.proxy_handler = ProxyHandler()
  24. def fetch(self):
  25. """
  26. fetch proxy into db with proxyFetcher
  27. :return:
  28. """
  29. proxy_set = set()
  30. self.log.info("ProxyFetch : start")
  31. for fetch_name in self.conf.fetchers:
  32. self.log.info("ProxyFetch - {func}: start".format(func=fetch_name))
  33. fetcher = getattr(ProxyFetcher, fetch_name, None)
  34. if not fetcher:
  35. self.log.error("ProxyFetch - {func}: class method not exists!")
  36. continue
  37. if not callable(fetcher):
  38. self.log.error("ProxyFetch - {func}: must be class method")
  39. continue
  40. try:
  41. for proxy in fetcher():
  42. if proxy in proxy_set:
  43. self.log.info('ProxyFetch - %s: %s exist' % (fetch_name, proxy.ljust(23)))
  44. continue
  45. else:
  46. self.log.info('ProxyFetch - %s: %s success' % (fetch_name, proxy.ljust(23)))
  47. if proxy.strip():
  48. proxy_set.add(proxy)
  49. except Exception as e:
  50. self.log.error("ProxyFetch - {func}: error".format(func=fetch_name))
  51. self.log.error(str(e))
  52. self.log.info("ProxyFetch - all complete!")
  53. return proxy_set
  54. def runFetcher():
  55. return Fetcher().fetch()