Exemple #1
0
    async def process(self):
        request = self.request

        try:
            callback = request.query_args.get("c", None)
        except Exception:
            callback = request.args.get("c", None)

        if callback is None:
            await self.session._remote_closed()
            raise exceptions.ServerError('"callback" parameter required')

        elif not self.check_callback.match(callback):
            await self.session._remote_closed()
            raise exceptions.ServerError('invalid "callback" parameter')

        headers = (
            ('Content-Type', "text/html; charset=UTF-8"),
            ('Cache-Control', CACHE_CONTROL),
            ('Connection', "close"),
        )
        headers += session_cookie(request)
        headers += cors_headers(request.headers)

        async def stream(_response):
            nonlocal self
            self.response = _response
            await _response.write(b"".join(
                (PRELUDE1, callback.encode("utf-8"), PRELUDE2, b" " * 1024)))
            # handle session
            await self.handle_session()

        # open sequence (sockjs protocol)
        return StreamingHTTPResponse(stream, headers=headers)
Exemple #2
0
async def serve(request):
    try:
        if (config["allow_download_dataset"]):
            return await response.file(config["dataset_file"])
        else:
            raise exceptions.ServerError("Option not enabled", status_code=401)

    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError("Bad key", status_code=401)
Exemple #3
0
def parse_coordinates(args):
    if not args.get("xy"):
        raise exceptions.ServerError("XY parameter is requried.",
                                     status_code=500)

    elements = args.get("xy").split(",")

    try:
        elements = [float(elem) for elem in elements]
    except ValueError:
        raise exceptions.ServerError(
            "Unable to handle xy. Verify that xy is a comma-separated string of numbers.",
            status_code=500)

    return [tuple(elements[x:x + 2]) for x in range(0, len(elements), 2)]
Exemple #4
0
async def chat(request):
    try:
        if(request.json["statement"] and request.json["response"] and request.json['userid']):
            persona = config["personas"].index(request.json["persona"])
            if(request.json["bad_response"] == ""):
                pass
            if (badwords.hasBadWords(request.json["statement"])==False):
                chatbots[persona].train(request.json["statement"],
                                        request.json["response"],
                                        request.json.get("bad_response", ""))
            nsfw=""
            if(badwords.hasBadWords(request.json["statement"]) or badwords.hasBadWords(request.json["response"])) :
                nsfw="nsfw"
            if(request.json["bad_response"] == False):
                with open(config.get("dataset_file", "./chat_dataset"+nsfw+".csv"), 'a') as f:
                    # Concatination is faster than join
                    csv = (request.json.get('statement')+";" +
                           request.json.get('response')+";" +
                           request.json.get('persona')+";" +
                           request.json.get('userid'))

                    f.write(csv+'\n')
            return response.json({"status": "ok"})
    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError("Bad Request", status_code=401)
Exemple #5
0
    def make_response(self, request, data, *args, **kwargs):
        """
        Looks up the representation transformer for the requested media
        type, invoking the transformer to create a response object. This
        defaults to default_mediatype if no transformer is found for the
        requested mediatype. If default_mediatype is None, a 406 Not
        Acceptable response will be sent as per RFC 2616 section 14.1

        :param data: Python object containing response data to be transformed
        """
        default_mediatype = kwargs.pop('fallback_mediatype',
                                       None) or self.default_mediatype
        mediatype = best_match_accept_mimetype(
            request,
            self.representations,
            default=default_mediatype,
        )
        if mediatype is None:
            raise exceptions.SanicException("Not Acceptable", 406)
        if mediatype in self.representations:
            resp = self.representations[mediatype](request, data, *args,
                                                   **kwargs)
            resp.headers['Content-Type'] = mediatype
            return resp
        elif mediatype == 'text/plain':
            resp = text(str(data), *args, **kwargs)
            resp.headers['Content-Type'] = 'text/plain'
            return resp
        else:
            raise exceptions.ServerError(None)
Exemple #6
0
async def add(request):
    try:
        if(request.json["text"] and request.json["emotions"] and config.get("add_dataset",True)):
            with open(config.get("dataset_file","./emotion_dataset.csv"), 'a') as f:
                # Concatination is faster than join
                csv = (request.json.get('text')+";" +
                       request.json["emotions"].get('anger', "0")+";" +
                       request.json["emotions"].get('boredom', "0")+";" +
                       request.json["emotions"].get('empty', "0")+";" +
                       request.json["emotions"].get('enthusiasm', "0")+";" +
                       request.json["emotions"].get('fear', "0")+";" +
                       request.json["emotions"].get('happiness', "0")+";" +
                       request.json["emotions"].get('hate', "0")+";" +
                       request.json["emotions"].get('love', "0")+";" +
                       request.json["emotions"].get('neutral', "0")+";" +
                       request.json["emotions"].get('anger', "0")+";" +
                       request.json["emotions"].get('relief', "0")+";" +
                       request.json["emotions"].get('sadness', "0")+";" +
                       request.json["emotions"].get('worry', "0"))

                f.write(csv+'\n')

            return response.text("ok")
    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError(
            "Bad Request: 'text' or 'emotions' doesnt exist in json", status_code=401)
