songchooser.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 pyautogui import keyDown, keyUp
  17. from utils import error_msg, log, calculate_yyyy_mm_dd_date
  18. import config as const
  19. def make_sure_cachefile_exists() -> None:
  20. if not path.isfile(const.NEXTSONG_CACHE_FILE):
  21. try:
  22. with open(
  23. const.NEXTSONG_CACHE_FILE, mode="w+", encoding="utf8"
  24. ) as file_creator:
  25. file_creator.write("")
  26. except (FileNotFoundError, PermissionError, IOError) as error:
  27. error_msg(
  28. "Failed to create cachefile in '{}'. Reason: {}".format(
  29. const.NEXTSONG_CACHE_FILE, error
  30. )
  31. )
  32. def switch_to_song(song_number: int) -> None:
  33. if song_number > const.OBS_MIN_SUBDIRS:
  34. song_number = 1
  35. log("sending hotkey to switch to scene {}".format(song_number), "cyan")
  36. scene_switch_hotkey = list(const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX)
  37. scene_switch_hotkey.append("f{}".format(song_number))
  38. safe_send_hotkey(scene_switch_hotkey)
  39. log("sending hotkey to transition to scene {}".format(song_number), "cyan")
  40. safe_send_hotkey(const.OBS_TRANSITION_HOTKEY)
  41. create_cachfile_for_song(song_number)
  42. def safe_send_hotkey(hotkey: list, sleep_time=0.1) -> None:
  43. for key in hotkey:
  44. keyDown(key)
  45. sleep(sleep_time)
  46. for key in hotkey:
  47. keyUp(key)
  48. def create_cachfile_for_song(song) -> None:
  49. log("writing song {} to cachefile...".format(song))
  50. try:
  51. with open(
  52. const.NEXTSONG_CACHE_FILE, mode="w", encoding="utf8"
  53. ) as file_writer:
  54. file_writer.write(calculate_yyyy_mm_dd_date() + "\n")
  55. file_writer.write(str(song) + "\n")
  56. except (FileNotFoundError, PermissionError, IOError) as error:
  57. error_msg(
  58. "Failed to write to cachefile '{}'. Reason: {}".format(
  59. const.NEXTSONG_CACHE_FILE, error
  60. )
  61. )