示例#1
0
def get_and_store_routes(user: User, strava_client_id, strava_client_secret):
    """Retrieves all (summary) activities from the Strava-API and stores them in the DB."""

    # TODO: find better place for this function
    # TODO: Handle API exhaustion

    user.check_and_refresh(strava_client_id, strava_client_secret)

    api = swagger_client.ActivitiesApi()
    api.api_client.configuration.access_token = user.access_token
    existing_route_ids = [r.id for r in db.session.query(Route).all()]

    # Pagination: Load new route pages until exhausted
    page = 1
    while True:
        r = api.get_logged_in_athlete_activities(per_page=100, page=page)
        # Filter bike-rides and new routes
        routes = []
        for route in r:
            if route.type == "Ride" and route.id not in existing_route_ids:
                route = Route.from_summary_activity(route)
                # Get detailed route and overwrite the summary polyline
                detailed_route = api.get_activity_by_id(route.id)
                route.route = detailed_route.map.polyline
                routes.append(route)

        if routes:
            db.session.add_all(routes)
            page += 1
        else:
            break

    db.session.commit()
示例#2
0
 def __init__(self, access_token=None):
     if access_token is None:
         access_token = os.getenv('STRAVA_ACCESS_TOKEN')
     self.configuration = swagger_client.Configuration()
     self.configuration.access_token = access_token
     self._api_client = swagger_client.ApiClient(self.configuration)
     self.athletes_api = swagger_client.AthletesApi(self._api_client)
     self.activities_api = swagger_client.ActivitiesApi(self._api_client)
     self.streams_api = swagger_client.StreamsApi(self._api_client)
示例#3
0
def swagger_get_activity(token: str, activity_id: int) -> swagger_client.models.detailed_activity:
    configuration = swagger_client.Configuration()
    configuration.access_token = token
    api_instance = swagger_client.ActivitiesApi(swagger_client.ApiClient(configuration))
    try:
        # Get Authenticated Athlete
        api_response = api_instance.get_activity_by_id(activity_id)
        return api_response
    except ApiException as e:
        print("Exception when calling AthletesApi->getLoggedInAthlete: %s\n" % e)  # todo - handle the exception
示例#4
0
def list_routes(user_id):
    """List the last routes."""
    user_id = int(user_id)
    check_and_refresh(app, user_id)
    api = swagger_client.ActivitiesApi()
    api.api_client.configuration.access_token = app.config["USERS"][user_id][
        "access_token"]
    r = api.get_logged_in_athlete_activities(per_page=100)
    response = jsonify(list(map(activity_to_dict, r)))
    response.headers.add("Access-Control-Allow-Origin", "*")
    return response
示例#5
0
def _update_activity_data(access_token: str, file_path: str, activities: list):
    """
    Update the file and list of detailed activity data with any new
    activities uploaded to Strava since the last stored activity.

    Arguments:
    access_token - An OAuth2 access token for the Strava v3 API.
    activities - The list of detailed activity data to be updated.
    """

    print('Strava: Checking for new activities')

    # Create an instance of the Activities API class
    api_instance = swagger_client.ActivitiesApi()
    api_instance.api_client.configuration.access_token = access_token

    # Get the start time of the last stored activity
    start_time = _get_last_activity_start_time(activities)

    # Get and store any new activities in pages of 25
    page_count = 1
    while True:
        page = api_instance.get_logged_in_athlete_activities(after=start_time,
                                                             page=page_count,
                                                             per_page=25)

        if page:
            activities_in_page = []

            for activity in page:
                print('Strava: Getting detailed activity data for {}'.format(
                    activity.name))

                # Get detailed activity data for each activity in the
                # page
                response = api_instance.get_activity_by_id(activity.id)

                # Convert the detailed activity data into a dictionary
                # and append it to the list of activity data from the
                # current page
                activities_in_page.append(response.to_dict())

            # Write the current page of activity data to the Strava
            # activities file
            _write_activity_data_to_file(file_path, activities_in_page)

            # Append the current page of activity data to the master
            # list of activity data
            activities += activities_in_page

            page_count += 1
        else:
            print('Strava: No new activities found')
            break