Exemple #7
0
async def test2(request):
    try:
        if (request.args["text"]):
            return response.text("Valid request")
    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError("Bad Request", status_code=401)
Exemple #8
0
async def fetch_link(request, username):
    """
    Fetch link from the given username
    :param request: The thing that sanic give us, but seems it is useless in this case.
    :param username: The requested username.
    :return: Redirection if the profile page's link has been found.
    """
    logger.debug(request)  # Stop IDEA from blaming me about unused variable
    if username in CACHE.keys():
        logger.info('Cache hit: [ %s: %s ]' % (username, CACHE[username]))
        return response.redirect(CACHE[username])
    name = username.split('@')
    if len(name) != 2:
        raise exceptions.InvalidUsage('Invalid username', status_code=400)
    host_meta = await fetch('https://' + name[1] + '/.well-known/host-meta')
    try:
        host_links = host_meta['XRD']['Link']
    except TypeError:
        raise exceptions.InvalidUsage('Unsupported platform', status_code=400)
    # Get Webfinger's URL
    webfinger = ''
    if type(host_links) == list:
        for link in host_links:
            if is_webfinger(link):
                webfinger = link['@template']
                break
    elif type(host_links) == OrderedDict:
        if is_webfinger(host_links):
            webfinger = host_links['@template']
    else:
        raise exceptions.InvalidUsage('Unsupported platform', status_code=400)
    # Request Webfinger for user's meta
    user_meta = await fetch(
        webfinger.replace('{uri}', 'acct%3A' + username.replace('@', '%40')))
    try:
        user_link = ''
        if 'aliases' in user_meta.keys():
            # If the server provides aliases
            user_link = user_meta['aliases'][0]
        elif 'links' in user_meta.keys():
            # Otherwise, read `links`
            for link in user_meta['links']:
                if link['rel'] == 'self' or link[
                        'rel'] == 'http://webfinger.net/rel/profile-page':
                    user_link = link['href']
        else:
            raise exceptions.InvalidUsage('Unsupported platform',
                                          status_code=400)
        if user_link:
            logger.info('Adding to cache: [ %s: %s ], redirecting...' %
                        (username, user_link))
            CACHE[username] = user_link
            return response.redirect(user_link)
        else:
            raise exceptions.ServerError(
                'I don\'t really know what\'s going on...', status_code=500)
    except Exception:
        raise exceptions.InvalidUsage(
            'Unknown error, probably the user doesn\'t exist.',
            status_code=500)
Exemple #9
0
async def classify(request):
    try:
        if(request.args["text"]):
            return response.json({"classification": classify_emotion(request.args["text"])})
    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError(
            "Bad Request,missing 'text' paramerter", status_code=401)
Exemple #10
0
def parse_flow(args):
    if not args.get("flow") or args.get("flow") == "origin":
        return "origin"
    elif args.get("flow") == "destination":
        return "destination"
    else:
        raise exceptions.ServerError(
            "Unsupported flow specified. Must be either origin (default) or destination.",
            status_code=500)
Exemple #11
0
def get_query_geom(coords):
    if len(coords) == 1:
        return Point(coords)
    elif len(coords) > 2:
        return asPolygon(coords)
    else:
        raise exceptions.ServerError(
            "Insufficient xy coordinates provided. A LinearRing must have at least 3 coordinate tuples.",
            status_code=500)
Exemple #12
0
async def chat(request):
    try:
        if(request.json["statement"] and request.json['userid']):
            reply = chatbots[config["personas"].index(request.json["persona"])].reply(
                request.json["statement"])
            return response.json({"response": reply})
    except:
        debug(("Unexpected error:", sys.exc_info()[0]))
        raise exceptions.ServerError(
            "Bad Request missing json 'statement' or 'userid'", status_code=401)
Exemple #13
0
def parse_mode(args):
    if not args.get("mode") or args.get("mode") == "all":
        return "total"
    elif args.get("mode") == "scooter":
        return "scooter"
    elif args.get("mode") == "bicycle":
        return "bicycle"
    else:
        raise exceptions.ServerError(
            "Unsupported mode specified. Must be either scooter, bicycle, or all (default).",
            status_code=500)
