-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
84 lines (62 loc) · 2.57 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import logging
import requests
from settings import SECRET_KEY, API_ENDPOINT, DEFAULT_CITY_BOUNDS
logger = logging.getLogger(__name__)
def make_request(endpoint, method='get', data=None):
headers = {'Authorization': 'Bearer %s' % SECRET_KEY}
url = os.path.join(API_ENDPOINT, endpoint)
return requests.request(method, url, data=data, headers=headers)
def prepare_request(*args, **kwargs):
req = make_request(*args, **kwargs)
if req.ok:
return req.json()
else:
logger.warning('response is not json serializable')
return {}
def make_segments_request(bounds=DEFAULT_CITY_BOUNDS):
return prepare_request('segments/explore', data={'bounds': bounds})
def make_leaderboard_request(segment_id):
uri = 'segments/{segment_id}/leaderboard'.format(segment_id=segment_id)
return prepare_request(uri, data={'per_page': 50})
def get_segments():
segments_response = make_segments_request()
return segments_response.get('segments', [])
def get_leaderboard_entries(segment_id):
leaderboard_response = make_leaderboard_request(segment_id)
return leaderboard_response.get('entries', [])
def get_entrie_score(entrie):
speed = entrie['distance'] / entrie['moving_time']
average_hourly = entrie['average_hr'] or 1
return int(sum((speed, average_hourly)))
def leaderboard_data_handler():
segments = get_segments()
intersection_entries = {}
for segment in segments:
entries = get_leaderboard_entries(segment['id'])
for entrie in entries:
entrie['shots_count'] = 1
athlete_id = entrie['athlete_id']
intersection_entrie = intersection_entries.get(athlete_id)
if intersection_entrie:
entrie_ratio = get_entrie_score(entrie)
intersection_entrie['entrie_ratios'].append(entrie_ratio)
intersection_entrie['shots_count'] += 1
else:
entrie['entrie_ratios'] = [get_entrie_score(entrie)]
intersection_entries[athlete_id] = entrie
return intersection_entries.values()
def get_leaderboard_intersection():
leaderboard_data = leaderboard_data_handler()
entrie_list = []
for entrie in leaderboard_data:
if entrie['shots_count'] > 1:
entrie_list.append(entrie)
return entrie_list
def get_leaderboard_intersection_scores():
intersection_list = get_leaderboard_intersection()
entrie_list = []
for entrie in intersection_list:
entrie['score'] = sum(entrie['entrie_ratios'])
entrie_list.append(entrie)
return entrie_list