proxyHandler.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: ProxyHandler.py
  5. Description :
  6. Author : JHao
  7. date: 2016/12/3
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/3:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. from helper.proxy import Proxy
  15. from db.dbClient import DbClient
  16. from handler.configHandler import ConfigHandler
  17. class ProxyHandler(object):
  18. """ Proxy CRUD operator"""
  19. def __init__(self):
  20. self.conf = ConfigHandler()
  21. self.db = DbClient(self.conf.dbConn)
  22. def get(self):
  23. """
  24. return a useful proxy
  25. :return:
  26. """
  27. self.db.changeTable(self.conf.useProxy)
  28. proxy = self.db.get()
  29. if proxy:
  30. return Proxy.newProxyFromJson(proxy)
  31. return None
  32. def pop(self):
  33. """
  34. return and delete a useful proxy
  35. :return:
  36. """
  37. self.db.changeTable(self.conf.useProxy)
  38. proxy = self.db.pop()
  39. if proxy:
  40. return Proxy.newProxyFromJson(proxy)
  41. return None
  42. def delete(self, proxy_str):
  43. """
  44. delete useful proxy
  45. :param proxy_str:
  46. :return:
  47. """
  48. self.db.changeTable(self.conf.useProxy)
  49. return self.db.delete(proxy_str)
  50. def getAll(self):
  51. """
  52. get all proxy from pool as Proxy list
  53. :return:
  54. """
  55. self.db.changeTable(self.conf.useProxy)
  56. proxies_dict = self.db.getAll()
  57. return [Proxy.newProxyFromJson(value) for _, value in proxies_dict.items()]
  58. def exists(self, proxy_str):
  59. """
  60. check proxy exists
  61. :param proxy_str:
  62. :return:
  63. """
  64. self.db.changeTable(self.conf.useProxy)
  65. return self.db.exists(proxy_str)
  66. def getCount(self):
  67. """
  68. return raw_proxy and use_proxy count
  69. :return:
  70. """
  71. self.db.changeTable(self.conf.useProxy)
  72. total_raw_proxy = self.db.getCount()
  73. self.db.changeTable(self.conf.rawProxy)
  74. total_use_proxy = self.db.getCount()
  75. return {'raw_proxy': total_raw_proxy, 'use_proxy': total_use_proxy}