def testing_route(): user_id = str(request.json['owner_id']) # below will throw a TypeError for testing # user_id = (request.json['owner_id']) activity_id = str(request.json['object_id']) action = str(request.json['aspect_type']) name = helper.find_user(user_id)['name'] if action == 'create': try: print('request.json: ', request.json) show_biz(user_id, activity_id) except TypeError as e: print(e) logging_handler.error_logger.warning("TypeError: ", exc_info=True) except Exception as e: print('Unhandled error:', e) logging_handler.error_logger.warning("Unhandled error: ", exc_info=True) finally: return make_response('Ok'), 200 else: print('request.json: ', request.json) print('type != create') logging_handler.posts_logger.info( f"{name} {action}d an activity with an activity ID of {activity_id}." ) return make_response('Ok'), 200
def match_to_umail(): ldap_conn = helper.connect() for user in sorted( User.query_by().filter( not_(User.username.contains('@umail.ucsb.edu'))).all()): if user.admin_for or user.is_admin or '(' in user.name: continue match = helper.find_user(ldap_conn, user.name) if match: print match, user.username
def show_biz(user_id, activity_id): # Below variable will store the result of a GET request to the Strava API to return the activity details. test_act = webhook_handler.testing_refresh(user_id, activity_id) if test_act['visibility'] == 'only_me': # Private activities are not posted. name = helper.find_user(user_id)['name'] logging_handler.posts_logger.info( f"{name} posted a private activity with an ID of {activity_id}.") else: disco_webhook.push_disco(test_act)
def push_disco(activity): """ Pushes the relevant activity information to the Discord endpoint. """ user_id = str(activity['athlete']['id']) name = helper.find_user(str(activity['athlete']['id']))['name'] activity_type = activity['name'] activity_duration = activity['moving_time'] activity_distance = activity['distance']/1000 logging_handler.posts_logger.info(f"{name} posted an activity with an ID of {activity['id']}.") content = f"{name} just posted to Strava! This hero just went on a {activity_type} for {activity_distance}km in {activity_duration}s. What a MACHINE! Kudos!" webhook = DiscordWebhook(url=disco_url, username='******', content=content) response = webhook.execute()
def webhook(): if request.method == 'GET': """ GET request at the /webhook endpoint is the method of initiation for receiving the Strava webhook. An instant response of 200 with the appropriate verification token must be received by the Strava API for the webhook to be initiated. """ verify_token = os.getenv('INITIATE_TOKEN') mode = request.args['hub.mode'] token = request.args['hub.verify_token'] challenge = request.args['hub.challenge'] if (mode and token): if mode == 'subscribe' and token == verify_token: print('webhook verified!') response = jsonify({'hub.challenge': challenge}) return make_response(response), 200 else: return make_response('Forbidden'), 403 elif request.method == 'POST': """Strava webhook received here.""" user_id = str(request.json['owner_id']) activity_id = str(request.json['object_id']) action = str(request.json['aspect_type']) name = helper.find_user(user_id)['name'] if action == 'create': # Only newly created activities are posted try: # If a valid create request is received, process is continued at show_biz. show_biz(user_id, activity_id) except TypeError as e: logging_handler.error_logger.warning("TypeError: ", exc_info=True) except Exception as e: logging_handler.error_logger.warning("Unhandled error: ", exc_info=True) finally: return make_response('Ok'), 200 else: logging_handler.posts_logger.info( f"{name} {action}d an activity with an activity ID of {activity_id}." ) return make_response('Ok'), 200 return 'x'
def find_refresh(user_id): """Finds a user's refresh token.""" user = helper.find_user(user_id) return user['refresh_token']