ssync.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import shutil
  5. from configparser import ConfigParser
  6. from termcolor import colored
  7. def error_msg(msg: str):
  8. print(colored("[*] Error: {}".format(msg), "red"))
  9. sys.exit(1)
  10. def log(message: str):
  11. print(colored("[*] {}".format(message), "green"))
  12. class Ssync:
  13. def __init__(self):
  14. self.parse_config()
  15. def parse_config(self):
  16. config_parser = ConfigParser()
  17. config_parser.read("config.ini")
  18. try:
  19. self.rclone_remote_dir = config_parser["RCLONE"]["remote_dir"]
  20. self.rclone_local_dir = config_parser["RCLONE"]["local_dir"]
  21. self.obs_slides_dir = config_parser["OBS"]["slides_dir"]
  22. self.obs_target_subdir = config_parser["OBS"]["target_subdir"]
  23. except KeyError:
  24. error_msg("configuration file 'config.ini' could not be parsed")
  25. log("configuration initialised")
  26. def sync_slide_repo(self):
  27. log("syncing with remote slide repository...")
  28. os.system(
  29. "rclone sync -v {} {}".format(
  30. self.rclone_remote_dir, self.rclone_local_dir
  31. )
  32. )
  33. def clear_obs_slides_dir(self):
  34. log("clearing obs slides directory...")
  35. for filename in os.listdir(self.obs_slides_dir):
  36. file_path = os.path.join(self.obs_slides_dir, filename)
  37. try:
  38. if os.path.isfile(file_path) or os.path.islink(file_path):
  39. os.unlink(file_path)
  40. elif os.path.isdir(file_path):
  41. shutil.rmtree(file_path)
  42. except Exception as e:
  43. error_msg("Failed to delete %s. Reason: %s" % (file_path, e))
  44. def slide_selection_iterator(self):
  45. iterator_prompt = "Do you want to exit now? (default=no) [y/N]: "
  46. dir_list_str = ""
  47. for directory in os.listdir(self.rclone_local_dir):
  48. dir_list_str += directory + "\n"
  49. dir_list_str = dir_list_str[:-1]
  50. tempfile_str = ".chosen-tempfile"
  51. index = 0
  52. while True:
  53. index += 1
  54. prompt_answer = str(
  55. input("[Song {}] ".format(index) + iterator_prompt)
  56. )
  57. if prompt_answer.lower() == "y":
  58. break
  59. os.system("echo '{}' | fzf > {}".format(dir_list_str, tempfile_str))
  60. with open(tempfile_str, "r") as file_opener:
  61. chosen_slides = file_opener.read()[:-1].strip()
  62. if len(chosen_slides) == 0:
  63. log("no slides chosen, skipping")
  64. else:
  65. log(
  66. "copying slides '{}' to '{} {}'...".format(
  67. chosen_slides, self.obs_target_subdir, index
  68. )
  69. )
  70. src_dir = self.rclone_local_dir + "/" + chosen_slides
  71. dest_dir = (
  72. self.obs_slides_dir
  73. + "/"
  74. + self.obs_target_subdir
  75. + " "
  76. + str(index)
  77. )
  78. shutil.copytree(src_dir, dest_dir)
  79. if os.path.isfile(tempfile_str):
  80. os.remove(tempfile_str)
  81. def execute(self):
  82. self.sync_slide_repo()
  83. self.clear_obs_slides_dir()
  84. self.slide_selection_iterator()
  85. def main():
  86. ssync = Ssync()
  87. ssync.execute()
  88. if __name__ == "__main__":
  89. main()