|
|
@ -4,6 +4,8 @@ import toml |
|
|
|
import pathlib |
|
|
|
from datetime import datetime |
|
|
|
|
|
|
|
_autouploadFiles = [] |
|
|
|
|
|
|
|
class AutoUpload(object): |
|
|
|
"""AutoUpload is a class handling the state of videos automatically uploaded by prismedia""" |
|
|
|
|
|
|
@ -18,31 +20,60 @@ class AutoUpload(object): |
|
|
|
def __init__(self, autouploadFile): |
|
|
|
super(AutoUpload, self).__init__() |
|
|
|
|
|
|
|
# print("content of file") |
|
|
|
# with open(autouploadFile, "r") as file: |
|
|
|
# print(file.readlines()) |
|
|
|
|
|
|
|
self._autouploadFile = autouploadFile |
|
|
|
self._basePath = pathlib.Path(autouploadFile).resolve().parent |
|
|
|
self._autoUploadConfig = toml.load(autouploadFile) |
|
|
|
|
|
|
|
# print("content of file") |
|
|
|
# with open(autouploadFile, "r") as file: |
|
|
|
# print(file.readlines()) |
|
|
|
_autouploadFiles.append((autouploadFile, self)) |
|
|
|
|
|
|
|
# This should take care of circular references |
|
|
|
# TODO: make a test for this |
|
|
|
for autouploadFile in self._autoUploadConfig.get("autoupload", []): |
|
|
|
alreadyPresentAutoupload = False |
|
|
|
|
|
|
|
for file in _autouploadFiles: |
|
|
|
if file[0] == autouploadFile: |
|
|
|
alreadyPresentAutoupload = True |
|
|
|
|
|
|
|
if not alreadyPresentAutoupload: |
|
|
|
AutoUpload(autouploadFile) |
|
|
|
|
|
|
|
# TODO: remove me / should only be here for debug |
|
|
|
# print("setting autouplad config") |
|
|
|
# print(self._autouploadFile) |
|
|
|
# print(self._autoUploadConfig) |
|
|
|
|
|
|
|
def nextVideo(self, platform): |
|
|
|
def nextVideo(self, platform, recursive=True): |
|
|
|
"""Get the path to the next video to upload for a specific platform""" |
|
|
|
for video in self._autoUploadConfig["videos"]: |
|
|
|
if platform + self._urlSuffix not in self._autoUploadConfig["videos"][video]: |
|
|
|
self._currentVideo = video |
|
|
|
return (self._basePath / video).with_suffix(self._videoSuffix).resolve().as_posix() |
|
|
|
|
|
|
|
# Get next video from current autoupload file |
|
|
|
for nextVideo in self._autoUploadConfig["videos"]: |
|
|
|
if platform + self._urlSuffix not in self._autoUploadConfig["videos"][nextVideo]: |
|
|
|
self._currentVideo = nextVideo |
|
|
|
return (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix() |
|
|
|
|
|
|
|
# Get next video from another autoupload file |
|
|
|
if recursive: |
|
|
|
for file in _autouploadFiles: |
|
|
|
if file[0] != self._autouploadFile: |
|
|
|
print("file:", file, "autofile:", self._autouploadFile) |
|
|
|
nextVideo = file[1].nextVideo(platform, False) |
|
|
|
print(file, nextVideo) |
|
|
|
if nextVideo: |
|
|
|
self._currentVideo = nextVideo |
|
|
|
return (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix() |
|
|
|
|
|
|
|
# No video to upload for this platforms |
|
|
|
self._currentVideo = None |
|
|
|
return False |
|
|
|
|
|
|
|
def success(self, platform, url, publishDate): |
|
|
|
"""Last video asked was successfully uploaded""" |
|
|
|
|
|
|
|
updateTime = datetime.today() |
|
|
|
|
|
|
|
self._autoUploadConfig["videos"][self._currentVideo].pop(platform + self._errorSuffix, None) |
|
|
@ -57,6 +88,7 @@ class AutoUpload(object): |
|
|
|
|
|
|
|
def error(self, platform, errorString): |
|
|
|
"""There was an error on upload of last video""" |
|
|
|
|
|
|
|
updateTime = datetime.today() |
|
|
|
|
|
|
|
self._autoUploadConfig["videos"][self._currentVideo][platform + self._errorSuffix] = errorString |
|
|
@ -68,5 +100,6 @@ class AutoUpload(object): |
|
|
|
def _write(self): |
|
|
|
"""Private helper function |
|
|
|
Write current autoupload state on file(s)""" |
|
|
|
|
|
|
|
with open(self._autouploadFile, 'w', encoding="utf-8", errors="strict") as f: |
|
|
|
toml.dump(self._autoUploadConfig, f, encoder=toml.TomlPreserveInlineDictEncoder()) # can we also inherit from toml.TomlPreserveCommentEncoder()? |