manage.py 2.94 KB
Newer Older
1
2
import time
import sys
3
4
5
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from app import app, db
6
from celerytasks import update_agencies, update_routes
7
8
9
10
11
12

migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

13
@manager.command
14
def data_init(agencies_only=False):
15
    from nextbus import Nextbus
16
    start = time.time()
17
    agencies = Nextbus.get_agencies()
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    print("Got {0} agencies from Nextbus in {1:.2f} seconds.".format(len(agencies), time.time() - start))
    if not agencies_only:
        for a in agencies:
            a_start = time.time()
            if a.tag in app.config['AGENCIES']:
                routes = Nextbus.get_routes(a.tag)
                print("Got {0} routes for agency \"{1}\" in {2:.2f} seconds.".format(len(routes), a.tag, time.time() - a_start))
    print("Total time: {0:.2f} seconds".format(time.time() - start))

@manager.command
def update_predictions(loop=False,agencies=None):
    def do_it(agencies):
        from nextbus import Nextbus
        from models import Agency
        start = time.time()
        if agencies:
            agencies = agencies.split(",")
        if not agencies:
            agencies = app.config['AGENCIES']
37
        prediction_count = len(Nextbus.get_predictions(agencies, truncate=False))
38
        elapsed = time.time() - start
39
40
        print("Got {0} predictions for {1} agencies in {2:0.2f} seconds."\
              .format(prediction_count, len(agencies), elapsed))
41
42
43
44
45
    if loop:
        while True:
            do_it(agencies)
    else:
        do_it(agencies)
46

47
48
49
50
51
52
53
54
55
56
@manager.command
def update_vehicle_locations(loop=False,agencies=None):
    def do_it(agencies):
        from nextbus import Nextbus
        from models import Agency
        start = time.time()
        if agencies:
            agencies = agencies.split(",")
        if not agencies:
            agencies = app.config['AGENCIES']
57
        vl_count = len(Nextbus.get_vehicle_locations(agencies, truncate=False))
58
        elapsed = time.time() - start
59
60
        print("Got {0} vehicle locations for {1} agencies in {2:0.2f} seconds."\
              .format(vl_count, len(agencies), elapsed))
61
62
63
64
65
66
    if loop:
        while True:
            do_it(agencies)
    else:
        do_it(agencies)

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@manager.command
def api_quota(tail=False):
    """
    Check the API quota balance.
    Use --tail for updates every 0.25 seconds.
    """
    from nextbus import Nextbus
    if tail:
        try:
            while True:
                remaining_mb = Nextbus.remaining_quota() / 1024**2
                sys.stdout.write("\rNextbus Quota: {0:.3f} MB remaining.".format(remaining_mb))
                sys.stdout.flush()
                time.sleep(0.25)
        except KeyboardInterrupt:
            print("")
83
            sys.exit()
84
85
86
    else:
        remaining_mb = Nextbus.remaining_quota() / 1024**2
        print("Nextbus Quota: {0:.3f} MB remaining.".format(remaining_mb))
87

88
89
if __name__ == "__main__":
    manager.run()