# Path: falsy/netboy/curl_loop.py
# class CurlLoop:
#     class CurlException(Exception):
#         def __init__(self, code, desc, data):
#             self.code = code
#             self.desc = desc
#             self.data = data
# 
#     _multi = pycurl.CurlMulti()
#     _multi.setopt(pycurl.M_PIPELINING, 1)
#     atexit.register(_multi.close)
#     _futures = {}
# 
#     @classmethod
#     async def handler_ready(cls, c):
#         cls._futures[c] = aio.Future()
#         cls._multi.add_handle(c)
#         try:
#             try:
#                 curl_ret = await cls._futures[c]
#             except CurlLoop.CurlException as e:
#                return {
#                     'url': c._raw_url,
#                     'id': c._raw_id,
#                     'payload': c._raw_payload,
#                     'spider': 'pycurl',
#                     'state': 'error',
#                     'error_code': e.code,
#                     'error_desc': e.desc,
#                 }
#             except Exception as e:
#                 return {
#                     'url': c._raw_url,
#                     'id': c._raw_id,
#                     'payload': c._raw_payload,
#                     'spider': 'pycurl',
#                     'state': 'critical',
#                     'error_code': -1,
#                     'error_desc': "{} - {}".format(type(e), str(e)),
#                 }
#             return curl_ret
#         finally:
#             cls._multi.remove_handle(c)
# 
#     @classmethod
#     def perform(cls):
#         if cls._futures:
#             while True:
#                 status, num_active = cls._multi.perform()
#                 if status != pycurl.E_CALL_MULTI_PERFORM:
#                     break
#             while True:
#                 num_ready, success, fail = cls._multi.info_read()
#                 for c in success:
#                     cc = cls._futures.pop(c)
#                     result = curl_result(c)
#                     result['url'] = c._raw_url
#                     result['id'] = c._raw_id
#                     result['state'] = 'normal'
#                     result['spider'] = 'pycurl'
#                     result['payload'] = payload = c._raw_payload
# 
#                     # post_func = payload.get('post_func')
#                     # if type(post_func) == str:
#                     #     post_func = load(post_func)
#                     # if post_func:
#                     #     result = post_func(payload, result)
# 
#                     cc.set_result(result)
#                 for c, err_num, err_msg in fail:
#                     print('error:', err_num, err_msg, c.getinfo(pycurl.EFFECTIVE_URL))
#                     result = curl_result(c)
# 
#                     result['url'] = c._raw_url
#                     result['id'] = c._raw_id
#                     result['state'] = 'error'
#                     result['spider'] = 'pycurl'
#                     result['error_code'] = err_num
#                     result['error_desc'] = err_msg
# 
#                     result['payload'] = payload = c._raw_payload
# 
#                     # post_func = payload.get('post_func')
#                     # if type(post_func) == str:
#                     #     post_func = load(post_func)
#                     # if post_func:
#                     #     result2 = post_func(payload, result)
#                     #     if type(result2) is dict and len(result2) >= len(result):
#                     #         result = result2
#                     cls._futures.pop(c).set_exception(CurlLoop.CurlException(code=err_num, desc=err_msg, data=result))
#                 if num_ready == 0:
#                     break
# 
# Path: falsy/netboy/fetch.py
# async def net_boy(payload, share=None):
#     targets = []
#     for p in payload:
#         if p.get('postfields'):
#             targets.append(post_request(p, share))
#         else:
#             targets.append(get_request(p, share))
#     res = await aio.gather(
#         *targets, return_exceptions=True
#     )
#     return res
# 
# Path: falsy/netboy/run.py
# def run(coro, loop=None):
#     async def main_task():
#         pycurl_task = aio.ensure_future(curl_loop())
#         try:
#             r = await coro
#         finally:
#             pycurl_task.cancel()
#             with suppress(aio.CancelledError):
#                 await pycurl_task
#         return r, pycurl_task
# 
#     if loop is None:
#         loop = uvloop.new_event_loop()
#         # loop = aio.get_event_loop()
#     aio.set_event_loop(loop)
#     loop.set_exception_handler(exception_handler)
#     r, _ = loop.run_until_complete(main_task())
#     return r


import typing
import pycurl
from falsy.netboy.curl_loop import CurlLoop
from falsy.netboy.fetch import net_boy
from falsy.netboy.run import run
                return self[name]
            except KeyError:
                # raise NetBoy.Exception('netboy key error: ' + name)
                return None  # '!netboy key [' + name + '] does not exist'
            except Exception:
                raise NetBoy.Exception('netboy exception: ' + name)

        def __setattr__(self, name, value):
            # type: (str, Any) -> None
            self[name] = value

    def __init__(self, payload=None, share=None):
        self.payload = payload
        if share:
            s = pycurl.CurlShare()
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_COOKIE)
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_DNS)
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_SSL_SESSION)
            self.share = s
        else:
            self.share = None

    def run(self, payload=None, loop=None):
        real_payload = payload
        if self.payload is None:
            real_payload = payload
        elif payload is None:
            real_payload = self.payload
        else:
            real_payload = self.payload + payload

