Example #1
0
def post_quote():
    try:
        payload = request.get_json()
        key = payload['country'].lower() + '_' + payload['city'].lower(
        ) + '_' + payload['sku'].lower()

        state = redis_cli.get(key)

        if state:
            js = json.loads(state)

            data = {
                "sku": js['sku'],
                "description": js['description'],
                "country": js['country'],
                "city": js['city'],
                "base_price": js['base_price'],
                "variation": js['variation'],
                "cache": "hit"
            }
        else:
            wl = Worklog(mysql, app.logger)
            clima = requests.get(
                'http://api.openweathermap.org/data/2.5/weather?q=' +
                payload['city'] + ',' + payload['country'] +
                '&appid=3225ae99d4c4cb46be4a2be004226918').json()

            if clima['cod'] == "404":
                data = {
                    "mensaje":
                    "No existe informacion metereologica para " +
                    payload['city']
                }
            else:
                js = wl.find_rules(clima['weather'][0]['id'], **payload)

                if js is None:
                    data = {"mensaje": "Registro no encontrado"}
                else:
                    redis_cli.setex(
                        key, 300, '{"sku":"' + js[0] + '","description":"' +
                        js[1] + '","country":"' + js[2] + '","city":"' +
                        js[3] + '","base_price":' + str(js[4]) +
                        ',"variation":' + str(js[5]) + '}')
                    data = {
                        "sku": js[0],
                        "description": js[1],
                        "country": js[2],
                        "city": js[3],
                        "base_price": str(js[4]),
                        "variation": str(js[5]),
                        "cache": "miss"
                    }

        return jsonify(data)
    except:
        return jsonify({"mensaje": "Ha ocurrido un error, Verifique el URL."})
Example #2
0
def post_quote():
    #try:
    payload = request.get_json()
    key = payload['country'].lower() + '-' + payload['city'].lower(
    ) + '-' + payload['sku'].lower()

    cache = redis_cli.get(key)

    if cache:
        js = json.loads(cache)

        response = {
            "sku": js['sku'],
            "country": js['country'],
            "city": js['city'],
            #"price": js['price'],
            #"var": js['var'],
            "cache": "hit"
        }
        return jsonify(response)
    else:
        weather = requests.get(
            'http://api.openweathermap.org/data/2.5/weather?q=' +
            payload['city'] + ',' + payload['country'] +
            '&appid=3225ae99d4c4cb46be4a2be004226918').json()

        wl = Worklog(mysql, app.logger)
        js = wl.find_rules(weather['weather'][0]['id'], **payload)
        redis_cli.setex(
            key, 300, '{"country":"' + js[0] + '","city":"' + js[1] +
            '","sku":"' + js[2] + '","min":"' + str(js[3]) + '","max":"' +
            str(js[4]) + '","var":"' + str(js[5]) + '","description":"' +
            js[6] + '","price":' + str(js[7]) + '}')
        return jsonify({
            'cache': 'miss',
            'country': js[0],
            'city': js[1],
            'sku': js[2],
            'min': str(js[3]),
            'max': str(js[4]),
            'var': str(js[5]),
            'price': str(js[7]),
            'description': js[6]
        })