ProxyManager.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 helper 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 fetcher.getFreeProxy import GetFreeProxy
  22. class ProxyManager(object):
  23. pass
  24. # """
  25. # ProxyManager
  26. # """
  27. #
  28. # def __init__(self):
  29. # self.db = DbClient()
  30. # self.raw_proxy_queue = 'raw_proxy'
  31. # self.log = LogHandler('proxy_manager')
  32. # self.useful_proxy_queue = 'useful_proxy'
  33. #
  34. # def fetch(self):
  35. # """
  36. # fetch proxy into db by ProxyGetter
  37. # :return:
  38. # """
  39. # self.db.changeTable(self.raw_proxy_queue)
  40. # proxy_set = set()
  41. # self.log.info("ProxyFetch : start")
  42. # for proxyGetter in config.proxy_getter_functions:
  43. # self.log.info("ProxyFetch - {func}: start".format(func=proxyGetter))
  44. # try:
  45. # for proxy in getattr(GetFreeProxy, proxyGetter.strip())():
  46. # proxy = proxy.strip()
  47. #
  48. # if not proxy or not verifyProxyFormat(proxy):
  49. # self.log.error('ProxyFetch - {func}: '
  50. # '{proxy} illegal'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  51. # continue
  52. # elif proxy in proxy_set:
  53. # self.log.info('ProxyFetch - {func}: '
  54. # '{proxy} exist'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  55. # continue
  56. # else:
  57. # self.log.info('ProxyFetch - {func}: '
  58. # '{proxy} success'.format(func=proxyGetter, proxy=proxy.ljust(20)))
  59. # self.db.put(Proxy(proxy, source=proxyGetter))
  60. # proxy_set.add(proxy)
  61. # except Exception as e:
  62. # self.log.error("ProxyFetch - {func}: error".format(func=proxyGetter))
  63. # self.log.error(str(e))
  64. #
  65. # def get(self):
  66. # """
  67. # return a useful proxy
  68. # :return:
  69. # """
  70. # self.db.changeTable(self.useful_proxy_queue)
  71. # item_list = self.db.getAll()
  72. # if item_list:
  73. # random_choice = random.choice(item_list)
  74. # return Proxy.newProxyFromJson(random_choice)
  75. # return None
  76. #
  77. # def delete(self, proxy_str):
  78. # """
  79. # delete proxy from pool
  80. # :param proxy_str:
  81. # :return:
  82. # """
  83. # self.db.changeTable(self.useful_proxy_queue)
  84. # self.db.delete(proxy_str)
  85. #
  86. # def getAll(self):
  87. # """
  88. # get all proxy from pool as list
  89. # :return:
  90. # """
  91. # self.db.changeTable(self.useful_proxy_queue)
  92. # item_list = self.db.getAll()
  93. # return [Proxy.newProxyFromJson(_) for _ in item_list]
  94. #
  95. # def getNumber(self):
  96. # self.db.changeTable(self.raw_proxy_queue)
  97. # total_raw_proxy = self.db.getNumber()
  98. # self.db.changeTable(self.useful_proxy_queue)
  99. # total_useful_queue = self.db.getNumber()
  100. # return {'raw_proxy': total_raw_proxy, 'useful_proxy': total_useful_queue}
  101. # if __name__ == '__main__':
  102. # pp = ProxyManager()
  103. # pp.fetch()