当前位置: 首页>>代码示例>>Python>>正文


Python Client.get_club方法代码示例

本文整理汇总了Python中stravalib.client.Client.get_club方法的典型用法代码示例。如果您正苦于以下问题:Python Client.get_club方法的具体用法?Python Client.get_club怎么用?Python Client.get_club使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stravalib.client.Client的用法示例。


在下文中一共展示了Client.get_club方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from stravalib.client import Client [as 别名]
# 或者: from stravalib.client.Client import get_club [as 别名]
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': 'strava_bot', '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))
开发者ID:opendwellers,项目名称:pymatterstrava,代码行数:87,代码来源:StravaBot.py


注:本文中的stravalib.client.Client.get_club方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。