verify.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import sys
  13. from os import path
  14. from re import match
  15. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  16. QMessageBox,
  17. )
  18. from psutil import pid_exists
  19. import config as const
  20. from utils import get_current_yyyy_mm_dd_date, InfoMsgBox, expand_dir
  21. from input import get_cachefile_content
  22. def calc_cuesheet_timestamp(start_milis: int, current_milis: int) -> str:
  23. milis_diff = current_milis - start_milis
  24. mins = milis_diff // 60000
  25. milis_diff -= 60000 * mins
  26. secs = int(milis_diff / 1000)
  27. milis_diff -= 1000 * secs
  28. frames = int(75 / 1000 * milis_diff)
  29. return "{:02d}:{:02d}:{:02d}\n".format(mins, secs, frames)
  30. def is_valid_cd_record_checkfile(
  31. cachefile_content: list, yyyy_mm_dd: str
  32. ) -> bool:
  33. return (
  34. len(cachefile_content) == 6
  35. # YYYY-MM-DD
  36. and bool(match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0]))
  37. # last set marker
  38. and bool(match(r"^[0-9][0-9]?$", cachefile_content[1]))
  39. # pid of ffmpeg recording instance
  40. and bool(match(r"^[0-9]+$", cachefile_content[2]))
  41. # unix milis @ recording start
  42. and bool(match(r"^[0-9]+$", cachefile_content[3]))
  43. # unix milis @ last track
  44. and bool(match(r"^[0-9]+$", cachefile_content[4]))
  45. # cd number
  46. and bool(match(r"^[0-9]+$", cachefile_content[5]))
  47. # date matches today
  48. and cachefile_content[0].strip() == yyyy_mm_dd
  49. )
  50. def ongoing_cd_recording_detected() -> bool:
  51. if path.isfile(expand_dir(const.CD_RECORD_CACHEFILE)):
  52. cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
  53. if is_valid_cd_record_checkfile(
  54. cachefile_content, get_current_yyyy_mm_dd_date()
  55. ):
  56. if cachefile_content[1].strip() != "9001" and pid_exists(
  57. int(cachefile_content[2].strip())
  58. ):
  59. return True
  60. return False
  61. def make_sure_there_is_no_ongoing_cd_recording() -> None:
  62. if ongoing_cd_recording_detected():
  63. InfoMsgBox(
  64. QMessageBox.Critical,
  65. "Error",
  66. "Error: Ongoing CD Recording detected",
  67. )
  68. sys.exit(1)
  69. def is_legal_sheet_filename(filename: str) -> bool:
  70. return bool(match(r"^sheet-[0-9]+\.cue", filename)) and len(filename) == 17
  71. def get_padded_cd_num_from_sheet_filename(filename: str) -> str:
  72. if not is_legal_sheet_filename(filename):
  73. InfoMsgBox(
  74. QMessageBox.Critical,
  75. "Error",
  76. f"Error: filename '{filename}' in illegal format",
  77. )
  78. sys.exit(1)
  79. return filename[6:13]