tle_reader.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import urllib.request
  2. import sys
  3. import logging
  4. logger = logging.getLogger('tle_fetcher.' + __name__)
  5. class TleReader():
  6. def __init__(self, url_root: str):
  7. self.url_root = url_root
  8. def get_tles(self) -> list:
  9. url = self.url_root + 'active.txt'
  10. tles = []
  11. try:
  12. with urllib.request.urlopen(url) as txt:
  13. i = 0
  14. sat = ''
  15. tle = ''
  16. for line in txt:
  17. line = line.strip().decode('utf-8')
  18. if i % 3 == 0:
  19. sat = line
  20. elif i % 3 == 1:
  21. tle = line
  22. else:
  23. tle += "\n" + line
  24. tles.append((sat, line.split()[1], tle))
  25. i += 1
  26. except Exception as ex:
  27. logger.error("Error getting TLEs: %s", ex)
  28. sys.exit(1)
  29. return tles
  30. def get_group(self, group_name: str) -> list:
  31. url = self.url_root + group_name + '.txt'
  32. group = []
  33. try:
  34. with urllib.request.urlopen(url) as txt:
  35. i = 0
  36. for line in txt:
  37. line = line.strip().decode('utf-8')
  38. if i % 3 == 2:
  39. group.append((line.split()[1], group_name))
  40. i += 1
  41. except Exception as ex:
  42. logger.error("Error getting groups: %s", ex)
  43. sys.exit(1)
  44. return group