scripts.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, listdir
  14. from time import sleep
  15. from re import match
  16. from enum import Enum
  17. from dataclasses import dataclass
  18. from pyautogui import keyDown, keyUp
  19. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  20. QApplication,
  21. QMessageBox,
  22. )
  23. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  24. QDialog,
  25. )
  26. from utils import (
  27. log,
  28. error_msg,
  29. get_yyyy_mm_dd_date,
  30. expand_dir,
  31. )
  32. from input import RadioButtonDialog, get_cachefile_content, InfoMsgBox
  33. import config as const
  34. def make_sure_file_exists(cachefile: str) -> None:
  35. if not path.isfile(cachefile):
  36. try:
  37. with open(
  38. cachefile, mode="w+", encoding="utf-8-sig"
  39. ) as file_creator:
  40. file_creator.write("")
  41. except (FileNotFoundError, PermissionError, IOError) as error:
  42. error_msg(
  43. "Failed to create file in '{}'. Reason: {}".format(
  44. cachefile, error
  45. )
  46. )
  47. class SongDirection(Enum):
  48. PREVIOUS = "previous"
  49. NEXT = "next"
  50. def cycle_to_song_direction(song_direction: SongDirection):
  51. cachefile_content = get_cachefile_content(const.NEXTSONG_CACHE_FILE)
  52. if song_direction == SongDirection.PREVIOUS:
  53. step = -1
  54. else:
  55. step = 1
  56. if (
  57. not (
  58. len(cachefile_content) == 2
  59. and match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0])
  60. and match(r"^[0-9]+$", cachefile_content[1])
  61. )
  62. or cachefile_content[0].strip() != get_yyyy_mm_dd_date()
  63. ):
  64. switch_to_song(1)
  65. else:
  66. switch_to_song(int(cachefile_content[1]) + step)
  67. def switch_to_song(song_number: int) -> None:
  68. if song_number > const.OBS_MIN_SUBDIRS:
  69. song_number = 1
  70. if song_number < 1:
  71. song_number = const.OBS_MIN_SUBDIRS
  72. log("sending hotkey to switch to scene {}".format(song_number), "cyan")
  73. scene_switch_hotkey = list(const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX)
  74. scene_switch_hotkey.append("f{}".format(song_number))
  75. safe_send_hotkey(scene_switch_hotkey)
  76. log("sending hotkey to transition to scene {}".format(song_number), "cyan")
  77. safe_send_hotkey(const.OBS_TRANSITION_HOTKEY)
  78. create_cachfile_for_song(song_number)
  79. def create_cachfile_for_song(song) -> None:
  80. log("writing song {} to cachefile...".format(song))
  81. cachefile = expand_dir(const.NEXTSONG_CACHE_FILE)
  82. try:
  83. with open(cachefile, mode="w", encoding="utf-8-sig") as file_writer:
  84. file_writer.write(get_yyyy_mm_dd_date() + "\n")
  85. file_writer.write(str(song) + "\n")
  86. except (FileNotFoundError, PermissionError, IOError) as error:
  87. app = QApplication
  88. InfoMsgBox(
  89. QMessageBox.Critical,
  90. "Error",
  91. "Failed to write to cachefile '{}'. Reason: {}".format(
  92. cachefile, error
  93. ),
  94. )
  95. del app
  96. sys.exit(1)
  97. def safe_send_hotkey(hotkey: list, sleep_time=0.1) -> None:
  98. for key in hotkey:
  99. keyDown(key)
  100. sleep(sleep_time)
  101. for key in hotkey:
  102. keyUp(key)