switch.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 re import match
  14. from enum import Enum
  15. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  16. QApplication,
  17. QMessageBox,
  18. )
  19. from utils import (
  20. log,
  21. get_yyyy_mm_dd_date,
  22. expand_dir,
  23. )
  24. from input import get_cachefile_content, InfoMsgBox
  25. import config as const
  26. from .obs import safe_send_hotkey
  27. class SongDirection(Enum):
  28. PREVIOUS = "previous"
  29. NEXT = "next"
  30. def cycle_to_song_direction(song_direction: SongDirection):
  31. cachefile_content = get_cachefile_content(const.NEXTSONG_CACHE_FILE)
  32. if song_direction == SongDirection.PREVIOUS:
  33. step = -1
  34. else:
  35. step = 1
  36. if (
  37. not (
  38. len(cachefile_content) == 2
  39. and match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0])
  40. and match(r"^[0-9]+$", cachefile_content[1])
  41. )
  42. or cachefile_content[0].strip() != get_yyyy_mm_dd_date()
  43. ):
  44. switch_to_song(1)
  45. else:
  46. switch_to_song(int(cachefile_content[1]) + step)
  47. def switch_to_song(song_number: int) -> None:
  48. if song_number > const.OBS_MIN_SUBDIRS:
  49. song_number = 1
  50. if song_number < 1:
  51. song_number = const.OBS_MIN_SUBDIRS
  52. log("sending hotkey to switch to scene {}".format(song_number), "cyan")
  53. scene_switch_hotkey = list(const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX)
  54. scene_switch_hotkey.append("f{}".format(song_number))
  55. safe_send_hotkey(scene_switch_hotkey)
  56. log("sending hotkey to transition to scene {}".format(song_number), "cyan")
  57. safe_send_hotkey(const.OBS_TRANSITION_HOTKEY)
  58. create_cachfile_for_song(song_number)
  59. def create_cachfile_for_song(song) -> None:
  60. log("writing song {} to cachefile...".format(song))
  61. cachefile = expand_dir(const.NEXTSONG_CACHE_FILE)
  62. try:
  63. with open(cachefile, mode="w", encoding="utf-8-sig") as file_writer:
  64. file_writer.write(get_yyyy_mm_dd_date() + "\n")
  65. file_writer.write(str(song) + "\n")
  66. except (FileNotFoundError, PermissionError, IOError) as error:
  67. app = QApplication
  68. InfoMsgBox(
  69. QMessageBox.Critical,
  70. "Error",
  71. "Failed to write to cachefile '{}'. Reason: {}".format(
  72. cachefile, error
  73. ),
  74. )
  75. del app
  76. sys.exit(1)