Skip to content

Instantly share code, notes, and snippets.

@JaniniRami
Last active July 13, 2024 07:29
Show Gist options
  • Save JaniniRami/229d93365df5b100afb7bca660a25a45 to your computer and use it in GitHub Desktop.
Save JaniniRami/229d93365df5b100afb7bca660a25a45 to your computer and use it in GitHub Desktop.
Download all TLE lines from CelesTrak.com.
import json
import requests
tle_urls = ['http://www.celestrak.com/NORAD/elements/active.txt',
'http://celestrak.com/NORAD/elements/weather.txt',
'http://celestrak.com/NORAD/elements/resource.txt',
'https://www.celestrak.com/NORAD/elements/cubesat.txt',
'http://celestrak.com/NORAD/elements/stations.txt',
'https://www.celestrak.com/NORAD/elements/sarsat.txt',
'https://www.celestrak.com/NORAD/elements/noaa.txt',
'https://www.celestrak.com/NORAD/elements/amateur.txt',
'https://www.celestrak.com/NORAD/elements/engineering.txt']
def download_tle():
tle_json = []
for url in tle_urls:
request = requests.get(url)
tmp_dict = {}
for i in request.text.split('\n'):
try:
if i[0] == '1':
tmp_dict['tle_1'] = i.strip()
elif i[0] == '2':
tmp_dict['tle_2'] = i.strip()
else:
tmp_dict['satellite_name'] = i.strip()
if "tle_1" in tmp_dict and "tle_2" in tmp_dict and "satellite_name" in tmp_dict:
tle_json.append(tmp_dict)
tmp_dict = {}
else:
pass
except:
pass
with open('celes_tle.json', 'w') as f:
json.dump(tle_json, f, indent=3)
print('[+] Downloaded TLE data in celes_tle.json')
if __name__ == '__main__':
print('[+] Downloading TLE data...')
download_tle()
@Rhett12345
Copy link

Excuse me, now on July 12, 2024, the urls in your code (http://www.celestrak.com/NORAD/elements/active.txt etc.) are no longer accessible, is there any way to crawl the historical TLE data from celestrak, if you know, you can let me know, thank you very much.

@JaniniRami
Copy link
Author

Hello @Rhett12345, this gist is pretty old and all the links have been probably changed since then, here is a quick documentation on the new way to obtain the TLE data from CelesTrak's website, also you can check SpaceTrack, it's well maintained, but somewhat restricted on the process of using their data in public projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment