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.

72 lines
2.6 KiB

  1. #!/usr/bin/env python3
  2. import toml
  3. import pathlib
  4. from datetime import datetime
  5. class AutoUpload(object):
  6. """AutoUpload is a class handling the state of videos automatically uploaded by prismedia"""
  7. _currentVideo = None
  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. self._autouploadFile = autouploadFile
  16. self._basePath = pathlib.Path(autouploadFile).resolve().parent
  17. self._autoUploadConfig = toml.load(autouploadFile)
  18. # print("content of file")
  19. # with open(autouploadFile, "r") as file:
  20. # print(file.readlines())
  21. # TODO: remove me / should only be here for debug
  22. # print("setting autouplad config")
  23. # print(self._autouploadFile)
  24. # print(self._autoUploadConfig)
  25. def nextVideo(self, platform):
  26. """Get the path to the next video to upload for a specific platform"""
  27. for video in self._autoUploadConfig["videos"]:
  28. if platform + self._urlSuffix not in self._autoUploadConfig["videos"][video]:
  29. self._currentVideo = video
  30. return (self._basePath / video).with_suffix(self._videoSuffix).resolve().as_posix()
  31. self._currentVideo = None
  32. return False
  33. def success(self, platform, url, publishDate):
  34. """Last video asked was successfully uploaded"""
  35. updateTime = datetime.today()
  36. self._autoUploadConfig["videos"][self._currentVideo].pop(platform + self._errorSuffix, None)
  37. self._autoUploadConfig["videos"][self._currentVideo][platform + self._urlSuffix] = url
  38. self._autoUploadConfig["videos"][self._currentVideo][platform + self._publishSuffix] = publishDate
  39. self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
  40. self._write()
  41. def error(self, platform, errorString):
  42. """There was an error on upload of last video"""
  43. updateTime = datetime.today()
  44. self._autoUploadConfig["videos"][self._currentVideo][platform + self._errorSuffix] = errorString
  45. self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
  46. self._write()
  47. def _write(self):
  48. """Private helper function
  49. Write current autoupload state on file(s)"""
  50. with open(self._autouploadFile, 'w', encoding="utf-8", errors="strict") as f:
  51. toml.dump(self._autoUploadConfig, f, encoder=toml.TomlPreserveInlineDictEncoder()) # can we also inherit from toml.TomlPreserveCommentEncoder()?