-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitibike-trips
executable file
·134 lines (116 loc) · 3.68 KB
/
citibike-trips
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
import argparse
from citibike_trips import CitibikeTrips
import logging
import json
logging.basicConfig(level=logging.WARN, format="%(asctime)s %(message)s")
log = logging.getLogger()
parser = argparse.ArgumentParser(description="Citibike personal trip history download.")
parser.add_argument("-u", "--username", required=False, type=str, help="Citibike account username")
parser.add_argument("-p", "--password", required=False, type=str, help="Citibike account password")
parser.add_argument("-c", "--config", required=False, type=str, help="Config file")
parser.add_argument("-j", "--jar", required=False, type=str, help="Path to cookie jar file")
parser.add_argument(
"-v", "--verbose", required=False, default=False, action="store_true", help="Enable verbose output",
)
parser.add_argument(
"-d", "--debug", required=False, default=False, action="store_true", help="Enable debug output",
)
parser.add_argument(
"-r",
"--recent",
required=False,
type=int,
help="Number of recent trip pages to get. Defaults to 1, use 0 for all.",
)
parser.add_argument(
"-a", "--account", required=False, default=False, action="store_true", help="Show account data instead of trips.",
)
parser.add_argument(
"-b",
"--bikeangels",
required=False,
# default=False,
action="store_true",
help="Collect Bike Angels stats from profile",
)
parser.add_argument(
"-x", "--extended", required=False, action="store_true", help="Enable extended reporting format",
)
parser.add_argument(
"-k", "--keep", required=False, default=False, type=str, help="Keep retrieved files in this cache dir",
)
parser.add_argument(
"-o", "--output", required=False, default="json", type=str, help="Output in json or csv",
)
args = parser.parse_args()
if args.verbose:
log.setLevel(logging.INFO)
if args.debug:
log.setLevel(logging.DEBUG)
config = {
"username": None,
"password": None,
"ba": None,
"jar": None,
"keep": None,
"output": None,
"verbose": bool(args.verbose),
"debug": bool(args.debug),
"extended": None,
"account": False,
"recent": 1,
}
if args.config:
log.debug("Config from {}".format(args.config))
config_file = args.config
with open(config_file, "r") as f:
config.update(json.load(f))
else:
log.debug("Config from defaults")
# let command line override config file
if args.username:
config["username"] = args.username
if args.password:
config["password"] = args.password
if config["username"] is None or config["password"] is None:
log.error("Need username and password")
exit(1)
else:
log.debug("Got user: {} and pass: {}".format(config["username"], config["password"]))
if args.recent or args.recent == 0:
config["last_page"] = args.recent
config["recent"] = args.recent
else:
config["last_page"] = 1
config["recent"] = 1
if args.bikeangels:
config["ba"] = args.bikeangels
if args.jar:
config["jar"] = args.jar
if args.account:
config["account"] = args.account
if args.extended:
config["extended"] = args.extended
if args.keep:
config["keep"] = args.keep
if args.output not in ("json", "csv"):
log.error("Output must be one of json or csv")
exit(1)
log.debug("Calling cb with config {}".format(config))
cb = CitibikeTrips(
username=config["username"],
password=config["password"],
ba=config["ba"],
jar=config["jar"],
keep=config["keep"],
output=config["output"],
recent=config["recent"],
extended=config["extended"],
verbose=config["verbose"],
debug=config["debug"],
)
if config["account"]:
print(json.dumps(cb.get_account()))
else:
print(json.dumps(cb.get_trips(last_page=config["last_page"])))