示例#6
0
文件: rk2s.py 项目: toravir/rk2s
def uploadManualActivity(tok, acttype, name, startDate, durationSec, descr,
                         distanceMtrs):

    api_instance = swagger_client.ActivitiesApi()
    api_instance.api_client.configuration.access_token = tok
    api_instance.api_client.set_default_header(
        'Content-Type', 'application/x-www-form-urlencoded')
    try:
        api_response = api_instance.create_activity(name,
                                                    acttype,
                                                    startDate,
                                                    durationSec,
                                                    description=descr,
                                                    distance=distanceMtrs,
                                                    trainer=0,
                                                    commute=0)
        return True
    except ApiException as e:
        print("Exception when calling ActivitiesApi->createActivity: %s\n" % e)
        return False
示例#7
0
def swagger_get_activities(token: str,
                           before: int = datetime.now().timestamp(),
                           after: int = 0,
                           page: int = 1,
                           per_page: int = 30,
                           async_req=True) -> list[swagger_client.models.summary_activity.SummaryActivity]:
    # todo make possible synchronous requests. Now asyc behaviour is hardcoded
    configuration = swagger_client.Configuration()
    configuration.access_token = token
    api_instance = swagger_client.ActivitiesApi(swagger_client.ApiClient(configuration))

    def _get_page_of_activities(current_page):
        try:
            # List Athlete Activities
            thread = api_instance.get_logged_in_athlete_activities(
                async_req=async_req,
                before=before,
                after=after,
                page=current_page,
                per_page=per_page
            )
            api_response = thread.get()
            return api_response
        except ApiException as e:
            print("Exception when calling ActivitiesApi->getLoggedInAthleteActivities: %s\n" % e)

    list_activities = []
    while True:
        page_of_activities = _get_page_of_activities(page)
        if not page_of_activities:
            break
        list_activities.extend(page_of_activities)
        if len(page_of_activities) < 30:
            break
        page += 1
    return list_activities
示例#8
0
from __future__ import print_statement
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = '750c4ecce9aecec0414bbeb8e295b833a6f7d95a'

# create an instance of the API class
api_instance = swagger_client.ActivitiesApi()
before = 56 # Integer | An epoch timestamp to use for filtering activities that have taken place before a certain time. (optional)
after = 56 # Integer | An epoch timestamp to use for filtering activities that have taken place after a certain time. (optional)
page = 56 # Integer | Page number. Defaults to 1. (optional)
perPage = 56 # Integer | Number of items per page. Defaults to 30. (optional) (default to 30)

try: 
    # List Athlete Activities
    api_response = api_instance.getLoggedInAthleteActivities(before=before, after=after, page=page, perPage=perPage)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ActivitiesApi->getLoggedInAthleteActivities: %s\n" % e)
示例#9
0
def get_strava(last_date=False):
    ## Getting log in info
    config = configparser.ConfigParser()
    config.read("/home/irarickman/strava.ini")
    params = {
        'client_id': config['strava']['client_id'],
        'client_secret': config['strava']['client_secret'],
        'code': config['strava']['code']
    }
    auth_url = config['strava']['auth_url']
    ref_url = config['strava']['ref_url']
    athlete_id = config['strava']['athlete_id']

    if not last_date:
        ## Getting the most recent date
        last_date = datetime.datetime.strptime(
            strav.start_date.max().split(' ')[0], "%Y-%m-%d")
    else:
        last_date = datetime.datetime.strptime(last_date, "%Y-%m-%d")
    timestamp = last_date.timestamp()
    delta = datetime.datetime.now() - last_date
    date_diff = delta.days + 5

    r_auth = requests.post(auth_url, data=params)
    response = r_auth.json()

    configuration = swagger_client.Configuration()
    configuration.access_token = response['access_token']

    # create an instance of the API class
    api_instance = swagger_client.ActivitiesApi(
        swagger_client.ApiClient(configuration))
    if date_diff < 200:
        try:
            # Get Authenticated Athlete
            api_response = api_instance.get_logged_in_athlete_activities(
                after=timestamp, per_page=date_diff)
        except ApiException as e:
            print(
                "Exception when calling AthletesApi->get_logged_in_athlete: {}\n"
                .format(e))
    else:
        num_rounds = math.ceil(date_diff / 200)
        api_response = []
        for n in range(num_rounds):
            if n == num_rounds:
                page_num = date_diff - (200 * (n - 1))
            else:
                page_num = 200
            try:
                # Get Authenticated Athlete
                activities = api_instance.get_logged_in_athlete_activities(
                    after=timestamp, page=n + 1, per_page=page_num)
            except ApiException as e:
                print(
                    "Exception when calling AthletesApi->get_logged_in_athlete: {}\n"
                    .format(e))
            api_response = api_response + activities

    example = list(api_response[len(api_response) - 1].to_dict().keys())
    example = [
        x for x in example
        if x not in ['map', 'athlete', 'start_latlng', 'end_latlng']
    ]
    dicts = {}
    for n in range(len(api_response)):
        d = api_response[n].to_dict()
        new_dict = {variable: d[variable] for variable in example}
        dicts[n] = new_dict

    index = list(dicts.keys())
    strava = pd.DataFrame([dicts[key] for key in index], index=index)
    mult_mile = 0.000621371
    strava['miles'] = strava.distance * mult_mile
    strava['race'] = strava.workout_type.apply(lambda x: 1
                                               if x in [1.0, 11.0] else 0)
    strava['date_string'] = strava.start_date_local.astype(str).apply(
        lambda x: x[:10])
    strava['moving_minutes'] = strava.moving_time / 60
    strava['elapsed_minutes'] = strava.elapsed_time / 60
    strava['rest'] = strava.elapsed_minutes - strava.moving_minutes
    ## average speed is in meters/second - 2.237 to multiply to mph
    strava['avg_mph'] = strava.average_speed * 2.237
    strava.start_date = pd.to_datetime(strava.start_date_local)
    strava.sort_values('start_date', inplace=True)
    strava['time_since_last_act'] = (
        pd.to_datetime(strava.start_date) -
        pd.to_datetime(strava.start_date.shift(1))).astype('timedelta64[h]')
    strava['order'] = strava.groupby('date_string').start_date.rank()
    if len(strav) == 0:
        wks_1.set_dataframe(strava, (1, 1))
    else:
        all_acts = pd.concat([strava, strav])
        all_acts.drop_duplicates(['id'], keep='first', inplace=True)
        wks_1.set_dataframe(all_acts, (1, 1))
