stop_cd_recording.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. from os import kill
  14. from signal import SIGTERM
  15. from time import sleep
  16. from utils import (
  17. get_yyyy_mm_dd_date,
  18. make_sure_file_exists,
  19. is_valid_cd_record_checkfile,
  20. error_msg,
  21. mark_end_of_recording,
  22. get_unix_milis,
  23. warn,
  24. )
  25. from input import get_cachefile_content, validate_cd_record_config
  26. import config as const
  27. def stop_cd_recording() -> None:
  28. cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
  29. yyyy_mm_dd = get_yyyy_mm_dd_date()
  30. if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
  31. unix_milis = get_unix_milis()
  32. last_track_milis = int(cachefile_content[4])
  33. milis_diff = unix_milis - last_track_milis
  34. if milis_diff < const.CD_RECORD_MIN_TRACK_MILIS:
  35. warn(
  36. f"Minimum track length of {const.CD_RECORD_MIN_TRACK_MILIS}"
  37. + "ms not satisfied, sleeping until reached..."
  38. )
  39. sleep(milis_diff / 1000 + 1)
  40. try:
  41. kill(int(cachefile_content[2]), SIGTERM)
  42. except ProcessLookupError:
  43. error_msg("Recording not running, cannot be stopped.")
  44. mark_end_of_recording(cachefile_content)
  45. else:
  46. error_msg("CD Record Checkfile is invalid.")
  47. def main() -> None:
  48. validate_cd_record_config()
  49. make_sure_file_exists(const.CD_RECORD_CACHEFILE)
  50. stop_cd_recording()
  51. if __name__ == "__main__":
  52. main()