songchooser.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 pyautogui import hotkey
  16. from utils import error_msg, log, calculate_yyyy_mm_dd_date
  17. import config as const
  18. def make_sure_cachefile_exists() -> None:
  19. if not path.isfile(const.NEXTSONG_CACHE_FILE):
  20. try:
  21. with open(
  22. const.NEXTSONG_CACHE_FILE, mode="w+", encoding="utf8"
  23. ) as file_creator:
  24. file_creator.write("")
  25. except (FileNotFoundError, PermissionError, IOError) as error:
  26. error_msg(
  27. "Failed to create cachefile in '{}'. Reason: {}".format(
  28. const.NEXTSONG_CACHE_FILE, error
  29. )
  30. )
  31. def switch_to_song(song: int) -> None:
  32. if song > const.OBS_MIN_SUBDIRS:
  33. song = 1
  34. log("sending hotkey Ctr + Shift + F{}".format(song), color="cyan")
  35. hotkey("ctrl", "shift", "f{}".format(song))
  36. create_cachfile_for_song(song)
  37. def create_cachfile_for_song(song) -> None:
  38. log("writing song {} to cachefile...".format(song))
  39. try:
  40. with open(
  41. const.NEXTSONG_CACHE_FILE, mode="w", encoding="utf8"
  42. ) as file_writer:
  43. file_writer.write(calculate_yyyy_mm_dd_date() + "\n")
  44. file_writer.write(str(song) + "\n")
  45. except (FileNotFoundError, PermissionError, IOError) as error:
  46. error_msg(
  47. "Failed to write to cachefile '{}'. Reason: {}".format(
  48. const.NEXTSONG_CACHE_FILE, error
  49. )
  50. )