aboutsummaryrefslogtreecommitdiff
path: root/bvg-grabber.py
blob: b5aa7106380dcf4d5a6d84796d0fe4f15830a65f (plain)
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import sys

from bvggrabber.api.actualdeparture import ActualDepartureQueryApi

from bvggrabber.api.scheduleddeparture import ScheduledDepartureQueryApi, Vehicle


if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        description='Query the BVG-website for departures')
    parser.add_argument('station', type=str, help='The station to query')
    parser.add_argument('file', type=str, help='Path to file')
    parser.add_argument('--vehicle', type=str, nargs='*',
                        help='''Vehicles which shall be queried,
                              if non given actualdepartue (bus)
                              will be used''')
    parser.add_argument('--limit', type=int, help='Max departures to query')
    args = parser.parse_args()

    query = None
    res = None
    if args.vehicle:
        vehicles = 0
        bus = False
        for vehicle in args.vehicle:
            if vehicle == 'S':
                vehicles |= Vehicle.S
            elif vehicle == 'U':
                vehicles |= Vehicle.U
            elif vehicle == 'TRAM':
                vehicles |= Vehicle.TRAM
            elif vehicle == 'BUS':
                bus = True
            elif vehicle == 'FERRY':
                vehicles |= Vehicle.FERRY
            elif vehicle == 'RB':
                vehicles |= Vehicle.RB
            elif vehicle == 'IC':
                vehicles |= Vehicle.IC
        limit = 9
        if args.limit:
            limit = args.limit

        query = ScheduledDepartureQueryApi(args.station, vehicles, limit=limit)
        res = query.call()
        if bus:
            aquery = ActualDepartureQueryApi(args.station)
            res2 = aquery.call()
            res.merge(res2)
    else:
        query = ActualDepartureQueryApi(args.station)
        res = query.call()

    if args.file in ('stdout', '-'):
        print(res.to_json, file=sys.stdout)
    else:
        file = open(args.file, 'w')
        print(res.to_json, file=file)
        file.close()