| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- # -*- coding: utf-8 -*-
- """
- -------------------------------------------------
- File Name: ProxyHandler.py
- Description :
- Author : JHao
- date: 2016/12/3
- -------------------------------------------------
- Change Activity:
- 2016/12/3:
- -------------------------------------------------
- """
- __author__ = 'JHao'
- from helper.proxy import Proxy
- from db.dbClient import DbClient
- from handler.configHandler import ConfigHandler
- class ProxyHandler(object):
- """ Proxy CRUD operator"""
- def __init__(self):
- self.conf = ConfigHandler()
- self.db = DbClient(self.conf.dbConn)
- self.db.changeTable(self.conf.tableName)
- def get(self):
- """
- return a useful proxy
- :return:
- """
- proxy = self.db.get()
- if proxy:
- return Proxy.createFromJson(proxy)
- return None
- def pop(self):
- """
- return and delete a useful proxy
- :return:
- """
- proxy = self.db.pop()
- if proxy:
- return Proxy.createFromJson(proxy)
- return None
- def put(self, proxy):
- """
- put proxy into use proxy
- :return:
- """
- self.db.put(proxy)
- def delete(self, proxy):
- """
- delete useful proxy
- :param proxy:
- :return:
- """
- return self.db.delete(proxy.proxy)
- def getAll(self):
- """
- get all proxy from pool as Proxy list
- :return:
- """
- proxies_dict = self.db.getAll()
- return [Proxy.createFromJson(value) for _, value in proxies_dict.items()]
- def exists(self, proxy):
- """
- check proxy exists
- :param proxy:
- :return:
- """
- return self.db.exists(proxy.proxy)
- def getCount(self):
- """
- return raw_proxy and use_proxy count
- :return:
- """
- total_use_proxy = self.db.getCount()
- return {'count': total_use_proxy}
|