forked from hewanglanhao/fudan-sport-automate-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sport_api.py
89 lines (80 loc) · 3.13 KB
/
sport_api.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
85
86
87
88
89
from geopy.point import Point
import requests
import json
import os
def get_routes():
route_url = 'https://sport.fudan.edu.cn/sapi/route/list'
params = {'userid': os.getenv('USER_ID'), 'token': os.getenv('FUDAN_SPORT_TOKEN')}
response = requests.get(route_url, params=params)
data = json.loads(response.text)
try:
route_data_list = filter(lambda route: route['points'] is not None and len(route['points']) == 1,
data['data']['list'])
return [FudanRoute(route_data) for route_data in route_data_list]
except:
print(f"ERROR: {data['message']}")
exit(1)
def default_env(name, default):
if os.getenv(name) is None or os.getenv(name).strip() == '':
return default
else:
return os.getenv(name)
class FudanAPI:
def __init__(self, route):
self.route = route
self.user_id = os.getenv('USER_ID')
self.token = os.getenv('FUDAN_SPORT_TOKEN')
self.system = default_env('PLATFORM_OS', 'iOS 2016.3.1')
self.device = default_env('PLATFORM_DEVICE', 'iPhone|iPhone 13<iPhone14,5>')
self.run_id = None
def start(self):
start_url = 'https://sport.fudan.edu.cn/sapi/run/start'
params = {'userid': self.user_id,
'token': self.token,
'route_id': self.route.id,
'route_type': self.route.type,
'system': self.system,
'device': self.device,
'lng': self.route.start_point.longitude,
'lat': self.route.start_point.latitude}
response = requests.get(start_url, params=params)
data = json.loads(response.text)
try:
self.run_id = data['data']['run_id']
except:
print(f"ERROR: {data['message']}")
exit(1)
def update(self, point):
update_url = 'https://sport.fudan.edu.cn/sapi/run/point'
params = {'userid': self.user_id,
'token': self.token,
'run_id': self.run_id,
'lng': point.longitude,
'lat': point.latitude}
response = requests.get(update_url, params)
try:
data = json.loads(response.text)
return data['message']
except:
return response.text
def finish(self, point):
finish_url = 'https://sport.fudan.edu.cn/sapi/run/finish'
params = {'userid': self.user_id,
'token': self.token,
'run_id': self.run_id,
'system': self.system,
'device': self.device,
'lng': point.longitude,
'lat': point.latitude}
response = requests.get(finish_url, params)
data = json.loads(response.text)
return data['message']
class FudanRoute:
def __init__(self, data):
self.id = data['route_id']
self.name = data['name']
self.type = data['types'][0]
self.start_point = Point(data['points'][0]['lat'],
data['points'][0]['lng'])
def pretty_print(self):
print(f"#{self.id}: {self.name}")