tle_database.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import mariadb
  2. import logging
  3. import sys
  4. logger = logging.getLogger("tle_fetcher." + __name__)
  5. class TleDatabase:
  6. def __init__(self):
  7. self.conn = None
  8. def connect_db(self):
  9. # Connect to MariaDB
  10. try:
  11. conn = mariadb.connect(
  12. user="tle_fetcher",
  13. host="localhost",
  14. port=3306,
  15. database="celestrak_tles",
  16. )
  17. except mariadb.Error as e:
  18. logger.error("Error connecting to MariaDB Platform: %s", e.msg)
  19. sys.exit(1)
  20. self.conn = conn
  21. def delete_rows(self):
  22. cur = self.conn.cursor()
  23. try:
  24. cur.execute("delete from sat_groups")
  25. cur.execute("delete from tles")
  26. except mariadb.Error as e:
  27. logger.error("Error clearing tables: %s", e.msg)
  28. self.conn.commit()
  29. def update_tles(self, tles: list):
  30. cur = self.conn.cursor()
  31. try:
  32. cur.executemany(
  33. "insert into tles (sat, sat_cat_num, tle) values (?,?,?)", tles
  34. )
  35. except mariadb.Error as e:
  36. logger.error("Error updating TLEs: %s", e.msg)
  37. self.conn.commit()
  38. def update_sat_group(self, group: list):
  39. cur = self.conn.cursor()
  40. try:
  41. cur.executemany(
  42. "insert ignore into sat_groups (sat_cat_num, group_name) values (?,?)",
  43. group,
  44. )
  45. except mariadb.Error as e:
  46. logger.error("Error updating groups: %s", e.msg)
  47. self.conn.commit()
  48. def close_db(self):
  49. self.conn.close()