#!/usr/bin/env python3
|
|
|
|
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"""
|
|
|
|
_currentVideo = None
|
|
_videoSuffix = ".mp4"
|
|
|
|
_urlSuffix = "-url"
|
|
_errorSuffix = "-error"
|
|
_publishSuffix = "-publish"
|
|
_lastUpdateTimeKey = "update-time"
|
|
|
|
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)
|
|
|
|
_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, recursive=True):
|
|
"""Get the path to the next video to upload for a specific platform"""
|
|
|
|
# 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)
|
|
|
|
|
|
self._autoUploadConfig["videos"][self._currentVideo][platform + self._urlSuffix] = url
|
|
self._autoUploadConfig["videos"][self._currentVideo][platform + self._publishSuffix] = publishDate
|
|
|
|
self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
|
|
|
|
self._write()
|
|
|
|
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
|
|
|
|
self._autoUploadConfig["videos"][self._currentVideo][self._lastUpdateTimeKey] = updateTime
|
|
|
|
self._write()
|
|
|
|
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()?
|