def collect_ride_data(self, challenge):
        """
        Pull the latest ride data from Strava via the API
        """
        client = Client()
        client.access_token = self.strava_config['access-token']
        #activities = client.get_club_activities(165009)
        activities = client.get_club_activities(challenge.get_group_id())

        act_list = []
        for activity in activities:
            if not activity.type == 'Ride' and not activity.type == 'VirtualRide':
                print 'Non-ride activity: %s, type: %s' % (activity.name, activity.type)
                continue
            act = Activity(activity.id)
            act.set_athlete(activity.athlete.id)
            act.set_name(activity.name)
            act.set_gmt_date(activity.start_date) # GMT Start date
            act.set_elapsed_time(activity.elapsed_time)
            act.set_distance(round(unithelper.kilometers(activity.distance).num, 2))
            act.set_elevation(unithelper.meters(activity.total_elevation_gain).num)
            act.set_ride_type(activity.type)
            act.set_trainer_ride(activity.trainer)
            act_list.append(act)
        db_store = GTDataStore(challenge.get_db_path(), self.verbose)
        db_store.store_if_new(act_list)
class StravaBot:
    def __init__(self):
        Config = ConfigParser.ConfigParser()
        Config.read("configuration/config")
        Config.sections()

        self.clientId = Config.get('Strava', 'ClientId')
        self.clientSecret = Config.get('Strava', 'ClientSecret')
        self.clientAccessToken = Config.get('Strava', 'ClientAccessToken')
        self.clubId = Config.get('Strava', 'ClubId')
        self.mattermostUrl = Config.get('Mattermost', 'URL')
        self.delay = Config.get('Bot', 'Delay')

        self.client = Client()
        self.client.access_token = self.clientAccessToken
        self.club = self.client.get_club(self.clubId)

        self.http = urllib3.PoolManager(
            cert_reqs='CERT_REQUIRED',
            ca_certs=certifi.where())

        print('Bot for club {name} with id {id} is here :^)\n'.format(name=self.club.name, id=self.clubId))

    def post_activity(self, activity):
        payload = {}
        if (activity.athlete.firstname is None):
            activity.athlete = self.client.get_athlete(activity.athlete.id)

        first_name = activity.athlete.firstname
        last_name = activity.athlete.lastname
        distance = kilometers(activity.distance)
        activity_duration = activity.moving_time
        speed = kilometers_per_hour(activity.average_speed)
        climbing = meters(activity.total_elevation_gain)
        activity_id = activity.id
        description = activity.name

        if (len(description) > 100):
            description = description[:97] + "..."

        payload = {'username': '******', 'icon_url': 'https://raw.githubusercontent.com/patoupatou/pymatterstrava/master/icon-strava.png', 'text': u':bicyclist: *{} {} : distance: {}, moving time duration: {}, speed: {}, climbing: {}* [{}](http://strava.com/activities/{}) :bicyclist:'.format(first_name, last_name, distance, activity_duration, speed, climbing, description, activity_id)}
        r = self.http.request('POST', self.mattermostUrl, headers={'Content-Type': 'application/json'}, body=json.dumps(payload))
        print(time.ctime() + ': New activity posted')
        print('payload: ' + str(payload) + '\n')

    def get_activity_details(self, activity):
        return self.client.get_activity(activity.id)

    def get_new_activities(self, old_activities, new_activities):
        new_list = []
        new_activity_ids = []
        old_activity_ids = []
        for new_activity in new_activities:
            new_activity_ids.append(new_activity.id)
        for old_activity in old_activities:
            old_activity_ids.append(old_activity.id)

        diff_ids = list(set(new_activity_ids) - set(old_activity_ids))
        new_list = [act for act in new_activities if act.id in diff_ids]

        return new_list

    def run(self):

        activities = set(self.client.get_club_activities(self.clubId, limit=5))
        new_activities = activities

        # for activity in activities:
            # details = self.get_activity_details(activity)
            # self.post_activity(details)

        while(1):
            new_activities = set(self.client.get_club_activities(self.clubId, limit=5))
            diff_activities = self.get_new_activities(activities, new_activities)
            if len(diff_activities) > 0:
                print(time.ctime() + ': New activities!\n')
                print(diff_activities)
                for new_activity in diff_activities:
                    details = self.get_activity_details(new_activity)
                    self.post_activity(details)
            else:
                print(time.ctime() + ': No new activities\n')

            activities = new_activities
            time.sleep(float(self.delay))
Beispiel #3
0
client = Client(config.access_token)

athlete = client.get_athlete()
print("For {id}, I now have an access token {token}".format(
    id=athlete.id, token=client.access_token))

# my_activities = client.get_activities(limit=100)
#
# data = []
# for activity in my_activities:
#     my_dict = activity.to_dict()
#     data.append(my_dict)
#
# df = pd.DataFrame(data)

clubs = client.get_athlete_clubs()
res_club = clubs[0]
res_club_activities = client.get_club_activities(res_club.id)

