stop_cd_recording.py 2.1 KB

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