-
Notifications
You must be signed in to change notification settings - Fork 1
/
refreshTLEs.py
executable file
·60 lines (49 loc) · 2.04 KB
/
refreshTLEs.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
#!/usr/bin/env python3
import os
import sys
import time
import argparse
from urllib.request import urlretrieve
def main(args):
# Figure out what do to
toRefresh = []
for filename in ('visual.txt', 'science.txt', 'resource.txt', 'geo.txt', 'starlink.txt'):
if os.path.exists(filename):
## Get the age of the file
mtime = os.stat(filename)[8]
age = time.time() - mtime
print(f"File '{filename}' last modified {age/86400:.1f} days ago")
## Does this file need to be refreshed?
if age > args.age or args.force:
toRefresh.append( filename )
print("-> adding to update list")
else:
print("-> skipping")
else:
toRefresh.append( filename )
print(f"File '{filename}' does not exist")
print("-> adding to update list")
print(" ")
# Do it
for filename in toRefresh:
## Download the file and extract its contents
print(f"Downloading '{filename}' from 'http://celestrak.com'")
tlename = os.path.splitext(filename)[0]
url = f"https://celestrak.org/NORAD/elements/gp.php?GROUP={tlename}&FORMAT=tle"
t0 = time.time()
urlretrieve(url, filename)
t1 = time.time()
sz = os.path.getsize(filename)
print(f"-> downloaded {sz} bytes in {t1-t0:.3f} s ({sz/1024/(t1-t0):.1f} kB/s)")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='look at the CelesTrak TLEs in the current directory and refresh old ones',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('-f', '--force', action='store_true',
help='force re-downloading the TLEs')
parser.add_argument('-a', '--age', type=int, default=2,
help='age limit in days for refreshing a file')
args = parser.parse_args()
args.age *= 86400 # days -> seconds
main(args)