示例#10
0
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.username_pw_set(secrets["user"], password=secrets["pass"])
client.connect(mqttServer, mqttPort, 60)

#Get epoch time for a week ago
now = round(time.time())
aweek = 60 * 60 * 24 * 7
weekAgo = now - aweek

#Do the swagger stuff
configuration = swagger_client.Configuration()
configuration.access_token = token
api_instance = swagger_client.ActivitiesApi(
    swagger_client.ApiClient(configuration))

after = weekAgo  # int | An epoch timestamp to use for filtering activities that have taken place after a certain time. (optional)

#Call the API for the logged in athelete's activities
api_response = api_instance.get_logged_in_athlete_activities(after=after)

#Create our list
list = {}

#Take the API response and just get the stuff we want
count = 0
for i in api_response:
    activitynum = "activity" + count.__str__()
    print(i.type)
    activity = {
示例#11
0
def get_strava(last_date=False):
    ## Getting log in info
    config = configparser.ConfigParser()
    config.read("../strava.ini")
    params={'client_id':config['strava']['client_id'],
            'client_secret':config['strava']['client_secret'],
            'code':config['strava']['code']}
    auth_url=config['strava']['auth_url']
    ref_url=config['strava']['ref_url']
    athlete_id=config['strava']['athlete_id']
    
    if not last_date:
        ## Getting the most recent date
        last_date=datetime.datetime.strptime(strav.start_date.max().split(' ')[0],"%Y-%m-%d")
    else:
        last_date=datetime.datetime.strptime(last_date,"%Y-%m-%d")
    timestamp=last_date.timestamp()
    delta=datetime.datetime.now() - last_date
    date_diff=delta.days+5

    r_auth = requests.post(auth_url, data=params)
    response=r_auth.json()

    configuration = swagger_client.Configuration()
    configuration.access_token = response['access_token']

    # create an instance of the API class
    api_instance = swagger_client.ActivitiesApi(swagger_client.ApiClient(configuration))
    if date_diff<200:
        try:
            # Get Authenticated Athlete
            api_response = api_instance.get_logged_in_athlete_activities(after=timestamp, per_page=date_diff)
        except ApiException as e:
            print("Exception when calling AthletesApi->get_logged_in_athlete: {}\n".format(e))
    else:
        num_rounds=math.ceil(date_diff/200)
        api_response=[]
        for n in range(num_rounds):
            if n==num_rounds:
                page_num=date_diff-(200*(n-1))
            else:
                page_num=200
            try:
                # Get Authenticated Athlete
                activities = api_instance.get_logged_in_athlete_activities(after=timestamp, page=n+1,per_page=page_num)
            except ApiException as e:
                print("Exception when calling AthletesApi->get_logged_in_athlete: {}\n".format(e))
            api_response=api_response+activities
                
    example=list(api_response[len(api_response)-1].to_dict().keys())
    example=[x for x in example if x not in ['map','athlete','start_latlng','end_latlng']]
    dicts={}
    for n in range(len(api_response)):
        d=api_response[n].to_dict()
        new_dict={variable:d[variable] for variable in example}
        dicts[n]=new_dict

    index=list(dicts.keys())
    new_acts=pd.DataFrame([dicts[key] for key in index],index=index)
    if len(strav)==0:
        wks_1.set_dataframe(new_acts,(1,1))
    else:
        all_acts=pd.concat([new_acts,strav])
        all_acts.drop_duplicates(inplace=True)
        wks_1.set_dataframe(all_acts,(1,1))
示例#12
0
def main(args):
    client = Client()
    client_id = 40291
    client_secret = '174e515190aa81a04416b147a32937ea5a86c672'
    with open("stravtoken.json", "r") as stravtoken:
        tokendict = json.load(stravtoken)
    access_token = tokendict["access_token"]
    refresh_token1 = tokendict["refresh_token"]
    expires_at = tokendict['expires_at']
    if time.time() > tokendict['expires_at']:
        refresh_response = client.refresh_access_token(client_id,
                                                       client_secret,
                                                       refresh_token1)
        access_token = refresh_response['access_token']
        refresh_token1 = refresh_response['refresh_token']
        expires_at = refresh_response['expires_at']
        with open("stravtoken.json", "w+") as json_file:
            json.dump(refresh_response, json_file)

    client.access_token = access_token
    client.refresh_access_token = refresh_token1
    client.token_expires_at = expires_at

    api_instance = swagger_client.ActivitiesApi()
    api_instance.api_client.configuration.access_token = access_token
    list_acts2 = []
    for x in range(1, 20):
        try:
            api_response = api_instance.get_logged_in_athlete_activities(
                page=x, per_page=100)
            acts = str(api_response)
            acts = acts.replace("\'", "\"")
            acts = acts.replace("None", "0")
            acts = acts.replace("False", "0")
            acts = acts.replace("True", "1")
            acts = json.loads(acts)
            for each in acts:
                if isinstance(each['start_latlng'], list):
                    list_acts2.append([
                        each['id'], each['distance'] / 1609.344,
                        each['moving_time'],
                        each['elapsed_time'] - each['moving_time'],
                        each['total_elevation_gain'],
                        each['total_elevation_gain'] / each['moving_time'],
                        each['start_latlng'][0], each['start_latlng'][1],
                        each['map']['summary_polyline']
                    ])
        except ApiException as e:
            print(x)

    acts_df = pd.DataFrame(list_acts2)
    acts_df.columns = [
        'Run ID', 'Distance', 'Time', 'Resting Time', 'Elevation Gain',
        'Elevation Grade', 'Start Latitude', 'Start Longitude', 'Polyline'
    ]

    acts_df_clean = acts_df.drop(acts_df[acts_df['Start Latitude'] > 34].index)
    acts_df_clean = acts_df_clean.drop(
        acts_df[acts_df['Start Latitude'] < 33].index).reset_index()

    #DIST LABEL --------------------------------------------------------------------------------
    conditions = [
        (acts_df_clean['Distance'] <= 5),
        (acts_df_clean['Distance'] > 5) & (acts_df_clean['Distance'] <= 9),
        (acts_df_clean['Distance'] > 9)
    ]
    values = ['Short', 'Medium', 'Long']
    acts_df_clean['Difficulty'] = np.select(conditions, values)

    #DIFF LABEL ---------------------------------------------------------------------------------
    #clust_df = acts_df_clean.filter(['Distance','Elevation Grade'], axis=1)
    #clust_df['Distance'] = clust_df['Distance'].apply(lambda x: pow(x*150,.35))
    #clust_df['Elevation Grade'] = clust_df['Elevation Grade'].apply(lambda x: pow(x*150,.8))
    #kmeans = KMeans(n_clusters=6,random_state=3425)
    #kmeans.fit(clust_df)
    #labels = pd.Series(kmeans.predict(clust_df),name='Difficulty').to_frame().reset_index()
    #clust_df = pd.concat([clust_df, labels], axis=1, sort=False)
    #acts_df_clean = pd.concat([acts_df_clean, labels], axis=1, sort=False)
    #maps = {0:2,1:1,2:0,3:2,4:0,5:1}
    #acts_df_clean['Difficulty'] = acts_df_clean['Difficulty'].apply(lambda x: maps[x])

    acts_df_clean.to_csv('activities.csv', index=False)