ProxyManager.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: ProxyManager.py
  6. Description :
  7. Author : JHao
  8. date: 2016/12/3
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/3:
  12. -------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. import random
  16. from ProxyHelper import Proxy
  17. from DB.DbClient import DbClient
  18. from Config.ConfigGetter import config
  19. from Util.LogHandler import LogHandler
  20. from Util.utilFunction import verifyProxyFormat
  21. from ProxyGetter.getFreeProxy import GetFreeProxy
  22. class ProxyManager(object):
  23. """
  24. ProxyManager
  25. """
  26. def __init__(self):
  27. self.db = DbClient()
  28. self.raw_proxy_queue = 'raw_proxy'
  29. self.log = LogHandler('proxy_manager')
  30. self.useful_proxy_queue = 'useful_proxy'
  31. def fetch(self):
  32. """
  33. fetch proxy into db by ProxyGetter
  34. :return:
  35. """
  36. self.db.changeTable(self.raw_proxy_queue)
  37. proxy_set = set()
  38. self.log.info("ProxyFetch : start")
  39. for proxyGetter in config.proxy_getter_functions:
  40. self.log.info("ProxyFetch - {func}: start".format(func=proxyGetter))
  41. try:
  42. for proxy in getattr(GetFreeProxy, proxyGetter.strip())():
  43. proxy = proxy.strip()
  44. if not proxy or not verifyProxyFormat(proxy):
  45. self.log.error('ProxyFetch - {func}: '
  46. '{proxy} illegal'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  47. continue
  48. elif proxy in proxy_set:
  49. self.log.info('ProxyFetch - {func}: '
  50. '{proxy} exist'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  51. continue
  52. else:
  53. self.log.info('ProxyFetch - {func}: '
  54. '{proxy} success'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  55. self.db.put(Proxy(proxy, source=proxyGetter))
  56. proxy_set.add(proxy)
  57. except Exception as e:
  58. self.log.error("ProxyFetch - {func}: error".format(func=proxyGetter))
  59. self.log.error(str(e))
  60. def get(self):
  61. """
  62. return a useful proxy
  63. :return:
  64. """
  65. self.db.changeTable(self.useful_proxy_queue)
  66. item_list = self.db.getAll()
  67. if item_list:
  68. random_choice = random.choice(item_list)
  69. return Proxy.newProxyFromJson(random_choice)
  70. return None
  71. def delete(self, proxy_str):
  72. """
  73. delete proxy from pool
  74. :param proxy_str:
  75. :return:
  76. """
  77. self.db.changeTable(self.useful_proxy_queue)
  78. self.db.delete(proxy_str)
  79. def getAll(self):
  80. """
  81. get all proxy from pool as list
  82. :return:
  83. """
  84. self.db.changeTable(self.useful_proxy_queue)
  85. item_list = self.db.getAll()
  86. return [Proxy.newProxyFromJson(_) for _ in item_list]
  87. def getNumber(self):
  88. self.db.changeTable(self.raw_proxy_queue)
  89. total_raw_proxy = self.db.getNumber()
  90. self.db.changeTable(self.useful_proxy_queue)
  91. total_useful_queue = self.db.getNumber()
  92. return {'raw_proxy': total_raw_proxy, 'useful_proxy': total_useful_queue}
  93. if __name__ == '__main__':
  94. pp = ProxyManager()
  95. pp.fetch()