From f16e11fa58058a501feef525f148e0e6746eed98 Mon Sep 17 00:00:00 2001 From: Zykino Date: Tue, 1 Sep 2020 12:15:56 +0200 Subject: [PATCH] Handle other autouploadFiles --- autoupload.py | 49 ++++++++++++++++++++++++++++++++++++++-------- test_autoupload.py | 30 ++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/autoupload.py b/autoupload.py index 6cfffcb..e36ec99 100644 --- a/autoupload.py +++ b/autoupload.py @@ -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()? diff --git a/test_autoupload.py b/test_autoupload.py index 5aa403c..5671526 100644 --- a/test_autoupload.py +++ b/test_autoupload.py @@ -11,6 +11,10 @@ class TestVideoMethods(unittest.TestCase): platform1 = "platform" platform2 = "otherplatform" + def tearDown(self): + # Reset the kinda static variable between runs. + autoupload._autouploadFiles = [] + def test_nextVideo(self): with TestFileContent( """[videos] @@ -59,7 +63,7 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - auto.nextVideo(self.platform1) + self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode1.mp4") auto.error(self.platform1, "https://platform/url") self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode1.mp4") self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") @@ -77,7 +81,7 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - auto.nextVideo(self.platform1) + self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode1.mp4") auto.error(self.platform1, "https://platform/url") self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode1.mp4") self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") @@ -98,6 +102,28 @@ Episode1 = {} with open(videoFile.filename, "r") as file: print(file.readlines()) + def test_subAutouploadFile(self): + with TestFileContent( +"""[videos] +Episode2 = {} # TODO: We should chek that the path of this file is from the base path of this autoupload config file +""" + ) as autouploadFile: + with TestFileContent( +f"""autoupload = ["{autouploadFile.filename}"] # list of strings, one for each sub autoupload file + +[videos] +Episode1 = {{}} # Double curly braces to escape them for f-string +""" + ) as videoFile: + auto = autoupload.AutoUpload(videoFile.filename) + + self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode1.mp4") + self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") + auto.success(self.platform1, "https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) + self.assertEqual(auto.nextVideo(self.platform1), "/tmp/Episode2.mp4") + self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") + # TODO: read file and check formatting and content? + # https://stackoverflow.com/a/54053967 class TestFileContent: def __init__(self, content):