forked from enkepha1in/fudan-sport-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (58 loc) · 2.33 KB
/
main.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
import asyncio
import random
import time
from argparse import ArgumentParser
from playground import playgrounds
from sport_api import FudanAPI, get_routes
async def main():
parser = ArgumentParser()
parser.add_argument('-v', '--view', action='store_true', help="list available routes")
parser.add_argument('-r', '--route', help="set route ID", type=int)
parser.add_argument('-t', '--time', help="total time, in seconds", type=int)
parser.add_argument('-d', '--distance', help="total distance, in meters", type=int)
parser.add_argument('-q', '--delay', action='store_true', help="delay for random time")
args = parser.parse_args()
if args.view:
routes = await get_routes()
supported_routes = filter(lambda r: r.id in playgrounds, routes)
for route in supported_routes:
route.pretty_print()
exit()
if args.route:
# set distance
distance = 1200
if args.distance:
distance = args.distance
distance += random.uniform(-5.0, 25.0)
# set time
total_time = 360
if args.time:
total_time = args.time
total_time += random.uniform(-10.0, 10.0)
# get routes from server
routes = await get_routes()
for route in routes:
if route.id == args.route:
selected_route = route
break
else:
raise ValueError(f'不存在id为{args.route}的route')
# delay random time, used in GitHub Action deployment
if args.delay:
sleep_time = random.randint(0, 240)
time.sleep(sleep_time)
# prepare & start running
automator = FudanAPI(selected_route)
playground = playgrounds[args.route]
current_distance = 0
await automator.start()
print(f"START: {selected_route.name}")
while current_distance < distance:
current_distance += distance / total_time
message, _ = await asyncio.gather(
automator.update(playground.random_offset(current_distance)), asyncio.sleep(1))
print(f"UPDATE: {message} ({current_distance}m / {distance}m)")
finish_message = await automator.finish(playground.coordinate(distance))
print(f"FINISHED: {finish_message}")
if __name__ == '__main__':
asyncio.run(main())