club_activities = []
for activity in res_club.activities:
    activity_dict = activity.to_dict()
    activity_dict['athlete_name'] = activity_dict['athlete'][
        'firstname'] + ' ' + activity_dict['athlete']['lastname']
    club_activities.append(activity_dict)
club_df = pd.DataFrame(club_activities)
club_df.dropna(axis=1, inplace=True)
club_df.drop('athlete', axis=1, inplace=True)
club_df.to_csv('res_club_activities.csv')
Beispiel #4
0
from stravalib.client import Client
from config import *
import functions

#connection stuff
client = Client()
authorize_url = client.authorization_url(
    client_id=C_ID, redirect_uri='http://localhost:8282/authorized')
access_token = client.exchange_code_for_token(client_id=C_ID,
                                              client_secret=C_SECRET,
                                              code=CODE)
client.access_token = access_token

#getting activites
activities = client.get_club_activities(club_id=CLUB, limit=200)

#getting activities already recorded
done = functions.get_array('done')

#going through activities
data = []
for run in activities:
    #using local activity start date as a metric since using IDs feels shady
    if (str(run.start_date) not in done):
        #taking only runs
        if (run.type == 'Run'):
            data.append(run.start_date_local)
            data.append(run.elapsed_time)
            data.append(run.distance)
            data.append(run.total_elevation_gain)
            data.append(run.pr_count)
