# Path: app/blog_lab/models.py
# class Proxy(BaseModel):
#     ip = models.CharField(max_length=30, unique=True)
#     online = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.ip


from app.blog_lab.models import Proxy
from app.blog_lab.proxy.Proxy import *
from app.decorater import back_log
from app.decorater import BACK_CHECK_PROXY, BACK_GET_PROXY
# -*- coding: utf-8 -*-

@back_log(BACK_GET_PROXY)
def get_proxy(args = None):
    a = NetProcess()
    a.Host = 'www.hao123.com'
    errmsg = ''
    in_queue = 0
    myproxy = SProxy()
    ip_list = myproxy.renew_proxy()
    newitems = 0
    for item in ip_list:
        try:
            ishave = Proxy.objects.filter(ip = str(item))
            if ishave.count() > 0:
                continue
            a.Proxy = item
            in_queue += 1
            a.coroutine_request('GET', "http://www.hao123.com/", 'utf-8', use_proxy=True)
        except Exception, e:
            continue
    print '%d proxy in queue' % in_queue
    print 'start requests...'
    resp_list = a.coroutine_response(status_only=True)
    for i, itm in enumerate(resp_list):
        ishave = Proxy.objects.filter(ip=str(ip_list[i])).exists()
        if itm and not ishave:

# Path: app/blog_lab/models.py
# class Proxy(BaseModel):
#     ip = models.CharField(max_length=30, unique=True)
#     online = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.ip


# Path: app/blog_lab/proxy/method.py
from app.blog_lab.models import Proxy
from app.blog_lab.proxy.Proxy import *
from app.decorater import back_log
from app.decorater import BACK_CHECK_PROXY, BACK_GET_PROXY

# -*- coding: utf-8 -*-

@back_log(BACK_GET_PROXY)
def get_proxy(args = None):
    a = NetProcess()
    a.Host = 'www.hao123.com'
    errmsg = ''
    in_queue = 0
    myproxy = SProxy()
    ip_list = myproxy.renew_proxy()
    newitems = 0
    for item in ip_list:
        try:
            ishave = Proxy.objects.filter(ip = str(item))
            if ishave.count() > 0:
                continue
            a.Proxy = item
            in_queue += 1
            a.coroutine_request('GET', "http://www.hao123.com/", 'utf-8', use_proxy=True)
        except Exception, e:
            continue
    print '%d proxy in queue' % in_queue
    print 'start requests...'
    resp_list = a.coroutine_response(status_only=True)
    for i, itm in enumerate(resp_list):
        ishave = Proxy.objects.filter(ip=str(ip_list[i])).exists()
        if itm and not ishave:

            new_proxy = Proxy(ip=ip_list[i])