'Fetch with splash'

@gen.coroutine
def splash_fetch(self, url, task):

    start_time = time.time()
    self.on_fetch(u'splash', task)
    handle_error = (lambda x: self.handle_error(u'splash', url, task, start_time, x))
    if (not self.splash_endpoint):
        result = {u'orig_url': url, u'content': u'splash    is    not    enabled.', u'headers': {}, u'status_code': 501, u'url': url, u'time': (time.time() - start_time), u'cookies': {}, u'save': task.get(u'fetch', {}).get(u'save')}
        logger.warning(u'[501]    %s:%s    %s    0s', task.get(u'project'), task.get(u'taskid'), url)
        raise gen.Return(result)
    fetch = self.pack_tornado_request_parameters(url, task)
    task_fetch = task.get(u'fetch', {})
    for each in task_fetch:
        if (each not in fetch):
            fetch[each] = task_fetch[each]
    if task_fetch.get(u'robots_txt', False):
        user_agent = fetch[u'headers'][u'User-Agent']
        can_fetch = (yield self.can_fetch(user_agent, url))
        if (not can_fetch):
            error = tornado.httpclient.HTTPError(403, u'Disallowed    by    robots.txt')
            raise gen.Return(handle_error(error))
    request_conf = {u'follow_redirects': False, u'headers': {u'Content-Type': u'application/json'}}
    request_conf[u'connect_timeout'] = fetch.get(u'connect_timeout', 20)
    request_conf[u'request_timeout'] = (fetch.get(u'request_timeout', 120) + 1)
    session = cookies.RequestsCookieJar()
    if (u'Cookie' in fetch[u'headers']):
        c = http_cookies.SimpleCookie()
        try:
            c.load(fetch[u'headers'][u'Cookie'])
        except AttributeError:
            c.load(utils.utf8(fetch[u'headers'][u'Cookie']))
        for key in c:
            session.set(key, c[key])
        del fetch[u'headers'][u'Cookie']
    if (u'cookies' in fetch):
        session.update(fetch[u'cookies'])
        del fetch[u'cookies']
    request = tornado.httpclient.HTTPRequest(url=fetch[u'url'])
    cookie_header = cookies.get_cookie_header(session, request)
    if cookie_header:
        fetch[u'headers'][u'Cookie'] = cookie_header
    fetch[u'lua_source'] = self.splash_lua_source
    fetch[u'headers'] = dict(fetch[u'headers'])
    try:
        request = tornado.httpclient.HTTPRequest(url=self.splash_endpoint, method=u'POST', body=json.dumps(fetch), **request_conf)
    except Exception as e:
        raise gen.Return(handle_error(e))
    try:
        response = (yield gen.maybe_future(self.http_client.fetch(request)))
    except tornado.httpclient.HTTPError as e:
        if e.response:
            response = e.response
        else:
            raise gen.Return(handle_error(e))
    if (not response.body):
        raise gen.Return(handle_error(Exception(u'no    response    from    phantomjs')))
    result = {}
    try:
        result = json.loads(utils.text(response.body))
        assert (u'status_code' in result), result
    except ValueError as e:
        logger.error(u'result    is    not    json:    %r', response.body[:500])
        raise gen.Return(handle_error(e))
    except Exception as e:
        if response.error:
            result[u'error'] = utils.text(response.error)
        raise gen.Return(handle_error(e))
    if result.get(u'status_code', 200):
        logger.info(u'[%d]    %s:%s    %s    %.2fs', result[u'status_code'], task.get(u'project'), task.get(u'taskid'), url, result[u'time'])
    else:
        logger.error(u'[%d]    %s:%s    %s,    %r    %.2fs', result[u'status_code'], task.get(u'project'), task.get(u'taskid'), url, result[u'content'], result[u'time'])
    raise gen.Return(result)
