You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.7 KiB

  1. #!/usr/bin/env python3
  2. import toml
  3. import pathlib
  4. from datetime import datetime
  5. _autouploadFiles = []
  6. class AutoUpload(object):
  7. """AutoUpload is a class handling the state of videos automatically uploaded by prismedia"""
  8. _videoSuffix = ".mp4"
  9. urlSuffix = "-url"
  10. errorSuffix = "-error"
  11. publishSuffix = "-publish"
  12. lastUpdateTimeKey = "update-time"
  13. def __init__(self, autouploadFile):
  14. super(AutoUpload, self).__init__()
  15. # print("content of file")
  16. # with open(autouploadFile, "r") as file:
  17. # print(file.readlines())
  18. self._baseAutouploadFile = autouploadFile
  19. self._basePath = pathlib.Path(autouploadFile).resolve().parent
  20. self._load()
  21. def nextVideo(self, platform, recursive=True):
  22. """Get the path to the next video to upload for a specific platform"""
  23. # Get next video from current autoupload file
  24. for nextVideo in self._autoUploadConfig["videos"]:
  25. if platform + AutoUpload.urlSuffix not in self._autoUploadConfig["videos"][nextVideo]:
  26. videoPath = (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix()
  27. return Video(self, platform, nextVideo, videoPath, (self._baseAutouploadFile, self._autoUploadConfig))
  28. # Get next video from another autoupload file
  29. if recursive:
  30. for file in _autouploadFiles:
  31. if file[0] != self._baseAutouploadFile:
  32. nextVideo = file[1].nextVideo(platform, False)
  33. if nextVideo:
  34. videoPath = (self._basePath / nextVideo.videoName).with_suffix(self._videoSuffix).resolve().as_posix()
  35. return Video(self, platform, nextVideo.videoName, nextVideo.videoPath, (file[0], file[1]._autoUploadConfig))
  36. # No video to upload for this platforms
  37. return None
  38. def reload(self):
  39. """Reload the configuration"""
  40. _autouploadFiles.clear()
  41. self._load()
  42. def _load(self):
  43. """Private helper function
  44. Load the configuration file."""
  45. # We can reuse this function to reload the configuration after saving the files, or not
  46. self._autoUploadConfig = toml.load(self._baseAutouploadFile)
  47. _autouploadFiles.append((self._baseAutouploadFile, self))
  48. # This should take care of circular references
  49. # TODO: make a test for this
  50. for autouploadFile in self._autoUploadConfig.get("autoupload", []):
  51. alreadyPresentAutoupload = False
  52. for file in _autouploadFiles:
  53. if file[0] == autouploadFile:
  54. alreadyPresentAutoupload = True
  55. if not alreadyPresentAutoupload:
  56. AutoUpload(autouploadFile)
  57. # TODO: remove me / should only be here for debug
  58. # print("setting autouplad config")
  59. # print(self._baseAutouploadFile)
  60. # print(self._autoUploadConfig)
  61. class Video(object):
  62. """Returned by AutoUpload when looking for the next Video to upload."""
  63. def __init__(self, parent, platform, videoName, videoPath, autouploadFile):
  64. super(Video, self).__init__()
  65. self._autouploadFile = autouploadFile
  66. self.parent = parent
  67. self.platform = platform
  68. self.videoName = videoName
  69. self.videoPath = videoPath
  70. def success(self, url, publishDate):
  71. """Last video asked was successfully uploaded"""
  72. updateTime = datetime.today()
  73. # Remove last error if there were any
  74. self._autouploadFile[1]["videos"][self.videoName].pop(self.platform + AutoUpload.errorSuffix, None)
  75. self._autouploadFile[1]["videos"][self.videoName][AutoUpload.lastUpdateTimeKey] = updateTime
  76. self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.urlSuffix] = url
  77. self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.publishSuffix] = publishDate
  78. self._write()
  79. def error(self, errorString):
  80. """There was an error on upload of last video"""
  81. updateTime = datetime.today()
  82. self._autouploadFile[1]["videos"][self.videoName][AutoUpload.lastUpdateTimeKey] = updateTime
  83. self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.errorSuffix] = errorString
  84. self._write()
  85. def _write(self):
  86. """Private helper function
  87. Write current autoupload state on file(s)"""
  88. with open(self._autouploadFile[0], "w", encoding="utf-8", errors="strict") as f:
  89. toml.dump(self._autouploadFile[1], f, encoder=toml.TomlPreserveInlineDictEncoder()) # can we also inherit from toml.TomlPreserveCommentEncoder()?
  90. self.parent.reload()