stop_cd_recording.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. error_msg,
  20. get_unix_milis,
  21. warn,
  22. )
  23. from input import get_cachefile_content, validate_cd_record_config
  24. import config as const
  25. from recording import is_valid_cd_record_checkfile, mark_end_of_recording
  26. def stop_cd_recording() -> None:
  27. cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
  28. yyyy_mm_dd = get_yyyy_mm_dd_date()
  29. if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
  30. unix_milis = get_unix_milis()
  31. last_track_milis = int(cachefile_content[4])
  32. milis_diff = unix_milis - last_track_milis
  33. if milis_diff < const.CD_RECORD_MIN_TRACK_MILIS:
  34. warn(
  35. f"Minimum track length of {const.CD_RECORD_MIN_TRACK_MILIS}"
  36. + "ms not satisfied, sleeping until reached..."
  37. )
  38. sleep(milis_diff / 1000 + 1)
  39. try:
  40. kill(int(cachefile_content[2]), SIGTERM)
  41. except ProcessLookupError:
  42. error_msg("Recording not running, cannot be stopped.")
  43. mark_end_of_recording(cachefile_content)
  44. else:
  45. error_msg("CD Record Checkfile is invalid.")
  46. def main() -> None:
  47. validate_cd_record_config()
  48. make_sure_file_exists(const.CD_RECORD_CACHEFILE)
  49. stop_cd_recording()
  50. if __name__ == "__main__":
  51. main()