-
Notifications
You must be signed in to change notification settings - Fork 109
/
manage.py
72 lines (55 loc) · 2.14 KB
/
manage.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
# -*-coding:utf-8-*-
import time
import click
from datetime import datetime
from beijing_bus import BeijingBus, cache
@click.group()
def cmd_group():
pass
@cmd_group.command(help='build or re-build the cache')
def build_cache():
import logging
logging.basicConfig(level=logging.INFO)
cache.invalidate(hard=True)
BeijingBus.get_all_stations()
click.secho('Done!', fg='green')
def echo_realtime_data(line, station_num):
station = line.stations[station_num-1]
realtime_data = line.get_realtime_data(station_num)
click.clear()
now = datetime.now().strftime('%H:%M:%S')
title = '实时数据 [%s] 线路:%s (每5秒自动刷新,更新于%s)' % (station.name, line.name, now)
click.secho(title, fg='green', bold=True)
click.echo()
realtime_data = filter(lambda d: d['station_arriving_time'], realtime_data)
realtime_data.sort(key=lambda d: d['station_arriving_time'])
for i, data in enumerate(realtime_data):
click.secho('公交%s:' % (i+1), bold=True, underline=True)
click.echo('距离 %s 还有%s米' % (station.name, data['station_distance']))
click.echo('预计 %s 到达' % data['station_arriving_time'].strftime('%H:%M'))
click.echo()
@cmd_group.command()
def cli():
q = click.prompt('请输入线路名', value_proc=str)
lines = BeijingBus.search_lines(q)
for index, line in enumerate(lines):
click.echo()
click.secho('[%s] %s' % (index+1, line.name), bold=True, underline=True)
station_names = [s.name for s in line.stations]
click.echo()
click.echo('站点列表:%s' % ','.join(station_names))
click.echo()
q = click.prompt('请从结果中选择线路编号', type=int)
line = lines[q-1]
click.clear()
click.echo('你选择了 %s,下面请选择站点' % line.name)
click.echo()
for index, station in enumerate(line.stations):
click.echo('[%s] %s' % (index+1, station.name))
click.echo()
q = click.prompt('请从结果中选择线路编号', type=int)
while True:
echo_realtime_data(line, q)
time.sleep(5)
if __name__ == '__main__':
cmd_group()