fetch.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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("ProxyFetcher : start")
  31. for fetch_name in self.conf.fetchers:
  32. self.log.info("ProxyFetcher - {func}: start".format(func=fetch_name))
  33. fetcher = getattr(ProxyFetcher, fetch_name, None)
  34. if not fetcher:
  35. self.log.error("ProxyFetcher - {func}: class method not exists!")
  36. continue
  37. if not callable(fetcher):
  38. self.log.error("ProxyFetcher - {func}: must be class method")
  39. continue
  40. try:
  41. for proxy in fetcher():
  42. proxy = proxy.strip()
  43. if not proxy or not verifyProxyFormat(proxy):
  44. self.log.error('ProxyFetch - {func}: '
  45. '{proxy} illegal'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  46. continue
  47. elif proxy in proxy_set:
  48. self.log.info('ProxyFetch - {func}: '
  49. '{proxy} exist'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  50. continue
  51. else:
  52. self.log.info('ProxyFetch - {func}: '
  53. '{proxy} success'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  54. self.db.put(Proxy(proxy, source=proxyGetter))
  55. proxy_set.add(proxy)
  56. except Exception as e:
  57. self.log.error("ProxyFetch - {func}: error".format(func=proxyGetter))
  58. self.log.error(str(e))
  59. if __name__ == '__main__':
  60. a = callable(getattr(ProxyFetcher, 'freeProxy01'))
  61. pass