__author__ = 'root'
import request_class;
from db_mysql_module import SQLiteWraper;
import traceback;
import random;
import time;
import math;
import threading;
import concurrent.futures.thread as thread

class proxypool(object):
    def __init__(self,username,password,host,dbname,loadpool=True,harfile=None):
        self.testurl = "http://www.kuaidaili.com/free/inha/";
        self.pool = [];
        self.availablepool=[];
        self.copiedpool=[];
        self.harfile = harfile;
        self.session = request_class.getsession(harfile=harfile);
        # self.proxypool=[];
        self.db = SQLiteWraper(username,password,host,dbname);
        if loadpool:
            self.loadpooltomem();

    def loadpooltomem(self):
        try:
            print("loading proxy to mem")
            availableresult = self.db.select("select * from proxylist where enabled = 1 and costtime <4");
            result = self.db.select("select * from proxylist");
            for record in result:
                self.pool.append({"http":"http://"+record["ip"]+":"+record["port"],"id":record["id"],"costtime":record["costtime"]})
                # thistuple = {"http":"http://"+record["ip"]+":"+record["port"],"id":record["id"]},
                # self.proxypool.append(thistuple)
            for record in availableresult:
                self.availablepool.append({"http":"http://"+record["ip"]+":"+record["port"],"id":record["id"],"costtime":record["costtime"]})
                self.copyavailablepool()
        except Exception as e:
            traceback.print_stack()


    def buildpool(self,url):
        # soup = self.loopretryurl(url)
        # if not soup:
        #     soup = request_class.getsoup_by_url(url)
        soup = self.retryurl(6,url)
        return soup;

    def loopretryurl(self,url):
        for proxyobj in self.copiedpool:
            try:
                soup = request_class.getsoup_by_url(url,proxy=proxyobj["http"],timeout=proxyobj["costtime"]+2,harfile=self.harfile)
                print("+++++++++++++++++++++++++++++++++++++++++++++++")
                print("using proxy http ip=============="+proxyobj["http"])
                print("+++++++++++++++++++++++++++++++++++++++++++++++")
                return soup;
            except Exception as e:
                continue
        return None;