stop_cd_recording.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import sys
  14. from os import kill
  15. from signal import SIGTERM
  16. from time import sleep
  17. import colorama
  18. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  19. QApplication,
  20. QMessageBox,
  21. )
  22. from utils import (
  23. get_yyyy_mm_dd_date,
  24. make_sure_file_exists,
  25. get_unix_milis,
  26. warn,
  27. expand_dir,
  28. InfoMsgBox,
  29. )
  30. from input import get_cachefile_content, validate_cd_record_config
  31. import config as const
  32. from recording import is_valid_cd_record_checkfile, mark_end_of_recording
  33. def stop_cd_recording() -> None:
  34. filename = expand_dir(const.CD_RECORD_CACHEFILE)
  35. cachefile_content = get_cachefile_content(filename)
  36. yyyy_mm_dd = get_yyyy_mm_dd_date()
  37. if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
  38. unix_milis = get_unix_milis()
  39. last_track_milis = int(cachefile_content[4])
  40. milis_diff = unix_milis - last_track_milis
  41. if milis_diff < const.CD_RECORD_MIN_TRACK_MILIS:
  42. warn(
  43. f"Minimum track length of {const.CD_RECORD_MIN_TRACK_MILIS}"
  44. + "ms not satisfied, sleeping until reached..."
  45. )
  46. sleep(milis_diff / 1000 + 1)
  47. try:
  48. kill(int(cachefile_content[2]), SIGTERM)
  49. except ProcessLookupError:
  50. app = QApplication
  51. InfoMsgBox(
  52. QMessageBox.Critical,
  53. "Error",
  54. "Recording not running, cannot be stopped.",
  55. )
  56. del app
  57. sys.exit(1)
  58. mark_end_of_recording(cachefile_content)
  59. else:
  60. app = QApplication
  61. InfoMsgBox(
  62. QMessageBox.Critical,
  63. "Error",
  64. f"CD Record Checkfile {filename} is invalid.",
  65. )
  66. del app
  67. sys.exit(1)
  68. if __name__ == "__main__":
  69. colorama.init()
  70. validate_cd_record_config()
  71. make_sure_file_exists(const.CD_RECORD_CACHEFILE, gui_error_out=True)
  72. stop_cd_recording()