Example #1
0
def run(id):

    try:
        user_id = current_user.id
        r = get_request_retry(runs_endpoint(user_id), id)

        code = r.status_code
        if code == 200:
            run = r.json()

            run_id = run['id']
            name = run['title']
            timestamp = datetime.fromtimestamp(run['start_date'])
            date = timestamp.strftime('%A %d/%m/%y at %H:%M')
            headers = [
                'Distance (m)', 'AVG Speed (m/s)', 'Elapsed Time (s)',
                'Elevation (m)'
            ]
            values = [
                run['distance'], run['average_speed'], run['elapsed_time'],
                run['total_elevation_gain']
            ]
            return render_template("runs.html",
                                   name=name,
                                   date=date,
                                   headers=headers,
                                   values=values,
                                   id=run_id)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)

    return abort(404)
Example #2
0
def challenge(id):

    try:
        user_id = current_user.id
        r = get_request_retry(challenges_endpoint(user_id), id)

        code = r.status_code
        if code == 200:

            challenged_run = None
            challenger_run = None
            runs = None
            won = False

            result = r.json()

            run_challenged_id = result['run_challenged_id']
            run_challenger_id = result['run_challenger_id']
            won = result['result']

            challenged_run = get_run(run_challenged_id)
            challenger_run = get_run(run_challenger_id)

            if run_challenger_id is None:
                start_date_param = timestamp_to_param(result['start_date'])
                params = {'start-date': start_date_param}
                try:
                    r = get_request_retry(runs_endpoint(user_id),
                                          params=params)

                    code = r.status_code
                    if code == 200:
                        runs = r.json()

                        for run in runs:
                            date = run['start_date']
                            run['start_date'] = timestamp_to_date(date)
                    else:
                        return abort(code)
                except requests.exceptions.RequestException as err:
                    print(err)
                    return abort(503)

            return render_template("challenge.html",
                                   id=id,
                                   challenged_run=challenged_run,
                                   challenger_run=challenger_run,
                                   runs=runs,
                                   won=won)
        else:
            abort(code)
    except requests.exceptions.RequestException as err:
        print(err)
        return abort(503)

    return abort(400)
Example #3
0
def get_run(run_id):
    if run_id is not None:
        user_id = current_user.id
        try:
            r = get_request_retry(runs_endpoint(user_id), run_id)
            code = r.status_code
            if code == 200:
                result = r.json()
                result['start_date'] = timestamp_to_date(result['start_date'])
                return result

        except requests.exceptions.RequestException as err:
            print(err)
    return None
def update_distance(training_objectives, runner_id):
    lastRunId = db.session.query(Last_Run.lastRunId).filter(
        Last_Run.runner_id ==
        runner_id).first().lastRunId  #we take the id of the last fetched run
    dict_to = {}
    user = db.session.query(Last_Run).filter(
        Last_Run.runner_id == runner_id).first()
    maxRunId = user.lastRunId if user.lastRunId is not None else -1
    for to in training_objectives:
        id_ = to.id
        start_date = to.start_date

        end_date = to.end_date
        travelled_kilometers = to.travelled_kilometers

        params = {
            "start-date": str(start_date).replace(" ", "T") + "Z",
            "finish-date": str(end_date).replace(" ", "T") + "Z"
        }

        if lastRunId is not None:
            params["from-id"] = str(lastRunId)

        try:
            runs_response = get_request_retry(
                runs_endpoint(runner_id),
                params=params)  #request to data service
        except requests.exceptions.RequestException as ex:
            abort(503, str(ex))

        list_of_runs = runs_response.json()
        status_code = runs_response.status_code

        if status_code != 200:
            abort(status_code, runs_response.json().get('message'))

        partial_sum = 0
        for run in list_of_runs:
            partial_sum += run['distance']
            if run['id'] > maxRunId:  #here we pick the max id of the runs
                maxRunId = run['id']
        partial_sum /= 1000
        travelled_kilometers += partial_sum
        dict_to[id_] = travelled_kilometers

    for to in training_objectives:
        to.travelled_kilometers = dict_to[to.id]
    user.lastRunId = maxRunId  #here we update the lastRunId of the user (lastRunId is the id of the last fetched run by the user)
    db.session.commit()
Example #5
0
def index():

    strava_auth_url = _strava_auth_url(home.app.config)
    average_speed = None
    runs = None

    if current_user is not None and hasattr(current_user, 'id'):

        user_id = current_user.id

        # Average Speed
        try:
            r = get_request_retry(users_endpoint(user_id), 'average')
            code = r.status_code
            if code == 200:
                result = r.json()
                average_speed = result['average_speed']
            else:
                return abort(code)
        except requests.exceptions.RequestException as err:
            print(err)
            return abort(503)

        # Runs
        try:
            params = {'page': 0, "per_page": 10}

            r = get_request_retry(runs_endpoint(user_id), params=params)
            code = r.status_code
            if code == 200:
                runs = r.json()
            else:
                return abort(code)
        except requests.exceptions.RequestException as err:
            print(err)
            return abort(503)

    return render_template('index.html',
                           strava_auth_url=strava_auth_url,
                           average_speed=average_speed,
                           runs=runs)
Example #6
0
def get_single_run(runner_id, run_id):
    r = get_request_retry(runs_endpoint(runner_id), run_id)
    result = r.json()
    return result