stop_cd_recording.py 2.6 KB

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