From 322fd13b41a76381830fd901641ffc20a3a7c93d Mon Sep 17 00:00:00 2001 From: Zykino Date: Thu, 3 Sep 2020 20:52:49 +0200 Subject: [PATCH] Rework to update the correctautoupload file --- autoupload.py | 105 +++++++++++++++++++++++++++------------------ test_autoupload.py | 88 ++++++++++++++++++++++++------------- 2 files changed, 120 insertions(+), 73 deletions(-) diff --git a/autoupload.py b/autoupload.py index e36ec99..8798660 100644 --- a/autoupload.py +++ b/autoupload.py @@ -9,13 +9,12 @@ _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" + urlSuffix = "-url" + errorSuffix = "-error" + publishSuffix = "-publish" + lastUpdateTimeKey = "update-time" def __init__(self, autouploadFile): super(AutoUpload, self).__init__() @@ -24,11 +23,44 @@ class AutoUpload(object): # with open(autouploadFile, "r") as file: # print(file.readlines()) - self._autouploadFile = autouploadFile + self._baseAutouploadFile = autouploadFile self._basePath = pathlib.Path(autouploadFile).resolve().parent - self._autoUploadConfig = toml.load(autouploadFile) + self._load() + + 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 + AutoUpload.urlSuffix not in self._autoUploadConfig["videos"][nextVideo]: + videoPath = (self._basePath / nextVideo).with_suffix(self._videoSuffix).resolve().as_posix() + return Video(self, platform, nextVideo, videoPath, (self._baseAutouploadFile, self._autoUploadConfig)) + + # Get next video from another autoupload file + if recursive: + for file in _autouploadFiles: + if file[0] != self._baseAutouploadFile: + nextVideo = file[1].nextVideo(platform, False) + if nextVideo: + videoPath = (self._basePath / nextVideo.videoName).with_suffix(self._videoSuffix).resolve().as_posix() + return Video(self, platform, nextVideo.videoName, nextVideo.videoPath, (file[0], file[1]._autoUploadConfig)) + + # No video to upload for this platforms + return None + + def reload(self): + """Reload the configuration""" + + _autouploadFiles.clear() + self._load() - _autouploadFiles.append((autouploadFile, self)) + def _load(self): + """Private helper function + Load the configuration file.""" + # We can reuse this function to reload the configuration after saving the files, or not + + self._autoUploadConfig = toml.load(self._baseAutouploadFile) + _autouploadFiles.append((self._baseAutouploadFile, self)) # This should take care of circular references # TODO: make a test for this @@ -44,56 +76,43 @@ class AutoUpload(object): # TODO: remove me / should only be here for debug # print("setting autouplad config") - # print(self._autouploadFile) + # print(self._baseAutouploadFile) # 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() +class Video(object): + """Returned by AutoUpload when looking for the next Video to upload.""" - # 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() + def __init__(self, parent, platform, videoName, videoPath, autouploadFile): + super(Video, self).__init__() + self._autouploadFile = autouploadFile - # No video to upload for this platforms - self._currentVideo = None - return False + self.parent = parent + self.platform = platform + self.videoName = videoName + self.videoPath = videoPath - def success(self, platform, url, publishDate): + def success(self, url, publishDate): """Last video asked was successfully uploaded""" updateTime = datetime.today() - self._autoUploadConfig["videos"][self._currentVideo].pop(platform + self._errorSuffix, None) + # Remove last error if there were any + self._autouploadFile[1]["videos"][self.videoName].pop(self.platform + AutoUpload.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._autouploadFile[1]["videos"][self.videoName][AutoUpload.lastUpdateTimeKey] = updateTime + self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.urlSuffix] = url + self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.publishSuffix] = publishDate self._write() - def error(self, platform, errorString): + def error(self, 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._autouploadFile[1]["videos"][self.videoName][AutoUpload.lastUpdateTimeKey] = updateTime + self._autouploadFile[1]["videos"][self.videoName][self.platform + AutoUpload.errorSuffix] = errorString self._write() @@ -101,5 +120,7 @@ class AutoUpload(object): """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()? + with open(self._autouploadFile[0], "w", encoding="utf-8", errors="strict") as f: + toml.dump(self._autouploadFile[1], f, encoder=toml.TomlPreserveInlineDictEncoder()) # can we also inherit from toml.TomlPreserveCommentEncoder()? + + self.parent.reload() diff --git a/test_autoupload.py b/test_autoupload.py index 5671526..bb13ecc 100644 --- a/test_autoupload.py +++ b/test_autoupload.py @@ -23,8 +23,10 @@ Episode1 = {} ) 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") + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") + nextVideo = auto.nextVideo(self.platform2) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") def test_nextVideoSubfolder(self): with TestFileContent( @@ -34,8 +36,10 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - self.assertEqual(auto.nextVideo(self.platform1), "/tmp/subfolder/Episode1.mp4") - self.assertEqual(auto.nextVideo(self.platform2), "/tmp/subfolder/Episode1.mp4") + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo.videoPath, "/tmp/subfolder/Episode1.mp4") + nextVideo = auto.nextVideo(self.platform2) + self.assertEqual(nextVideo.videoPath, "/tmp/subfolder/Episode1.mp4") def test_success(self): with TestFileContent( @@ -45,10 +49,13 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - auto.nextVideo(self.platform1) - auto.success(self.platform1, "https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) - self.assertEqual(auto.nextVideo(self.platform1), False) - self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") + nextVideo = auto.nextVideo(self.platform1) + nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) + + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo, None) + nextVideo = auto.nextVideo(self.platform2) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") # TODO: read file and check formatting and content? # print("content of file") @@ -63,10 +70,11 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - 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") + nextVideo = auto.nextVideo(self.platform1) + nextVideo.error("Server not available") + + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") # TODO: read file and check formatting and content? # print("content of file") @@ -81,28 +89,31 @@ Episode1 = {} ) as videoFile: auto = autoupload.AutoUpload(videoFile.filename) - 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") - # TODO: read file and check formatting and content? + nextVideo = auto.nextVideo(self.platform1) + nextVideo.error("Server not available") - print("content of file") - with open(videoFile.filename, "r") as file: - print(file.readlines()) + nextVideo = auto.nextVideo(self.platform1) + # TODO: read file and check formatting and content? + # print("content of file") + # with open(videoFile.filename, "r") as file: + # print(file.readlines()) - auto.success(self.platform1, "https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) - self.assertEqual(auto.nextVideo(self.platform1), False) - self.assertEqual(auto.nextVideo(self.platform2), "/tmp/Episode1.mp4") + nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo, None) + nextVideo = auto.nextVideo(self.platform2) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") # TODO: read file and check formatting and content? # TODO: check the key "platform-error" is not present after a success - print("content of file") - with open(videoFile.filename, "r") as file: - print(file.readlines()) + # print("content of file") + # with open(videoFile.filename, "r") as file: + # print(file.readlines()) def test_subAutouploadFile(self): + # We may split this test to first test that we can have reference another file + # and then that we pass transparently between files. with TestFileContent( """[videos] Episode2 = {} # TODO: We should chek that the path of this file is from the base path of this autoupload config file @@ -117,12 +128,27 @@ 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") + nextVideo = auto.nextVideo(self.platform1) + nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) + + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode2.mp4") # TODO: read file and check formatting and content? + nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31)) + + nextVideo = auto.nextVideo(self.platform1) + self.assertEqual(nextVideo, None) + nextVideo = auto.nextVideo(self.platform2) + self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4") + # TODO: read file and check formatting and content? + + # print("content of autoupload file") + # with open(autouploadFile.filename, "r") as file: + # print(file.readlines()) + # + # print("content of video file") + # with open(videoFile.filename, "r") as file: + # print(file.readlines()) # https://stackoverflow.com/a/54053967 class TestFileContent: