示例#1
0
def hourly():
    get_args = flask.request.args
    get_headers = flask.request.headers
    # verify that a username and session key was sent
    if not authenticate_route(get_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    if not get_args["location"]:
        return {"status": 401, "error": "Missing location."}, 200
    api_return = get_weather_data(
        get_args.get("location"), get_args.get("units", "metric")
    )
    # convert to json
    weather_data = api_return.json()
    hours = {"hours": []}
    # build the json
    for weather in weather_data["hourly"]:
        if len(hours.get("hours")) > 23:
            break
        hour = {
            "date": weather["dt"],
            "temp": weather["temp"],
            "feels_like_temp": weather["feels_like"],
            "pop": weather["pop"],
            "wind_speed": weather["wind_speed"],
            "wind_deg": weather["wind_deg"],
            "humidity": weather["humidity"],
            "weather_type": weather["weather"][0]["main"],
            "uvi": weather_data["daily"][0].get("uvi", 0),
        }
        hours.get("hours").append(hour)
    # return the json
    hours.update({"status": 200})
    return hours, 200
示例#2
0
def change_password():
    post_args = flask.request.get_json()
    post_headers = flask.request.headers
    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    username = global_vars.sessions.get(post_headers.get("Session-Key"))
    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute(
        "SELECT password FROM users WHERE username = '******'".format(username))

    old_password = post_args.get("old_password")
    old_hashed_password = c.fetchone()[0]
    new_password = post_args.get("new_password")

    if check_encrypted_password(old_password, old_hashed_password):
        c.execute(
            "UPDATE users SET password = '******' WHERE username = '******'".format(
                encrypt_password(new_password), username))
        conn.commit()
        conn.close()
        return {"status": 200}, 200
    else:
        conn.commit()
        conn.close()
        return {
            "error": "Your password does not match your current password.",
            "status": 401,
        }, 200
def get_day_notification():
    get_headers = flask.request.headers

    if not authenticate_route(get_headers):
        return {"status": 401, "error": "Missing session key."}, 200

    username = global_vars.sessions.get(get_headers.get("Session-Key"))

    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute(
        """SELECT id, date, ideal_weather, location FROM notification_days WHERE username = '******'"""
        .format(username))
    notification_days = c.fetchall()
    conn.close()

    users_notification_days = []

    for notification_day in notification_days:
        users_notification_day = {
            "id": notification_day[0],
            "date": notification_day[1],
            "ideal_weather": notification_day[2],
            "location": notification_day[3],
        }
        users_notification_days.append(users_notification_day)

    return {"notification_days": users_notification_days, "status": 200}, 200
示例#4
0
def update_task(task_id):
    post_args = flask.request.get_json()
    post_headers = flask.request.headers
    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    if not task_id:
        return {"status": 401, "error": "Missing task id."}, 200
    if (
        not post_args.get("task", 0)
        and not post_args.get("date")
        and not post_args.get("ideal_weather")
        and not post_args.get("location")
    ):
        return {"status": 401, "error": "All fields are empty."}, 200
    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute("SELECT username FROM tasks WHERE id = '{}'".format(task_id))
    query = c.fetchone()
    if query:
        if query[0] != global_vars.sessions.get(post_headers.get("Session-Key")):
            return {"status": 401, "error": "This task does not belong to you."}, 401
    task = {
        "task": post_args.get("task", ""),
        "date": post_args.get("date", ""),
        "ideal_weather": post_args.get("ideal_weather", ""),
        "location": post_args.get("location", ""),
    }
    c.execute(build_update_query("tasks", task, "id = '{}'".format(task_id)))
    conn.commit()
    conn.close()
    return {"status": 200}, 200
示例#5
0
def create_task():
    post_args = flask.request.get_json()
    post_headers = flask.request.headers
    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    if not post_args.get("task", 0):
        return {
            "status": 401,
            "error": "Missing task.",
        }, 200
    task_id = str(uuid.uuid4())
    task = {
        "id": task_id,
        "username": global_vars.sessions.get(post_headers.get("Session-Key")),
        "task": post_args.get("task", ""),
        "date": post_args.get("date", ""),
        "ideal_weather": post_args.get("ideal_weather", ""),
        "location": post_args.get("location", ""),
    }
    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute(build_insert_query("tasks", task))
    conn.commit()
    conn.close()
    return {"status": 200, "id": task_id}, 200
示例#6
0
def add_token():
    post_args = flask.request.get_json()
    post_headers = flask.request.headers
    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    global_vars.tokens.update({
        global_vars.sessions.get(post_headers.get("Session-Key")):
        post_args.get("token")
    })
    return {"status": 200}, 200
示例#7
0
def get_consecutive_days():
    get_args = flask.request.args
    get_headers = flask.request.headers
    if not authenticate_route(get_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    if not get_args.get("location", 0):
        return {"status": 401, "error": "Missing location."}, 200
    if not get_args.get("ideal_weather", 0):
        return {"status": 401, "error": "Missing ideal weather."}, 200
    if not get_args.get("time", 0):
        return {"status": 401, "error": "Missing time."}, 200
    api_return = get_weather_data(
        get_args.get("location"), get_args.get("units", "metric")
    )
    days = {"days": []}
    # convert to json
    weather_data = api_return.json()
    if int(get_args.get("time")) <= 0 or weather_data.get('daily'):
        return {"days": days, "status": 204}, 200
    weather_counter = 0
    last_day = -1
    for weatherIndex in range(len(weather_data["daily"])):
        weather_day = weather_data["daily"][weatherIndex]
        if weather.get("weather", [{}])[0].get("main", 0) == get_args.get("ideal_weather", "").lower():
            weather_counter += 1
        else:
            weather_counter = 0
        if weather_counter == int(get_args.get("time")):
            last_day = weatherIndex
            break
    days = []
    if last_day == -1:
        return {"days": days, "status": 204}, 200
    weather_start = last_day-int(get_args.get("time"))
    for weather_index in range(int(get_args.get("time"))):
        weather = weather_data["daily"][weather_start + weather_index]
        day = {
            "date": weather.get("dt", 0),
            "temp": weather.get("temp", {}).get("day", 0),
            "feels_like_temp": weather.get("feels_like", {}).get("day", 0),
            "pop": weather.get("pop", 0),
            "wind_speed": weather.get("wind_speed", 0),
            "wind_deg": weather.get("wind_deg", 0),
            "humidity": weather.get("humidity", 0),
            "weather_type": weather.get("weather", [{}])[0].get("main", 0),
            "uvi": weather.get("uvi", -1)
        }
        days.append(day)
    return {"days": days, "start_date": days[0].get("date"), "status": 200}, 200
def delete_day_notification(notification_day_id):
    post_headers = flask.request.headers

    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200

    if not notification_day_id:
        return {"status": 401, "error": "Missing notification day id."}, 200

    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute("""DELETE FROM notification_days WHERE id = '{}'""".format(
        notification_day_id, ))
    conn.commit()
    conn.close()

    return {"status": 200}, 200
示例#9
0
def delete_task(task_id):
    post_headers = flask.request.headers
    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    if not task_id:
        return {"status": 401, "error": "Missing task id."}, 200
    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute("SELECT username FROM tasks WHERE id = '{}'".format(task_id))
    query = c.fetchone()
    if query:
        if query[0] != global_vars.sessions.get(post_headers.get("Session-Key")):
            return {"status": 401, "error": "This task does not belong to you."}, 200
    c.execute("DELETE FROM tasks WHERE id = '{}'".format(task_id))
    conn.commit()
    conn.close()
    return {"status": 200}, 200
def post_day_notification():
    post_args = flask.request.get_json()
    post_headers = flask.request.headers

    if not authenticate_route(post_headers):
        return {"status": 401, "error": "Missing session key."}, 200

    if not post_args.get("date", 0):
        return {"status": 401, "error": "Missing date."}, 200

    if not post_args.get("ideal_weather", 0):
        return {"status": 401, "error": "Missing ideal weather."}, 200

    if not post_args.get("location", 0):
        return {"status": 401, "error": "Missing location."}, 200

    if not post_args.get("offset", 0):
        return {"status": 401, "error": "Missing offset."}, 200

    notification_day_id = uuid.uuid4()

    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute(
        """INSERT INTO notification_days (id, date, ideal_weather, location, offset, username) VALUES ('{}', '{}', '{}', '{}', '{}', '{}')"""
        .format(
            notification_day_id,
            post_args.get("date"),
            post_args.get("ideal_weather"),
            post_args.get("location"),
            post_args.get("offset"),
            global_vars.sessions.get(post_headers.get("Session-Key")),
        ))
    conn.commit()
    conn.close()

    return {"notification_day_id": notification_day_id, "status": 200}, 200
示例#11
0
def get_task():
    get_args = flask.request.args
    get_headers = flask.request.headers
    if not authenticate_route(get_headers):
        return {"status": 401, "error": "Missing session key."}, 200
    offset = 0
    if get_args.get("offset", 0):
        offset_match = re.search(r"(\+|\-)(\d+)", get_args.get("offset"))
        if offset_match.groups():
            groups = offset_match.groups()
            if groups[0] == "+":
                offset = int(groups[1]) * 60
            else:
                offset = -1 * int(groups[1]) * 60
    conn = sqlite3.connect("db.db")
    c = conn.cursor()
    c.execute(
        "SELECT id, task, date, ideal_weather, location FROM tasks WHERE username = '******'".format(
            global_vars.sessions.get(get_headers.get("Session-Key"))
        )
    )
    tasks = []
    if get_args.get("current", False):
        query = [
            list(row)
            for row in c.fetchall()
            if datetime.fromtimestamp(int(list(row)[2])) >= datetime.now()
        ]
    else:
        query = [list(row) for row in c.fetchall()]
    if query:
        for task in query:
            if get_args.get("date", 0):
                formatted_date = datetime.fromtimestamp(int(task[2]) + offset).strftime(
                    "%Y-%m-%d"
                )
                if get_args.get("date") == formatted_date:
                    tasks.append(
                        {
                            "id": task[0],
                            "task": task[1],
                            "date": task[2],
                            "ideal_weather": task[3],
                            "location": task[4],
                        }
                    )
            else:
                tasks.append(
                    {
                        "id": task[0],
                        "task": task[1],
                        "date": task[2],
                        "ideal_weather": task[3],
                        "location": task[4],
                    }
                )

    return {
        "tasks": sorted(tasks, key=lambda task: task.get("date")),
        "status": 200,
    }, 200