# Path: falsy/netboy/curl_loop.py
# class CurlLoop:
#     class CurlException(Exception):
#         def __init__(self, code, desc, data):
#             self.code = code
#             self.desc = desc
#             self.data = data
# 
#     _multi = pycurl.CurlMulti()
#     _multi.setopt(pycurl.M_PIPELINING, 1)
#     atexit.register(_multi.close)
#     _futures = {}
# 
#     @classmethod
#     async def handler_ready(cls, c):
#         cls._futures[c] = aio.Future()
#         cls._multi.add_handle(c)
#         try:
#             try:
#                 curl_ret = await cls._futures[c]
#             except CurlLoop.CurlException as e:
#                return {
#                     'url': c._raw_url,
#                     'id': c._raw_id,
#                     'payload': c._raw_payload,
#                     'spider': 'pycurl',
#                     'state': 'error',
#                     'error_code': e.code,
#                     'error_desc': e.desc,
#                 }
#             except Exception as e:
#                 return {
#                     'url': c._raw_url,
#                     'id': c._raw_id,
#                     'payload': c._raw_payload,
#                     'spider': 'pycurl',
#                     'state': 'critical',
#                     'error_code': -1,
#                     'error_desc': "{} - {}".format(type(e), str(e)),
#                 }
#             return curl_ret
#         finally:
#             cls._multi.remove_handle(c)
# 
#     @classmethod
#     def perform(cls):
#         if cls._futures:
#             while True:
#                 status, num_active = cls._multi.perform()
#                 if status != pycurl.E_CALL_MULTI_PERFORM:
#                     break
#             while True:
#                 num_ready, success, fail = cls._multi.info_read()
#                 for c in success:
#                     cc = cls._futures.pop(c)
#                     result = curl_result(c)
#                     result['url'] = c._raw_url
#                     result['id'] = c._raw_id
#                     result['state'] = 'normal'
#                     result['spider'] = 'pycurl'
#                     result['payload'] = payload = c._raw_payload
# 
#                     # post_func = payload.get('post_func')
#                     # if type(post_func) == str:
#                     #     post_func = load(post_func)
#                     # if post_func:
#                     #     result = post_func(payload, result)
# 
#                     cc.set_result(result)
#                 for c, err_num, err_msg in fail:
#                     print('error:', err_num, err_msg, c.getinfo(pycurl.EFFECTIVE_URL))
#                     result = curl_result(c)
# 
#                     result['url'] = c._raw_url
#                     result['id'] = c._raw_id
#                     result['state'] = 'error'
#                     result['spider'] = 'pycurl'
#                     result['error_code'] = err_num
#                     result['error_desc'] = err_msg
# 
#                     result['payload'] = payload = c._raw_payload
# 
#                     # post_func = payload.get('post_func')
#                     # if type(post_func) == str:
#                     #     post_func = load(post_func)
#                     # if post_func:
#                     #     result2 = post_func(payload, result)
#                     #     if type(result2) is dict and len(result2) >= len(result):
#                     #         result = result2
#                     cls._futures.pop(c).set_exception(CurlLoop.CurlException(code=err_num, desc=err_msg, data=result))
#                 if num_ready == 0:
#                     break
# 
# Path: falsy/netboy/fetch.py
# async def net_boy(payload, share=None):
#     targets = []
#     for p in payload:
#         if p.get('postfields'):
#             targets.append(post_request(p, share))
#         else:
#             targets.append(get_request(p, share))
#     res = await aio.gather(
#         *targets, return_exceptions=True
#     )
#     return res
# 
# Path: falsy/netboy/run.py
# def run(coro, loop=None):
#     async def main_task():
#         pycurl_task = aio.ensure_future(curl_loop())
#         try:
#             r = await coro
#         finally:
#             pycurl_task.cancel()
#             with suppress(aio.CancelledError):
#                 await pycurl_task
#         return r, pycurl_task
# 
#     if loop is None:
#         loop = uvloop.new_event_loop()
#         # loop = aio.get_event_loop()
#     aio.set_event_loop(loop)
#     loop.set_exception_handler(exception_handler)
#     r, _ = loop.run_until_complete(main_task())
#     return r


# Path: falsy/netboy/netboy.py
import typing
import pycurl
from falsy.netboy.curl_loop import CurlLoop
from falsy.netboy.fetch import net_boy
from falsy.netboy.run import run

                return self[name]
            except KeyError:
                # raise NetBoy.Exception('netboy key error: ' + name)
                return None  # '!netboy key [' + name + '] does not exist'
            except Exception:
                raise NetBoy.Exception('netboy exception: ' + name)

        def __setattr__(self, name, value):
            # type: (str, Any) -> None
            self[name] = value

    def __init__(self, payload=None, share=None):
        self.payload = payload
        if share:
            s = pycurl.CurlShare()
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_COOKIE)
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_DNS)
            s.setopt(pycurl.SH_SHARE, pycurl.LOCK_DATA_SSL_SESSION)
            self.share = s
        else:
            self.share = None

    def run(self, payload=None, loop=None):
        real_payload = payload
        if self.payload is None:
            real_payload = payload
        elif payload is None:
            real_payload = self.payload
        else:
            real_payload = self.payload + payload

        ress = run(net_boy(real_payload, self.share), loop=loop)