Exemple #14
0
    async def process(self):
        # start websocket connection
        try:
            p = self.request.transport.get_protocol()
        except:
            raise exceptions.ServerError(
                "Cannot get http protocol from transport")
        if isinstance(p, WebSocketProtocol):
            wsp = p
        else:
            loop = asyncio.get_event_loop()
            app = self.request.app
            wsp = WebSocketProtocol(loop=loop,
                                    app=app,
                                    request_handler=app.handle_request,
                                    error_handler=app.error_handler)
            wsp.transport = self.request.transport
            self.request.transport.set_protocol(wsp)

        ws = self.ws = await wsp.websocket_handshake(self.request)

        # session was interrupted
        if self.session.interrupted:
            await self.ws.send_str(close_frame(1002, "Connection interrupted"))

        elif self.session.state == STATE_CLOSED:
            await self.ws.send_str(close_frame(3000, "Go away!"))

        else:
            try:
                await self.manager.acquire(self.session)
            except Exception:  # should use specific exception
                await self.ws.send_str(close_frame(3000, "Go away!"))
                await ws.close()
                return ws
            server = ensure_future(self.tx_loop(ws, self.session))
            client = ensure_future(self.rx_loop(ws, self.session))
            try:
                await asyncio.wait((server, client),
                                   return_when=asyncio.FIRST_COMPLETED)
            except asyncio.CancelledError:
                raise
            except Exception as exc:
                await self.session._remote_close(exc)
            finally:
                await self.manager.release(self.session)
                if not server.done():
                    server.cancel()
                if not client.done():
                    client.cancel()

        return ws
Exemple #15
0
    async def process(self):
        request = self.request
        allowed_methods = ('GET', 'POST', 'OPTIONS')
        if request.method not in allowed_methods:
            # TODO: This was previously exceptions.forbidden. Which is right?
            raise exceptions.MethodNotSupported("Method is not allowed",
                                                request.method,
                                                allowed_methods)

        if self.request.method == 'OPTIONS':
            headers = (
                ('Access-Control-Allow-Methods', "OPTIONS, POST"),
                ('Content-Type', "application/javascript; charset=UTF-8"),
            )
            headers += session_cookie(request)
            headers += cors_headers(request.headers)
            headers += cache_headers()
            return HTTPResponse(None, status=204, headers=headers)

        data = await request.stream.read()
        if not data:
            raise exceptions.ServerError("Payload expected.")

        try:
            messages = loads(data.decode(ENCODING))
        except Exception:
            raise exceptions.ServerError(text="Broken JSON encoding.")

        await self.session._remote_messages(messages)

        headers = (
            ('Content-Type', "text/plain; charset=UTF-8"),
            ('Cache-Control', CACHE_CONTROL),
        )
        headers += session_cookie(request)
        headers += cors_headers(request.headers)

        return HTTPResponse(status=204, headers=headers)
Exemple #16
0
def parse_mode(args):
    if not args.get("mode"):
        return "all"
    elif args.get("mode").lower() == "all":
        return "all"
    elif args.get("mode").lower() == "scooter":
        return "scooter"
    elif args.get("mode").lower() == "bicycle":
        return "bicycle"
    else:
        raise exceptions.ServerError(
            "Unsupported mode specified. Must be either `scooter`, `bicycle`, or `all` (default).",
            status_code=500,
        )
Exemple #17
0
def get_flow_keys(flow):
    """
    Bit of harcoding to map the flow to the corresponding dataset property
    """
    if flow == "origin":
        flow_key_init = "cell_id_start"
        flow_key_end = "cell_id_end"
    elif flow == "destination":
        flow_key_init = "cell_id_end"
        flow_key_end = "cell_id_start"
    else:
        # this should never happen because we validate the flow param when parsing
        # the request
        raise exceptions.ServerError(
            "Unsupported flow specified. Must be either `origin` (default) or `destination`.",
            status_code=500,
        )

    return [flow_key_init, flow_key_end]
Exemple #18
0
def to_local_string(timestamp):

    if not timestamp:
        return None

    try:
        timestamp = int(float(timestamp)) / 1000

    except ValueError:
        raise exceptions.ServerError(
            f"{date_param} must be a number representing Unix time in milliseconds.",
            status_code=500,
        )

    # for god knows why utcfromtimestamp returns a naive timestamp.
    # so we have to append `.replace(tzinfo=pytz.utc)` to make it tz aware
    dt = datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc)

    # and now we can represent the time in
    # we lop off the tz info from the timestamp because we're going
    # to pass socrata a "local" naive timestamp (YYYY-MM-DDTHH:MM:SS)
    return local.isoformat()[0:19]
