-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_track_api_script.py
More file actions
93 lines (73 loc) · 3.26 KB
/
space_track_api_script.py
File metadata and controls
93 lines (73 loc) · 3.26 KB
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
##
## SLTrack.py
## (c) 2019 Andrew Stokes All Rights Reserved
## Taken from https://www.space-track.org/documentation#howto-api_python
##
##
## Simple Python app to extract Starlink satellite history data from www.space-track.org into a spreadsheet
## (Note action for you in the code below, to set up a config file with your access and output details)
##
##
## Copyright Notice:
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## For full licencing terms, please refer to the GNU General Public License
## (gpl-3_0.txt) distributed with this release, or see
## http://www.gnu.org/licenses/.
##
import requests
import configparser
class MyError(Exception):
def __init___(self, args):
Exception.__init__(self,"my exception was raised with arguments {0}".format(args))
self.args = args
# See https://www.space-track.org/documentation for details on REST queries
uriBase = "https://www.space-track.org"
requestLogin = "/ajaxauth/login"
requestCmdAction = "/basicspacedata/query"
# Use configparser package to pull in the ini file (pip install configparser)
config = configparser.ConfigParser()
config.read("./SLTrack.ini")
configUsr = config.get("configuration", "username")
configPwd = config.get("configuration", "password")
siteCred = {'identity': configUsr, 'password': configPwd}
def space_track_api_request(start, end, norad_ids):
"""
Return Three line elements from SpaceTrack, using their API access.
:param start: [str] start date of TLE data, in the format "YYYY-MM-DD"
:param end: [str] end date of TLE data, in the format "YYYY-MM-DD"
:param norad_ids: [str] String containing the Norad IDs of satellites (comma-sep.)
:return:
"""
# norad_ids = str(norad_ids).strip("[]").replace(" ", "")
request_ = f"/class/gp_history/NORAD_CAT_ID/{norad_ids}/orderby/TLE_LINE1 ASC/EPOCH/{start}--{end}/format/3le"
# use requests package to drive the RESTful session with space-track.org
with requests.Session() as session:
# run the session in a with block to force session to close if we exit
# need to log in first. note that we get a 200 to say the website got the data,
# not that we are logged in
resp = session.post(uriBase + requestLogin, data=siteCred)
if resp.status_code != 200:
raise MyError(resp, "POST fail on login")
# this query picks up all satellites from the catalog. Note - a 401
# failure shows you have bad credentials
resp = session.get(uriBase + requestCmdAction + request_)
if resp.status_code != 200:
print(resp)
raise MyError(resp, "GET fail on request for satellites")
return resp
print("Completed TLE request session")
if __name__ == "__main__":
start = "2023-03-01"
end = "2023-03-02"
#name = "FLOCK"
name = "SENTINEL"