#!/usr/bin/env python3 import toml import pathlib from datetime import datetime 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__() 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()) # TODO: remove me / should only be here for debug # print("setting autouplad config") # print(self._autouploadFile) # print(self._autoUploadConfig) def nextVideo(self, platform): """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() 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()?