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.

105 lines
3.8 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. _currentVideo = None
  9. _videoSuffix = ".mp4"
  10. _urlSuffix = "-url"
  11. _errorSuffix = "-error"
  12. _publishSuffix = "-publish"
  13. _lastUpdateTimeKey = "update-time"
  14. def __init__(self, autouploadFile):
  15. super(AutoUpload, self).__init__()
  16. # print("content of file")
  17. # with open(autouploadFile, "r") as file:
  18. # print(file.readlines())
  19. self._autouploadFile = autouploadFile
  20. self._basePath = pathlib.Path(autouploadFile).resolve().parent
  21. self._autoUploadConfig = toml.load(autouploadFile)
  22. _autouploadFiles.append((autouploadFile, self))
  23. # This should take care of circular references
  24. # TODO: make a test for this
  25. for autouploadFile in self._autoUploadConfig.get("autoupload", []):
  26. alreadyPresentAutoupload = False
  27. for file in _autouploadFiles:
  28. if file[0] == autouploadFile:
  29. alreadyPresentAutoupload = True
  30. if not alreadyPresentAutoupload:
  31. AutoUpload(autouploadFile)
  32. # TODO: remove me / should only be here for debug
  33. # print("setting autouplad config")
  34. # print(self._autouploadFile)
  35. # print(self._autoUploadConfig)
  36. def nextVideo(self, platform, recursive=True):
  37. """Get the path to the next video to upload for a specific platform"""
  38. # Get next video from current autoupload file
  39. for nextVideo in self._autoUploadConfig["videos"]:
  40. if platform + self._urlSuffix not in self._autoUploadConfig["videos"][nextVideo]:
  41. self._currentVideo = nextVideo
  42. return (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix()
  43. # Get next video from another autoupload file
  44. if recursive:
  45. for file in _autouploadFiles:
  46. if file[0] != self._autouploadFile:
  47. print("file:", file, "autofile:", self._autouploadFile)
  48. nextVideo = file[1].nextVideo(platform, False)
  49. print(file, nextVideo)
  50. if nextVideo:
  51. self._currentVideo = nextVideo
  52. return (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix()
  53. # No video to upload for this platforms
  54. self._currentVideo = None
  55. return False
  56. def success(self, platform, url, publishDate):
  57. """Last video asked was successfully uploaded"""
  58. updateTime = datetime.today()
  59. self._autoUploadConfig["videos"][self._currentVideo].pop(platform + self._errorSuffix, None)
  60. self._autoUploadConfig["videos"][self._currentVideo][platform + self._urlSuffix] = url
  61. self._autoUploadConfig["videos"][self._currentVideo][platform + self._publishSuffix] = publishDate
  62. self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
  63. self._write()
  64. def error(self, platform, errorString):
  65. """There was an error on upload of last video"""
  66. updateTime = datetime.today()
  67. self._autoUploadConfig["videos"][self._currentVideo][platform + self._errorSuffix] = errorString
  68. self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
  69. self._write()
  70. def _write(self):
  71. """Private helper function
  72. Write current autoupload state on file(s)"""
  73. with open(self._autouploadFile, 'w', encoding="utf-8", errors="strict") as f:
  74. toml.dump(self._autoUploadConfig, f, encoder=toml.TomlPreserveInlineDictEncoder()) # can we also inherit from toml.TomlPreserveCommentEncoder()?