scripts.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. Copyright © 2022 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. """
  14. from os import path
  15. from time import sleep
  16. from re import match
  17. from pyautogui import keyDown, keyUp
  18. from utils import error_msg, log, get_yyyy_mm_dd_date
  19. import config as const
  20. def make_sure_file_exists(cachefile: str) -> None:
  21. if not path.isfile(cachefile):
  22. try:
  23. with open(
  24. cachefile, mode="w+", encoding="utf-8-sig"
  25. ) as file_creator:
  26. file_creator.write("")
  27. except (FileNotFoundError, PermissionError, IOError) as error:
  28. error_msg(
  29. "Failed to create file in '{}'. Reason: {}".format(
  30. cachefile, error
  31. )
  32. )
  33. def switch_to_song(song_number: int) -> None:
  34. if song_number > const.OBS_MIN_SUBDIRS:
  35. song_number = 1
  36. log("sending hotkey to switch to scene {}".format(song_number), "cyan")
  37. scene_switch_hotkey = list(const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX)
  38. scene_switch_hotkey.append("f{}".format(song_number))
  39. safe_send_hotkey(scene_switch_hotkey)
  40. log("sending hotkey to transition to scene {}".format(song_number), "cyan")
  41. safe_send_hotkey(const.OBS_TRANSITION_HOTKEY)
  42. create_cachfile_for_song(song_number)
  43. def safe_send_hotkey(hotkey: list, sleep_time=0.1) -> None:
  44. for key in hotkey:
  45. keyDown(key)
  46. sleep(sleep_time)
  47. for key in hotkey:
  48. keyUp(key)
  49. def create_cachfile_for_song(song) -> None:
  50. log("writing song {} to cachefile...".format(song))
  51. try:
  52. with open(
  53. const.NEXTSONG_CACHE_FILE, mode="w", encoding="utf-8-sig"
  54. ) as file_writer:
  55. file_writer.write(get_yyyy_mm_dd_date() + "\n")
  56. file_writer.write(str(song) + "\n")
  57. except (FileNotFoundError, PermissionError, IOError) as error:
  58. error_msg(
  59. "Failed to write to cachefile '{}'. Reason: {}".format(
  60. const.NEXTSONG_CACHE_FILE, error
  61. )
  62. )
  63. def is_valid_cd_record_checkfile(
  64. cachefile_content: list, yyyy_mm_dd: str
  65. ) -> bool:
  66. return (
  67. len(cachefile_content) == 5
  68. # YYYY-MM-DD
  69. and bool(match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0]))
  70. # last marker
  71. and bool(match(r"^[0-9]+$", cachefile_content[1]))
  72. # pid of ffmpeg recording instance
  73. and bool(match(r"^[0-9]+$", cachefile_content[2]))
  74. # unix milis @ recording start
  75. and bool(match(r"^[0-9]+$", cachefile_content[3]))
  76. # unix milis @ last track
  77. and bool(match(r"^[0-9]+$", cachefile_content[4]))
  78. # date matches today
  79. and cachefile_content[0].strip() == yyyy_mm_dd
  80. )