forked from Athanasius/fd-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd-ed-capi.py
executable file
·210 lines (177 loc) · 7.12 KB
/
fd-ed-capi.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
# vim: textwidth=0 wrapmargin=0 tabstop=2 shiftwidth=2 softtabstop=2 smartindent smarttab
import argparse
import dateutil.parser
import dateutil.utils
import logging
import os
import pathlib
import pprint
import requests
import sys
import time
import yaml
pp = pprint.PrettyPrinter(indent=2, width=10000)
import org.miggy.edcapi
###########################################################################
# Logging
###########################################################################
os.environ['TZ'] = 'UTC'
time.tzset()
__default_loglevel = logging.INFO
__logger = logging.getLogger('fd-api')
__logger.setLevel(__default_loglevel)
__logger_ch = logging.StreamHandler()
__logger_ch.setLevel(__default_loglevel)
__logger_formatter = logging.Formatter('%(asctime)s;%(name)s;%(levelname)s;%(module)s.%(funcName)s: %(message)s')
__logger_formatter.default_time_format = '%Y-%m-%d %H:%M:%S';
__logger_formatter.default_msec_format = '%s.%03d'
__logger_ch.setFormatter(__logger_formatter)
__logger.addHandler(__logger_ch)
###########################################################################
###########################################################################
"""
" Configuration
"""
###########################################################################
__configfile_fd = os.open(pathlib.Path(sys.path[0]) / "fd-api-config.yaml", os.O_RDONLY)
__configfile = os.fdopen(__configfile_fd)
__config = yaml.load(__configfile)
if __config.get('user_agent') is None:
__logger.error('You must set a "user_agent" in the config file')
exit(-1)
###########################################################################
###########################################################################
# Command-Line Arguments
###########################################################################
__parser = argparse.ArgumentParser()
__parser.add_argument("--loglevel", help="set the log level to one of: DEBUG, INFO (default), WARNING, ERROR, CRITICAL")
__parser.add_argument("--rawoutput", action="store_true", help="Output raw returned data")
__parser.add_argument("--pts", action="store_true", help="Use PTS server, not live")
__parser.add_argument("--decode-access-token", action="store_true", help="Decode the currently stored Access Token for the requested Commander")
__parser_endpoints = __parser.add_mutually_exclusive_group(required=False)
__parser_endpoints.add_argument("--endpoints", action="store_true", help="Ask the CAPI server what the currently available endpoints are")
__parser_endpoints.add_argument("--profile", action="store_true", help="Request retrieval of Cmdr's profile")
__parser_endpoints.add_argument("--market", action="store_true", help="Request retrieval of market data")
__parser_endpoints.add_argument("--shipyard", action="store_true", help="Request retrieval of shipyard data")
__parser_endpoints.add_argument("--fleetcarrier", action="store_true", help="Request retrieval of fleetcarrier data")
__parser_endpoints.add_argument(
"--journal",
metavar="date",
nargs="?",
default=False,
help="Request retrieval of journal data. Defaults to 'today' if no 'date' is given, else the string is parsed per python dateutil.parser capabilities.",
)
__parser_endpoints.add_argument("--communitygoals", action="store_true", help="Request retrieval of Community Goals data")
__parser.add_argument("cmdrname", nargs=1, help="Specify the Cmdr Name for this Authorization")
__args = __parser.parse_args()
if __args.loglevel:
__level = getattr(logging, __args.loglevel.upper())
__logger.setLevel(__level)
__logger_ch.setLevel(__level)
__logger.debug('Args: {!r}'.format(__args))
cmdrname = __args.cmdrname[0]
###########################################################################
###########################################################################
# Load a relevant Auth State
###########################################################################
def loadAuthState(cmdr: str) -> int:
########################################
# Retrieve and test state
########################################
db = edcapi.database(__logger, __config)
auth_state = db.getActiveTokenState()
if auth_state:
## Do we have an access_token, and does it work?
if auth_state['access_token']:
print("Found un-expired access_token, assuming it's good.")
return(0)
else:
print("Un-expired access_token, but no access_token? WTF!")
return(-1)
else:
print("No auth state with un-expired access_token found, continuing...")
########################################
###########################################################################
###########################################################################
# Main
###########################################################################
def main():
__logger.debug("Start")
# Set the required capi_url
if __args.pts:
__config['capi_url'] = __config['capi_urls']['pts']
else:
__config['capi_url'] = __config['capi_urls']['live']
capi = org.miggy.edcapi.edcapi(__logger, __config)
if __args.decode_access_token:
token_details = capi.decode(cmdrname)
print(f'{token_details}')
if __args.endpoints:
rawep, ep = capi.endpoints.get(cmdrname)
if __args.rawoutput:
print(f'{rawep}\n')
else:
print(pprint.pformat(ep, width=79))
if __args.profile:
(rawprofile, profile) = capi.profile.get(cmdrname)
if not profile:
return -1
if __args.rawoutput:
print(rawprofile)
print('')
else:
print(pp.pformat(profile))
if __args.market:
(rawmarket, market) = capi.market.get(cmdrname)
if not market:
return -1
if __args.rawoutput:
print(rawmarket)
print('')
else:
print(pp.pformat(market))
if __args.shipyard:
(rawshipyard, shipyard) = capi.shipyard.get(cmdrname)
if not shipyard:
return -1
if __args.rawoutput:
print(rawshipyard)
print('')
else:
print(pp.pformat(shipyard))
if __args.fleetcarrier:
(rawfleetcarrier, fleetcarrier) = capi.fleetcarrier.get(cmdrname)
if not fleetcarrier:
return -1
if __args.rawoutput:
print(rawfleetcarrier)
print('')
else:
print(pp.pformat(fleetcarrier))
# You get 'False' if not present at all, 'None' if no optional arg
if __args.journal != False:
# Validate the date format
if __args.journal:
try:
j_date = dateutil.parser.parse(__args.journal)
except Exception as e:
__logger.error("Could not parse the string '{date}' into a date".format(date=__args.journal))
return -1
else:
j_date = dateutil.utils.today()
datestr = j_date.strftime("%Y/%m/%d")
__logger.debug('Retrieving journals for date "{date}"'.format(date=datestr))
rawjournal = capi.journal.get(cmdrname, datestr)
if not rawjournal:
return -1
print('{journal}'.format(journal=rawjournal))
if __args.communitygoals:
rawcgs, cgs = capi.communitygoals.get(cmdrname)
if __args.rawoutput:
print(f'{rawcgs}\n')
else:
print(pprint.pformat(cgs, width=79))
###########################################################################
if __name__ == '__main__':
exit(main())