From 7e4f9d995c87be92966bc57c9565c2c50fd47c52 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 10 Dec 2018 07:28:04 +0100 Subject: [PATCH 01/12] correct some typo in output, fix #23 --- lib/yt_upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yt_upload.py b/lib/yt_upload.py index daebdc9..0dfbc57 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -254,7 +254,7 @@ def set_playlist(youtube, playlist_id, video_id): logging.error("Youtube: Error: " + str(e.message)) else: logging.error("Youtube: Error: " + str(e)) - logging.info('Youtube: Video is correclty added to the playlist.') + logging.info('Youtube: Video is correctly added to the playlist.') # This method implements an exponential backoff strategy to resume a From dffd3ffa842bc318bd3923c61e768f26ac4595eb Mon Sep 17 00:00:00 2001 From: Zykino Date: Sat, 10 Nov 2018 18:56:26 +0100 Subject: [PATCH 02/12] decode stdin strins arguments --- lib/utils.py | 14 ++++++++++---- prismedia_upload.py | 9 ++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/utils.py b/lib/utils.py index 772ff6b..11e09d5 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -123,7 +123,6 @@ def searchThumbnail(options): options['--thumbnail'] = video_directory + video_file + ".jpeg" return options - # return the nfo as a RawConfigParser object def loadNFO(options): video_directory = dirname(options.get('--file')) + "/" @@ -168,7 +167,6 @@ def loadNFO(options): logging.info("No suitable NFO found, skipping.") return False - def parseNFO(options): nfo = loadNFO(options) if nfo: @@ -189,11 +187,9 @@ def parseNFO(options): exit(1) return options - def upcaseFirstLetter(s): return s[0].upper() + s[1:] - def cleanString(toclean): toclean = toclean.split(' ') cleaned = '' @@ -208,3 +204,13 @@ def cleanString(toclean): cleaned = cleaned + strtoclean return cleaned + +def decodeArgumentStrings(options, encoding): + if options["--name"] is not None: + options["--name"] = options["--name"].decode(encoding) + + if options["--description"] is not None: + options["--description"] = options["--description"].decode(encoding) + + if options["--tags"] is not None: + options["--tags"] = options["--tags"].decode(encoding) diff --git a/prismedia_upload.py b/prismedia_upload.py index a300c96..6e5ee18 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -64,12 +64,12 @@ Languages: from os.path import dirname, realpath import sys import datetime +import locale import logging logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) from docopt import docopt - # Allows a relative import from the parent folder sys.path.insert(0, dirname(realpath(__file__)) + "/lib") @@ -110,7 +110,6 @@ VALID_LANGUAGES = ('arabic', 'english', 'french', 'japanese', 'korean', 'mandarin', 'portuguese', 'punjabi', 'russian', 'spanish') - def validateVideo(path): supported_types = ['video/mp4'] if magic.from_file(path, mime=True) in supported_types: @@ -118,21 +117,18 @@ def validateVideo(path): else: return False - def validateCategory(category): if category.lower() in VALID_CATEGORIES: return True else: return False - def validatePrivacy(privacy): if privacy.lower() in VALID_PRIVACY_STATUSES: return True else: return False - def validatePlatform(platform): for plfrm in platform.split(','): if plfrm.lower().replace(" ", "") not in VALID_PLATFORM: @@ -140,14 +136,12 @@ def validatePlatform(platform): return True - def validateLanguage(language): if language.lower() in VALID_LANGUAGES: return True else: return False - def validatePublish(publish): # Check date format and if date is future try: @@ -222,6 +216,7 @@ if __name__ == '__main__': '--version': bool }) + utils.decodeArgumentStrings(options, locale.getpreferredencoding()) options = utils.parseNFO(options) if not options.get('--thumbnail'): From dbcd2ff0109680a7ded20d337497cbf67a89679d Mon Sep 17 00:00:00 2001 From: Zykino Date: Sat, 10 Nov 2018 19:13:36 +0100 Subject: [PATCH 03/12] update the README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cd712e9..9bad074 100644 --- a/README.md +++ b/README.md @@ -149,8 +149,8 @@ Languages: - [x] set default language - [x] thumbnail/preview - [x] multiple lines description (see [issue 4](https://git.lecygnenoir.info/LecygneNoir/prismedia/issues/4)) - - [x] add videos to playlist for Peertube - - [x] add videos to playlist for Youtube + - [x] add videos to playlist + - [x] create playlist - [x] Use a config file (NFO) file to retrieve videos arguments - [x] Allow to choose peertube or youtube upload (to resume failed upload for example) - [x] Add publishAt option to plan your videos @@ -163,4 +163,4 @@ Languages: If your server uses peertube before 1.0.0-beta4, use the version inside tag 1.0.0-beta3! ## Sources -inspired by [peeror](https://git.rigelk.eu/rigelk/peeror) and [youtube-upload](https://github.com/tokland/youtube-upload) \ No newline at end of file +inspired by [peeror](https://git.rigelk.eu/rigelk/peeror) and [youtube-upload](https://github.com/tokland/youtube-upload) From 617e989154352d685bc54cf37ba45ae0c30f0852 Mon Sep 17 00:00:00 2001 From: Zykino Date: Sun, 11 Nov 2018 13:00:47 +0100 Subject: [PATCH 04/12] fix error message --- prismedia_upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prismedia_upload.py b/prismedia_upload.py index 6e5ee18..22accf9 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -174,7 +174,7 @@ if __name__ == '__main__': Optional('--description'): Or(None, And( str, lambda x: not x.isdigit(), - error="The video name should be a string") + error="The video description should be a string") ), Optional('--tags'): Or(None, And( str, From 6c68c3363bbc36fed94e302a4725f20009c109bc Mon Sep 17 00:00:00 2001 From: Zykino Date: Sun, 11 Nov 2018 14:31:51 +0100 Subject: [PATCH 05/12] prevent decoding unicode strings since python prefer to crash than doing nothing --- lib/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/utils.py b/lib/utils.py index 11e09d5..9ccfdb5 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -206,6 +206,10 @@ def cleanString(toclean): return cleaned def decodeArgumentStrings(options, encoding): + # Python crash when decding from UTF-8 to UTF-8, so we prevent this + if "utf-8" == encoding.lower(): + return; + if options["--name"] is not None: options["--name"] = options["--name"].decode(encoding) From 8d8898aa5578df8ea702e8977886343ed5d12830 Mon Sep 17 00:00:00 2001 From: Zykino Date: Sun, 11 Nov 2018 16:52:48 +0100 Subject: [PATCH 06/12] The strings arguments should be in unicode --- prismedia_upload.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prismedia_upload.py b/prismedia_upload.py index 22accf9..d2bfb8b 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -167,17 +167,17 @@ if __name__ == '__main__': schema = Schema({ '--file': And(str, validateVideo, error='file is not supported, please use mp4'), Optional('--name'): Or(None, And( - str, + unicode, lambda x: not x.isdigit(), error="The video name should be a string") ), Optional('--description'): Or(None, And( - str, + unicode, lambda x: not x.isdigit(), error="The video description should be a string") ), Optional('--tags'): Or(None, And( - str, + unicode, lambda x: not x.isdigit(), error="Tags should be a string") ), From f66ba6cc21e1c83fc2b32ec94294d1f7bd079c65 Mon Sep 17 00:00:00 2001 From: Zykino Date: Wed, 28 Nov 2018 00:50:38 +0100 Subject: [PATCH 07/12] string from URF-8 encoded files have not the "unicode" type --- README.md | 3 ++- prismedia_upload.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9bad074..a813848 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,8 @@ Options: --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled) --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe) --nfo=STRING Configure a specific nfo file to set options for the video. - By default Prismedia search a .txt based on video name + By default Prismedia search a .txt based on the video name and will + decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded) See nfo_example.txt for more details --platform=STRING List of platform(s) to upload to, comma separated. Supported platforms are youtube and peertube (default is both) diff --git a/prismedia_upload.py b/prismedia_upload.py index d2bfb8b..d97ca1b 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -167,17 +167,17 @@ if __name__ == '__main__': schema = Schema({ '--file': And(str, validateVideo, error='file is not supported, please use mp4'), Optional('--name'): Or(None, And( - unicode, + basestring, lambda x: not x.isdigit(), error="The video name should be a string") ), Optional('--description'): Or(None, And( - unicode, + basestring, lambda x: not x.isdigit(), error="The video description should be a string") ), Optional('--tags'): Or(None, And( - unicode, + basestring, lambda x: not x.isdigit(), error="Tags should be a string") ), From bddf2ee414e1c4d542bf3608386deda1a9bc24a6 Mon Sep 17 00:00:00 2001 From: Zykino Date: Tue, 11 Dec 2018 01:17:23 +0100 Subject: [PATCH 08/12] fix typo --- lib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.py b/lib/utils.py index 9ccfdb5..e7e3635 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -206,7 +206,7 @@ def cleanString(toclean): return cleaned def decodeArgumentStrings(options, encoding): - # Python crash when decding from UTF-8 to UTF-8, so we prevent this + # Python crash when decoding from UTF-8 to UTF-8, so we prevent this if "utf-8" == encoding.lower(): return; From 3b382900409f5e517e08473ce12c43a6a3b9b8b0 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sun, 10 Mar 2019 10:50:35 +0100 Subject: [PATCH 09/12] Update CHANGELOG according to the text used in gitea for v0.6.1-1 --- CHANGELOG.md | 4 +++- README.md | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a507abd..251aa61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # Changelog ## v0.6.1-1 Hotfix -This fix prepares the python3 compatibility +This fix prepares the python3 compatibility. +**Warning** you need a new prerequisites: python-unidecode - Remove mastodon tags (mt) options as it's deprecated. Compatibility between Peertube and Mastodon is complete. + - Simplify python2 specific functions ## v0.6.1 diff --git a/README.md b/README.md index a813848..923a411 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Search in your package manager, otherwise use ``pip install --upgrade`` - python-magic-bin - requests-toolbelt - tzlocal + - unidecode ## Configuration From 9b3d793975096a6677145eddc9a1c6d5d6061fe6 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Tue, 18 Jun 2019 12:58:02 +0200 Subject: [PATCH 10/12] Peertube: modify upload to ever use default channel and create true playlist instead. Playlist created as private for the moment --- lib/pt_upload.py | 62 ++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index ad0a36a..3ef9f6c 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -51,12 +51,16 @@ def get_authenticated_service(secret): return oauth +def get_default_channel(user_info): + return user_info['videoChannels'][0]['id'] + + def get_default_playlist(user_info): return user_info['videoChannels'][0]['id'] -def get_playlist_by_name(user_info, options): - for playlist in user_info["videoChannels"]: +def get_playlist_by_name(user_playlists, options): + for playlist in user_playlists["data"]: if playlist['displayName'].encode('utf8') == str(options.get('--playlist')): return playlist['id'] @@ -64,20 +68,16 @@ def get_playlist_by_name(user_info, options): def create_playlist(oauth, url, options): template = ('Peertube: Playlist %s does not exist, creating it.') logging.info(template % (str(options.get('--playlist')))) - playlist_name = utils.cleanString(str(options.get('--playlist'))) - # Peertube allows 20 chars max for playlist name - playlist_name = playlist_name[:19] - data = '{"name":"' + playlist_name +'", \ - "displayName":"' + str(options.get('--playlist')) +'", \ - "description":null}' - - headers = { - 'Content-Type': "application/json" - } + # We use files for form-data Content + # see https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file + files = {'displayName': (None, str(options.get('--playlist'))), + 'privacy': (None, "3"), + 'description': (None, "null"), + 'videoChannelId': (None, "null"), + 'thumbnailfile': (None, "null")} try: - response = oauth.post(url + "/api/v1/video-channels/", - data=data, - headers=headers) + response = oauth.post(url + "/api/v1/video-playlists/", + files=files) except Exception as e: if hasattr(e, 'message'): logging.error("Error: " + str(e.message)) @@ -86,18 +86,15 @@ def create_playlist(oauth, url, options): if response is not None: if response.status_code == 200: jresponse = response.json() - jresponse = jresponse['videoChannel'] + jresponse = jresponse['videoPlaylist'] return jresponse['id'] - if response.status_code == 409: - logging.error('Peertube: Error: It seems there is a conflict with an existing playlist, please beware ' - 'Peertube internal name is compiled from 20 firsts characters of playlist name.' - ' Please check your playlist name an retry.') - exit(1) else: logging.error(('Peertube: The upload failed with an unexpected response: ' '%s') % response) exit(1) +def set_playlist(oauth, url, video_id, playlist_id): + logging.info('Peertube: add video to playlist.') def upload_video(oauth, secret, options): @@ -109,9 +106,13 @@ def upload_video(oauth, secret, options): return (basename(path), open(abspath(path), 'rb'), mimetypes.types_map[splitext(path)[1]]) + def get_playlist(username): + return json.loads(oauth.get(url+"/api/v1/accounts/"+username+"/video-playlists").content) + path = options.get('--file') url = str(secret.get('peertube', 'peertube_url')).rstrip('/') user_info = get_userinfo() + user_playlists = get_playlist(str(secret.get('peertube', 'username').lower())) # We need to transform fields into tuple to deal with tags as # MultipartEncoder does not support list refer @@ -175,15 +176,16 @@ def upload_video(oauth, secret, options): fields.append(("previewfile", get_file(options.get('--thumbnail')))) if options.get('--playlist'): - playlist_id = get_playlist_by_name(user_info, options) + playlist_id = get_playlist_by_name(user_playlists, options) if not playlist_id and options.get('--playlistCreate'): playlist_id = create_playlist(oauth, url, options) - elif not playlist_id: - logging.warning("Playlist `" + options.get('--playlist') + "` is unknown, using default playlist.") - playlist_id = get_default_playlist(user_info) - else: - playlist_id = get_default_playlist(user_info) - fields.append(("channelId", str(playlist_id))) + else: + logging.warning("Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate" + " if you want to create it") + exit(1) + + default_channel = get_default_channel(user_info) + fields.append(("channelId", str(default_channel))) multipart_data = MultipartEncoder(fields) @@ -198,10 +200,12 @@ def upload_video(oauth, secret, options): jresponse = response.json() jresponse = jresponse['video'] uuid = jresponse['uuid'] - idvideo = str(jresponse['id']) + video_id = str(jresponse['id']) logging.info('Peertube : Video was successfully uploaded.') template = 'Peertube: Watch it at %s/videos/watch/%s.' logging.info(template % (url, uuid)) + # if options.get('--playlist'): + # set_playlist(oauth, url, video_id, playlist_id) else: logging.error(('Peertube: The upload failed with an unexpected response: ' '%s') % response) From 82fd09c0e71ba8095efda6cee4133988a26e0784 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Tue, 18 Jun 2019 13:17:44 +0200 Subject: [PATCH 11/12] Peertube: Add the function to set playlist for peertube video, and not use channel anymore --- lib/pt_upload.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index 3ef9f6c..4b38b22 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -70,8 +70,9 @@ def create_playlist(oauth, url, options): logging.info(template % (str(options.get('--playlist')))) # We use files for form-data Content # see https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file + # None is used to mute "filename" field files = {'displayName': (None, str(options.get('--playlist'))), - 'privacy': (None, "3"), + 'privacy': (None, "1"), 'description': (None, "null"), 'videoChannelId': (None, "null"), 'thumbnailfile': (None, "null")} @@ -89,12 +90,35 @@ def create_playlist(oauth, url, options): jresponse = jresponse['videoPlaylist'] return jresponse['id'] else: - logging.error(('Peertube: The upload failed with an unexpected response: ' + logging.error(('Peertube: Creating the playlist failed with an unexpected response: ' '%s') % response) exit(1) + def set_playlist(oauth, url, video_id, playlist_id): logging.info('Peertube: add video to playlist.') + data = '{"videoId":"' + str(video_id) + '"}' + + headers = { + 'Content-Type': "application/json" + } + try: + response = oauth.post(url + "/api/v1/video-playlists/"+str(playlist_id)+"/videos", + data=data, + headers=headers) + except Exception as e: + if hasattr(e, 'message'): + logging.error("Error: " + str(e.message)) + else: + logging.error("Error: " + str(e)) + if response is not None: + if response.status_code == 200: + logging.info('Peertube: Video is successfully added to the playlist.') + else: + logging.error(('Peertube: Configuring the playlist failed with an unexpected response: ' + '%s') % response) + exit(1) + def upload_video(oauth, secret, options): @@ -179,7 +203,7 @@ def upload_video(oauth, secret, options): playlist_id = get_playlist_by_name(user_playlists, options) if not playlist_id and options.get('--playlistCreate'): playlist_id = create_playlist(oauth, url, options) - else: + elif not playlist_id: logging.warning("Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate" " if you want to create it") exit(1) @@ -204,8 +228,9 @@ def upload_video(oauth, secret, options): logging.info('Peertube : Video was successfully uploaded.') template = 'Peertube: Watch it at %s/videos/watch/%s.' logging.info(template % (url, uuid)) - # if options.get('--playlist'): - # set_playlist(oauth, url, video_id, playlist_id) + # Upload is successful we may set playlist + if options.get('--playlist'): + set_playlist(oauth, url, video_id, playlist_id) else: logging.error(('Peertube: The upload failed with an unexpected response: ' '%s') % response) From 4707632f13e1065c70ed44a33f347917161994c3 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Thu, 27 Jun 2019 21:30:51 +0200 Subject: [PATCH 12/12] Add CHANGELOG for version v0.6.2 --- CHANGELOG.md | 8 ++++++++ prismedia_upload.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 251aa61..403319f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## v0.6.2 + +**Warning**: your Peertube instance should be at least in v1.3.0 to use this new functionality. + +### Features +New feature, the Peertube playlists are now supported! +We do not use channel in place of playlist anymore. + ## v0.6.1-1 Hotfix This fix prepares the python3 compatibility. **Warning** you need a new prerequisites: python-unidecode diff --git a/prismedia_upload.py b/prismedia_upload.py index 00f64ae..71ba287 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -92,7 +92,7 @@ except ImportError: 'see https://github.com/ahupp/python-magic\n') exit(1) -VERSION = "prismedia v0.6.1-1" +VERSION = "prismedia v0.6.2" VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') VALID_CATEGORIES = (