proxyHandler.py 1.9 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. self.db.changeTable(self.conf.tableName)
  23. def get(self):
  24. """
  25. return a useful proxy
  26. :return:
  27. """
  28. proxy = self.db.get()
  29. if proxy:
  30. return Proxy.createFromJson(proxy)
  31. return None
  32. def pop(self):
  33. """
  34. return and delete a useful proxy
  35. :return:
  36. """
  37. proxy = self.db.pop()
  38. if proxy:
  39. return Proxy.createFromJson(proxy)
  40. return None
  41. def put(self, proxy):
  42. """
  43. put proxy into use proxy
  44. :return:
  45. """
  46. self.db.put(proxy)
  47. def delete(self, proxy):
  48. """
  49. delete useful proxy
  50. :param proxy:
  51. :return:
  52. """
  53. return self.db.delete(proxy.proxy)
  54. def getAll(self):
  55. """
  56. get all proxy from pool as Proxy list
  57. :return:
  58. """
  59. proxies_dict = self.db.getAll()
  60. return [Proxy.createFromJson(value) for _, value in proxies_dict.items()]
  61. def exists(self, proxy):
  62. """
  63. check proxy exists
  64. :param proxy:
  65. :return:
  66. """
  67. return self.db.exists(proxy.proxy)
  68. def getCount(self):
  69. """
  70. return raw_proxy and use_proxy count
  71. :return:
  72. """
  73. total_use_proxy = self.db.getCount()
  74. return {'count': total_use_proxy}