Exemple #19
0
    async def handler(self, request, server, sid, tid):
        if tid not in self.handlers or tid in self.disable_transports:
            raise exceptions.NotFound("SockJS transport handler not found.")

        create, transport = self.handlers[tid]

        # session
        manager = self.manager
        if not manager.started:
            manager.start()

        if not sid or "." in sid or "." in server:
            raise exceptions.NotFound("SockJS bad route")

        try:
            session = manager.get(sid, create, request=request)
        except KeyError:
            return response.HTTPResponse(None,
                                         status=404,
                                         headers=session_cookie(request))

        t = transport(manager, session, request)
        try:
            return await t.process()
        except asyncio.CancelledError:
            raise
        except exceptions.SanicException as exc:
            msg = "Server Exception in Transport handler: %s" % str(exc)
            log.exception(msg)
            raise
        except Exception:
            msg = "Exception in transport: %s" % tid
            log.exception(msg)
            if manager.is_acquired(session):
                await manager.release(session)
            raise exceptions.ServerError(msg)
Exemple #20
0
    async def process(self):
        session = self.session
        request = self.request
        meth = request.method

        if request.method == 'GET':
            try:
                callback = self.callback = request.query_args.get("c")
            except Exception:
                callback = self.callback = request.args.get("c", None)

            if not callback:
                await self.session._remote_closed()
                raise exceptions.ServerError('"callback" parameter required')

            elif not self.check_callback.match(callback):
                await self.session._remote_closed()
                raise exceptions.ServerError('invalid "callback" parameter')

            headers = (
                ('Content-Type', "application/javascript; charset=UTF-8"),
                ('Cache-Control', CACHE_CONTROL),
            )
            headers += session_cookie(request)
            headers += cors_headers(request.headers)

            async def stream(_response):
                nonlocal self
                self.response = _response
                # handle session
                await self.handle_session()

            # open sequence (sockjs protocol)
            return StreamingHTTPResponse(stream, headers=headers)

        elif request.method == 'POST':
            data = await request.stream.read()

            ctype = request.content_type.lower()
            if ctype == "application/x-www-form-urlencoded":
                if not data.startswith(b"d="):
                    raise exceptions.ServerError("Payload expected.")

                data = unquote_plus(data[2:].decode(ENCODING))
            else:
                data = data.decode(ENCODING)

            if not data:
                raise exceptions.ServerError("Payload expected.")

            try:
                messages = loads(data)
            except Exception:
                raise exceptions.ServerError("Broken JSON encoding.")

            await session._remote_messages(messages)

            headers = (
                ('Content-Type', "text/html;charset=UTF-8"),
                ('Cache-Control', CACHE_CONTROL),
            )
            headers += session_cookie(request)
            return HTTPResponse(None, body_bytes=b"ok", headers=headers)
        else:
            raise exceptions.MethodNotSupported(
                "No support for such method: %s" % meth, meth, ('GET', 'POST'))
Exemple #21
0
async def function_execute(request):
    """
    :param request: functions: [{model: "model", function: "function"}]
    :return:
    """
    functions = request.json.get('functions', {})
    origin_data = request.json.get('origin_data', {})
    db = await Mongo().db()

    if not (functions and origin_data):
        raise exceptions.ServerError("请检查必要参数的传入!")

    # begin
    step = 0
    task_uuid = str(uuid.uuid4())

    for f in functions:
        complete_model = f.get('model')
        function = f.get('function')

        try:
            index = complete_model.rfind('.')
            module, model = complete_model[:index], complete_model[index + 1:]
            Class = getattr(__import__(module, fromlist=[module]), model)

            if step == 0:
                data = origin_data
                await db.record.insert_one({
                    "task_uuid":
                    task_uuid,
                    "step":
                    0,
                    "function":
                    '.'.join([complete_model, function]),
                    "record":
                    origin_data
                })
            else:
                rec = await db.record.find({
                    'task_uuid': task_uuid,
                    'step': step
                }).to_list(length=1)
                # data = await rec.to_list(length=1)
                data = rec[0].get('record')
            step += 1

            record = await getattr(Class(), function)(data)
            # mongodb create
            val = {
                "task_uuid": task_uuid,
                "step": step,
                "function": '.'.join([complete_model, function]),
                "record": record
            }
            await db.record.insert_one(val)

        except AttributeError:
            raise exceptions.ServerError("请检查第%s步的方法" % step)
        except Exception as e:
            logger.error(e)
            raise exceptions.ServerError(e)

    return json(record)