Beispiel #5
0
class Strava_scraper(object):
    '''
	A strava scraper class.
	'''
    def __init__(self, client_secret, access_token, strava_email,
                 strava_password):
        self.client_secret = client_secret
        self.access_token = access_token
        self.strava_email = strava_email
        self.strava_password = strava_password
        self.client = None
        self.athlete = None
        self.friends = None  # list of my friends, dtype = stravalib object
        self.activity_ids = []  # list of activity ids scraped from strava
        self.friend_ids = []
        self.activities = []  # list of activities
        self.clubs = []  # list of athlete clubs
        self.other_athletes = [
        ]  # list of other athlete objects unfollowed by client

    def get_client(self):
        """
		The get_client method create a client object for making requests to the strava API. The Client class accepts an access_token and a rate_limiter object. The method also populates a friends list
		Inputs: None
		Outputs: None
		"""
        self.client = Client(access_token=self.access_token,
                             rate_limiter=DefaultRateLimiter())

        self.athlete = self.client.get_athlete()  # Get Gordon's full athlete

        print "Client setup complete!"
        print
        self.friends = list(
            self.client.get_athlete_friends())  # Get athlete Gordon's friends
        print "Authenticated user's friends list complete!"
        print
        for friend in self.friends:
            self.friend_ids.append(friend.id)

    def log_in_strava(self):
        """
		The log_in_strava method uses a selenium webdriver to open and maintain a secure connect with Strava. It returns the driver object.
		Input: None
		Output: webdriver object
		"""
        chromeOptions = webdriver.ChromeOptions()
        prefs = {"profile.managed_default_content_settings.images": 2}
        chromeOptions.add_experimental_option("prefs", prefs)

        print "logging in..."
        print
        driver = webdriver.Chrome(chrome_options=chromeOptions)
        url = "https://www.strava.com/login"
        driver.get(url)
        user = driver.find_element_by_name('email')
        user.click()
        user.send_keys(self.strava_email)
        pwrd = driver.find_element_by_name('password')
        pwrd.click()
        pwrd.send_keys(self.strava_password)
        driver.find_element_by_id('login-button').click()
        sleep(10)
        print "complete!"
        return driver

    def _get_activity_by_id(self, act_id):
        try:
            activity = self.client.get_activity(
                act_id)  # get id with id = act_id from strava client
            return activity
        except HTTPError:
            return None

    def get_soup(self, driver, url):
        '''
		Helper function to get soup from a live url, as opposed to a local copy
		INPUT:
		-url: str
		OUTPUT: soup object
		'''
        driver.get(url)
        soup = BeautifulSoup(driver.page_source, 'html.parser')
        return soup

    def _make_interval_list(self):
        """
		This helper function makes an interval list that returns a list of numbers cooresponding with a year and week number for the given year. It only returns a static list as of now but in the future could search farther back. It only goes back to week 1, 2014.
		"""
        now = datetime.datetime.now()  # current date
        week_num = now.date().isocalendar()[1]  # current week number
        yr_wk = {
            2014: 52,
            2015: 53,
            2016: 52,
            2017: week_num
        }  # num of weeks each year only going back to 2014
        week_ints = [
            range(k * 100 + 1, k * 100 + v + 1) for k, v in yr_wk.iteritems()
        ]  # week ints in ugly nested lists
        new_week_ints = []
        for row in week_ints:
            new_week_ints.extend(
                row)  # creates new_week_ints which is week ints flattened
        return new_week_ints

    def _get_activities_from_page(self, soup):
        temp_act_id_list = []
        regex = re.compile('/activities/([0-9]*)')  # compile regex function
        for link in soup.find_all('a'):
            text = link.get('href')
            try:
                act_id = regex.findall(
                    text
                )  # look for digits after '/activities/'. Stop upon any character not a number. only looking for 1st item found. should be unicode string.
                try:  # only looking for integers 9 digits long
                    temp_act_id_list.append(int(
                        act_id[0]))  # append number to small list
                    # print act_id[0]
                except (IndexError, ValueError):
                    continue
            except TypeError:
                continue
        return temp_act_id_list

    def web_scrape_activities(self, start_n=0, sleep=False, sleep_time=2):
        """
		This function when called will scrape strava data for athlete activity id's. It will only get those of people I follow. It will store them in a list
		Example url:
		https://www.strava.com/athletes/2304253#interval?interval=201631&interval_type=week&chart_type=miles&year_offset=0
		where 2304253 is athlete id
		201631 is the year and week num

		This is whats needed to find and parse html from athlete pages and grab activity id's.
		Example tag:
		<a href="/activities/666921221">And the winning number is 400</a> ==$0
		"""
        driver = self.log_in_strava()
        week_ints = self._make_interval_list()

        print "scraping athletes"
        for ath_id in self.friend_ids[
                start_n:]:  #starting on index 191, athlete 66299
            athlete_act_id_list = []
            for yearweek_int in week_ints:
                url = "https://www.strava.com/athletes/{}#interval?interval={}&interval_type=week&chart_type=miles&year_offset=0".format(
                    str(ath_id), str(yearweek_int))
                soup = self.get_soup(driver, url)
                # self.activity_ids.extend(self._get_activities_from_page(soup))
                # print "added {}'s {} intervals to list".format(ath_id, yearweek_int)
                if sleep:
                    sleep(
                        np.random.exponential(1.0) * sleep_time
                    )  # pause for amount of sleep time before completing each loop
                athlete_act_id_list.extend(
                    self._get_activities_from_page(soup))
            filename = "{}_act_ids.csv".format(ath_id)
            filepath = os.path.join('activity_files', filename)
            write_list_to_csv(athlete_act_id_list, filepath)

        self.activity_ids = set(self.activity_ids)

        print "All done!"

    def get_other_athletes(self, list_ath_ids):
        """
		This utility function is provided to populate a list of other athletes. It requires a list of predifined athlete id's.
		Input: list_ath_ids as list
		Output: None
		"""
        print "Getting other athletes..."
        print
        for ath_id in list_ath_ids:
            if ath_id in self.friend_ids:
                continue
            else:
                athlete = self.client.get_athlete(ath_id)
                self.other_athletes.append(athlete)
        print "All done!"

    def load_activity_ids(self, act_id_csv_filename):
        """
		This utility function should only be called to populate the class attribute 'activity_ids' from a csv when a new scraper has been instantiated
		"""
        with open(act_id_csv_filename) as f:
            reader = csv.reader(f)
            self.activity_ids = np.array(next(reader), dtype='int')

    def get_activities_main(self):
        """
		This function when called after get client function will populate list attributes for class. This may be done when client wants all(last 200 for feeds) things associated with their athlete, friends, and clubs
		Input: None
		Output: None
		"""
        print "Getting client activities..."
        print
        self.activities.extend(list(self.client.get_activities()))  # gets all
        print "Getting friend activities..."
        print
        self.activities.extend(list(self.client.get_friend_activities(
        )))  # only gets last 200 activities from users feed
        print "Getting athlete clubs..."
        print
        self.clubs.extend(self.client.get_athlete_clubs())  # gets all
        club_ids = [club.id for club in self.clubs]
        print "Getting club activities..."
        print
        for club in club_ids:
            self.activities.extend(list(self.client.get_club_activities(
                club)))  # gets last 200 activities per club

        print "All done!"

    def get_activities_from_ids(self):
        requested_activity = None
        while len(self.activity_ids) > 0:
            requested_activity = self._get_activity_by_id(self.activity_ids[0])
            if requested_activity:
                self.activities.append(requested_activity)
            self.activity_ids = self.activity_ids[1:]

    def __repr__(self):
        return "This is {} {}'s strava scraper class".format(
            self.athlete.firstname, self.athlete.lastname)
Beispiel #6
0
client = Client(access_token='250d33ceabfbe833376eb18885e797af14888512')


athlete = client.get_athlete() # Get John's full athlete record
print("Hello, {}. I know your email is {}".format(athlete.firstname, athlete.email))
# "Hello, John.  I know your email is [email protected]"

activities = client.get_activities(limit=10)
assert len(list(activities)) == 10

clubs = client.get_athlete_clubs()
icc_members = client.get_club_members(club_id, limit=20)
assert len(list(icc_members)) == 20

club_activities = client.get_club_activities(club_id, limit=20)
assert len(list(club_activities)) == 20

#View activities
#for x in activities:
#    print (x)
    
for x in clubs:
    print (x)

for x in icc_members:
    print (x)
    
for x in club_activities:
    cm_activity = client.get_activity(x.id)
    print (x)