From 6a07bbbf4bb93952647b6043f4ba6afde7209155 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 13:50:39 +0200 Subject: [PATCH 01/18] Update peeror repo with new url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fd7ec79..8d864ba 100644 --- a/README.md +++ b/README.md @@ -160,4 +160,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.drycat.fr/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) \ No newline at end of file From 68959cc1a8887d661618c1e0f78dba55eb438593 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:02 +0200 Subject: [PATCH 02/18] Add support for playlist on Peertube --- lib/pt_upload.py | 52 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index e30d1f4..8195336 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -41,11 +41,46 @@ def get_authenticated_service(secret): return oauth +def get_playlist_by_name(user_info, options): + + for playlist in user_info["videoChannels"]: + if playlist['displayName'] == options.get('--playlist'): + return playlist['id'] + + +def create_playlist(oauth, url, options): + template = ('Peertube : Playlist %s does not exist, creating it.') + logging.info(template % (str(options.get('--playlist')))) + data = '{"displayName":"' + str(options.get('--playlist')) +'", \ + "description":null}' + + headers = { + 'Content-Type': "application/json" + } + try: + response = oauth.post(url + "/api/v1/video-channels/", + 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: + jresponse = response.json() + jresponse = jresponse['videoChannel'] + return jresponse['id'] + else: + logging.error(('Peertube : The upload failed with an unexpected response: ' + '%s') % response) + exit(1) + + def upload_video(oauth, secret, options): def get_userinfo(): - user_info = json.loads(oauth.get(url + "/api/v1/users/me").content) - return str(user_info["id"]) + return json.loads(oauth.get(url+"/api/v1/users/me").content) def get_videofile(path): mimetypes.init() @@ -54,6 +89,7 @@ def upload_video(oauth, secret, options): path = options.get('--file') url = secret.get('peertube', 'peertube_url') + user_info = get_userinfo() # We need to transform fields into tuple to deal with tags as # MultipartEncoder does not support list refer @@ -64,7 +100,6 @@ def upload_video(oauth, secret, options): ("licence", "1"), ("description", options.get('--description') or "default description"), ("nsfw", str(int(options.get('--nsfw')) or "0")), - ("channelId", get_userinfo()), ("videofile", get_videofile(path)) ] @@ -105,12 +140,21 @@ def upload_video(oauth, secret, options): else: fields.append(("commentsEnabled", "1")) + if options.get('--playlist'): + playlist_id = get_playlist_by_name(user_info, options) + if not playlist_id and options.get('--playlistCreate'): + playlist_id = create_playlist(oauth, url, options) + else: + playlist_id = user_info['id'] + else: + playlist_id = user_info['id'] + fields.append(("channelId", str(playlist_id))) + multipart_data = MultipartEncoder(fields) headers = { 'Content-Type': multipart_data.content_type } - response = oauth.post(url + "/api/v1/videos/upload", data=multipart_data, headers=headers) From bd2631699a42fa1300826894430a589a479be86f Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:37 +0200 Subject: [PATCH 03/18] Add option to manage playlist for videos --- prismedia_upload.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/prismedia_upload.py b/prismedia_upload.py index 7261cbf..297da76 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -34,6 +34,10 @@ Options: DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00 DATE should be in the future For Peertube, requires the "atd" and "curl utilities installed on the system + --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube. + If the playlist is not found, spawn an error except if --playlist-create is set. + --playlistCreate Create the playlist if not exists. (default do not create) + Only relevant if --playlist is set. -h --help Show this help. --version Show version. @@ -86,7 +90,7 @@ except ImportError: 'see https://github.com/ahupp/python-magic\n') exit(1) -VERSION = "prismedia v0.4" +VERSION = "prismedia v0.5" VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') VALID_CATEGORIES = ( @@ -199,6 +203,8 @@ if __name__ == '__main__': Optional('--cca'): bool, Optional('--disable-comments'): bool, Optional('--nsfw'): bool, + Optional('--playlist'): Or(None, str), + Optional('--playlistCreate'): bool, '--help': bool, '--version': bool }) From c5aeacb936e1e7d2113b62c9ccfe40ffc9c34860 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 13:50:39 +0200 Subject: [PATCH 04/18] Update peeror repo with new url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2197a95..f612c76 100644 --- a/README.md +++ b/README.md @@ -169,4 +169,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.drycat.fr/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) \ No newline at end of file From bd8aa9c4850cbc5597dc397117ce059371ee087e Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:02 +0200 Subject: [PATCH 05/18] Add support for playlist on Peertube --- lib/pt_upload.py | 57 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index fa3225d..afbb8ff 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -48,18 +48,55 @@ def get_authenticated_service(secret): return oauth +def get_playlist_by_name(user_info, options): + + for playlist in user_info["videoChannels"]: + if playlist['displayName'] == options.get('--playlist'): + return playlist['id'] + + +def create_playlist(oauth, url, options): + template = ('Peertube : Playlist %s does not exist, creating it.') + logging.info(template % (str(options.get('--playlist')))) + data = '{"displayName":"' + str(options.get('--playlist')) +'", \ + "description":null}' + + headers = { + 'Content-Type': "application/json" + } + try: + response = oauth.post(url + "/api/v1/video-channels/", + 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: + jresponse = response.json() + jresponse = jresponse['videoChannel'] + return jresponse['id'] + else: + logging.error(('Peertube : The upload failed with an unexpected response: ' + '%s') % response) + exit(1) + + def upload_video(oauth, secret, options): def get_userinfo(): - user_info = json.loads(oauth.get(url + "/api/v1/users/me").content) - return str(user_info["id"]) + return json.loads(oauth.get(url+"/api/v1/users/me").content) def get_file(path): mimetypes.init() return (basename(path), open(abspath(path), 'rb'), mimetypes.types_map[splitext(path)[1]]) - url = str(secret.get('peertube', 'peertube_url')).rstrip('/') + path = options.get('--file') + url = secret.get('peertube', 'peertube_url') + user_info = get_userinfo() # We need to transform fields into tuple to deal with tags as # MultipartEncoder does not support list refer @@ -70,8 +107,7 @@ def upload_video(oauth, secret, options): ("licence", "1"), ("description", options.get('--description') or "default description"), ("nsfw", str(int(options.get('--nsfw')) or "0")), - ("channelId", get_userinfo()), - ("videofile", get_file(options.get('--file'))) + ("videofile", get_videofile(path)) ] if options.get('--tags'): @@ -115,12 +151,21 @@ def upload_video(oauth, secret, options): fields.append(("thumbnailfile", get_file(options.get('--thumbnail')))) fields.append(("previewfile", get_file(options.get('--thumbnail')))) + if options.get('--playlist'): + playlist_id = get_playlist_by_name(user_info, options) + if not playlist_id and options.get('--playlistCreate'): + playlist_id = create_playlist(oauth, url, options) + else: + playlist_id = user_info['id'] + else: + playlist_id = user_info['id'] + fields.append(("channelId", str(playlist_id))) + multipart_data = MultipartEncoder(fields) headers = { 'Content-Type': multipart_data.content_type } - response = oauth.post(url + "/api/v1/videos/upload", data=multipart_data, headers=headers) From 9118f7b082c6825cca50c974dd7b3cb8d095b9d1 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:37 +0200 Subject: [PATCH 06/18] Add option to manage playlist for videos --- prismedia_upload.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prismedia_upload.py b/prismedia_upload.py index 27d8b8d..04473d8 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -37,6 +37,10 @@ Options: --thumbnail=STRING Path to a file to use as a thumbnail for the video. Supported types are jpg and jpeg. By default, prismedia search for an image based on video name followed by .jpg or .jpeg + --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube. + If the playlist is not found, spawn an error except if --playlist-create is set. + --playlistCreate Create the playlist if not exists. (default do not create) + Only relevant if --playlist is set. -h --help Show this help. --version Show version. @@ -211,6 +215,8 @@ if __name__ == '__main__': Optional('--thumbnail'): Or(None, And( str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'), ), + Optional('--playlist'): Or(None, str), + Optional('--playlistCreate'): bool, '--help': bool, '--version': bool }) From 461beaa5cb3516c1d1c0df55a5213f10aedee257 Mon Sep 17 00:00:00 2001 From: Zykino Date: Wed, 12 Sep 2018 00:45:18 +0200 Subject: [PATCH 07/18] peertube: fix default playlist --- lib/pt_upload.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index afbb8ff..2cc1083 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -48,8 +48,11 @@ def get_authenticated_service(secret): return oauth -def get_playlist_by_name(user_info, options): +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"]: if playlist['displayName'] == options.get('--playlist'): return playlist['id'] @@ -155,10 +158,11 @@ def upload_video(oauth, secret, options): playlist_id = get_playlist_by_name(user_info, options) if not playlist_id and options.get('--playlistCreate'): playlist_id = create_playlist(oauth, url, options) - else: - playlist_id = user_info['id'] + 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 = user_info['id'] + playlist_id = get_default_playlist(user_info) fields.append(("channelId", str(playlist_id))) multipart_data = MultipartEncoder(fields) From 972cfd73d36d6ea652449979ab7d8cfff6fb546b Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 13:50:39 +0200 Subject: [PATCH 08/18] Update peeror repo with new url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 713bf4c..9d25977 100644 --- a/README.md +++ b/README.md @@ -170,4 +170,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.drycat.fr/rigelk/Peeror) and [youtube-upload](https://github.com/tokland/youtube-upload) +inspired by [peeror](https://git.rigelk.eu/rigelk/peeror) and [youtube-upload](https://github.com/tokland/youtube-upload) \ No newline at end of file From cb8ae77a1052a218298e678a92e0aa8977e0ac7f Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:02 +0200 Subject: [PATCH 09/18] Add support for playlist on Peertube --- lib/pt_upload.py | 54 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index a765de9..997f335 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -48,17 +48,54 @@ def get_authenticated_service(secret): return oauth +def get_playlist_by_name(user_info, options): + + for playlist in user_info["videoChannels"]: + if playlist['displayName'] == options.get('--playlist'): + return playlist['id'] + + +def create_playlist(oauth, url, options): + template = ('Peertube : Playlist %s does not exist, creating it.') + logging.info(template % (str(options.get('--playlist')))) + data = '{"displayName":"' + str(options.get('--playlist')) +'", \ + "description":null}' + + headers = { + 'Content-Type': "application/json" + } + try: + response = oauth.post(url + "/api/v1/video-channels/", + 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: + jresponse = response.json() + jresponse = jresponse['videoChannel'] + return jresponse['id'] + else: + logging.error(('Peertube : The upload failed with an unexpected response: ' + '%s') % response) + exit(1) + + def upload_video(oauth, secret, options): def get_userinfo(): - user_info = json.loads(oauth.get(url + "/api/v1/users/me").content) - return str(user_info["id"]) + return json.loads(oauth.get(url+"/api/v1/users/me").content) def get_file(path): mimetypes.init() return (basename(path), open(abspath(path), 'rb'), mimetypes.types_map[splitext(path)[1]]) + path = options.get('--file') + user_info = get_userinfo() url = str(secret.get('peertube', 'peertube_url')).rstrip('/') # We need to transform fields into tuple to deal with tags as @@ -70,7 +107,6 @@ def upload_video(oauth, secret, options): ("licence", "1"), ("description", options.get('--description') or "default description"), ("nsfw", str(int(options.get('--nsfw')) or "0")), - ("channelId", get_userinfo()), ("videofile", get_file(options.get('--file'))) ] @@ -115,12 +151,21 @@ def upload_video(oauth, secret, options): fields.append(("thumbnailfile", get_file(options.get('--thumbnail')))) fields.append(("previewfile", get_file(options.get('--thumbnail')))) + if options.get('--playlist'): + playlist_id = get_playlist_by_name(user_info, options) + if not playlist_id and options.get('--playlistCreate'): + playlist_id = create_playlist(oauth, url, options) + else: + playlist_id = user_info['id'] + else: + playlist_id = user_info['id'] + fields.append(("channelId", str(playlist_id))) + multipart_data = MultipartEncoder(fields) headers = { 'Content-Type': multipart_data.content_type } - response = oauth.post(url + "/api/v1/videos/upload", data=multipart_data, headers=headers) @@ -138,7 +183,6 @@ def upload_video(oauth, secret, options): else: logging.error(('Peertube: The upload failed with an unexpected response: ' '%s') % response) - print(response.json()) exit(1) From 9efb18eb558a168c90e3643b48ad6bbfdeecf61a Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sat, 4 Aug 2018 14:07:37 +0200 Subject: [PATCH 10/18] Add option to manage playlist for videos --- prismedia_upload.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prismedia_upload.py b/prismedia_upload.py index 4634440..68ed0a2 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -37,6 +37,10 @@ Options: --thumbnail=STRING Path to a file to use as a thumbnail for the video. Supported types are jpg and jpeg. By default, prismedia search for an image based on video name followed by .jpg or .jpeg + --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube. + If the playlist is not found, spawn an error except if --playlist-create is set. + --playlistCreate Create the playlist if not exists. (default do not create) + Only relevant if --playlist is set. -h --help Show this help. --version Show version. @@ -211,6 +215,8 @@ if __name__ == '__main__': Optional('--thumbnail'): Or(None, And( str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'), ), + Optional('--playlist'): Or(None, str), + Optional('--playlistCreate'): bool, '--help': bool, '--version': bool }) From 95f6bc930f3be85ec57741ed30337dc439535a46 Mon Sep 17 00:00:00 2001 From: Zykino Date: Mon, 1 Oct 2018 21:24:34 +0200 Subject: [PATCH 11/18] fix merge: function name changed --- lib/pt_upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index 2cc1083..4dd6143 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -110,7 +110,7 @@ def upload_video(oauth, secret, options): ("licence", "1"), ("description", options.get('--description') or "default description"), ("nsfw", str(int(options.get('--nsfw')) or "0")), - ("videofile", get_videofile(path)) + ("videofile", get_file(path)) ] if options.get('--tags'): From 2d7b8e0b095cf02fb332c79e8b2ec8536f2ec7a4 Mon Sep 17 00:00:00 2001 From: Zykino Date: Tue, 2 Oct 2018 12:11:26 +0200 Subject: [PATCH 12/18] Update README to show the current state of the playlist integration --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f612c76..9e5ee38 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ Languages: - [x] thumbnail/preview - [x] multiple lines description (see [issue 4](https://git.lecygnenoir.info/LecygneNoir/prismedia/issues/4)) - [ ] add videos to playlist (YT & PT workflow: upload video, find playlist id, add video to playlist) + - [x] Peertube + - [ ] Youtube - [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 (need the [atd](https://linux.die.net/man/8/atd) daemon, [curl](https://linux.die.net/man/1/curl) and [jq](https://stedolan.github.io/jq/)) From b4691bba1e3827ca4d6731da65c545876da5fce8 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sun, 7 Oct 2018 21:26:21 +0200 Subject: [PATCH 13/18] Correct typo in readme for playlist feature --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 14cda0b..8c2b3d9 100644 --- a/README.md +++ b/README.md @@ -157,9 +157,8 @@ Languages: - [x] set default language - [x] thumbnail/preview - [x] multiple lines description (see [issue 4](https://git.lecygnenoir.info/LecygneNoir/prismedia/issues/4)) - - [ ] add videos to playlist (YT & PT workflow: upload video, find playlist id, add video to playlist) - - [x] Peertube - - [ ] Youtube + - [x] add videos to playlist for Peertube + - [ ] add videos to playlist for Youtube - [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 (need the [atd](https://linux.die.net/man/8/atd) daemon, [curl](https://linux.die.net/man/1/curl) and [jq](https://stedolan.github.io/jq/)) From 2565071e40dcfd3adaaa184181e0aacfd6bbc523 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 15 Oct 2018 12:42:09 +0200 Subject: [PATCH 14/18] Add examples for using playlist feature --- nfo_example.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nfo_example.txt b/nfo_example.txt index a4d4096..9a3890d 100644 --- a/nfo_example.txt +++ b/nfo_example.txt @@ -18,6 +18,8 @@ cca = True privacy = private disable-comments = True thumbnail = /path/to/your/thumbnail.jpg # Set the absolute path to your thumbnail +playlist = My Test Playlist +playlistCreate = True nsfw = True platform = youtube, peertube language = French From d3a42c4be1bc35218189ed69334748da00296537 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 15 Oct 2018 12:42:40 +0200 Subject: [PATCH 15/18] Patch playlist to be comaptible with beta16 (using name AND display name) --- lib/pt_upload.py | 11 ++++++----- lib/utils.py | 18 +++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index 4b57614..6e3a9cf 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -62,9 +62,10 @@ def get_playlist_by_name(user_info, options): def create_playlist(oauth, url, options): - template = ('Peertube : Playlist %s does not exist, creating it.') + template = ('Peertube: Playlist %s does not exist, creating it.') logging.info(template % (str(options.get('--playlist')))) - data = '{"displayName":"' + str(options.get('--playlist')) +'", \ + data = '{"name":"' + utils.cleanString(str(options.get('--playlist'))) +'", \ + "displayName":"' + str(options.get('--playlist')) +'", \ "description":null}' headers = { @@ -85,7 +86,7 @@ def create_playlist(oauth, url, options): jresponse = jresponse['videoChannel'] return jresponse['id'] else: - logging.error(('Peertube : The upload failed with an unexpected response: ' + logging.error(('Peertube: The upload failed with an unexpected response: ' '%s') % response) exit(1) @@ -101,7 +102,7 @@ def upload_video(oauth, secret, options): mimetypes.types_map[splitext(path)[1]]) path = options.get('--file') - url = secret.get('peertube', 'peertube_url') + url = str(secret.get('peertube', 'peertube_url')).rstrip('/') user_info = get_userinfo() # We need to transform fields into tuple to deal with tags as @@ -128,7 +129,7 @@ def upload_video(oauth, secret, options): exit(1) # If Mastodon compatibility is enabled, clean tags from special characters if options.get('--mt'): - strtag = utils.mastodonTag(strtag) + strtag = utils.cleanString(strtag) fields.append(("tags", strtag)) if options.get('--category'): diff --git a/lib/utils.py b/lib/utils.py index bfabaad..4234116 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -193,15 +193,15 @@ def parseNFO(options): def upcaseFirstLetter(s): return s[0].upper() + s[1:] -def mastodonTag(tag): - tags = tag.split(' ') - mtag = '' - for s in tags: +def cleanString(toclean): + toclean = toclean.split(' ') + cleaned = '' + for s in toclean: if s == '': continue - strtag = unicodedata.normalize('NFKD', unicode (s, 'utf-8')).encode('ASCII', 'ignore') - strtag = ''.join(e for e in strtag if e.isalnum()) - strtag = upcaseFirstLetter(strtag) - mtag = mtag + strtag + strtoclean = unicodedata.normalize('NFKD', unicode (s, 'utf-8')).encode('ASCII', 'ignore') + strtoclean = ''.join(e for e in strtoclean if e.isalnum()) + strtoclean = upcaseFirstLetter(strtoclean) + cleaned = cleaned + strtoclean - return mtag + return cleaned From 070408bb67c99fb130922f962e7dcd918bdf1a9b Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Thu, 18 Oct 2018 22:42:25 +0200 Subject: [PATCH 16/18] Add playlist feature to Youtube --- README.md | 2 +- lib/yt_upload.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 14c8a7e..189bb56 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ Languages: - [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 - - [ ] add videos to playlist for Youtube + - [x] add videos to playlist for Youtube - [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 diff --git a/lib/yt_upload.py b/lib/yt_upload.py index b28d495..0574aa1 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -51,13 +51,14 @@ RETRIABLE_STATUS_CODES = [500, 502, 503, 504] CLIENT_SECRETS_FILE = 'youtube_secret.json' CREDENTIALS_PATH = ".youtube_credentials.json" -SCOPES = ['https://www.googleapis.com/auth/youtube.upload'] +SCOPES = ['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube.force-ssl'] API_SERVICE_NAME = 'youtube' API_VERSION = 'v3' # Authorize the request and store authorization credentials. def get_authenticated_service(): + check_authenticated_scopes() flow = InstalledAppFlow.from_client_secrets_file( CLIENT_SECRETS_FILE, SCOPES) if exists(CREDENTIALS_PATH): @@ -121,6 +122,17 @@ def initialize_upload(youtube, options): publishAt = tz.localize(publishAt).isoformat() body['status']['publishAt'] = str(publishAt) + if options.get('--playlist'): + playlist_id = get_playlist_by_name(youtube, options.get('--playlist')) + if not playlist_id and options.get('--playlistCreate'): + playlist_id = create_playlist(youtube, options.get('--playlist')) + elif not playlist_id: + logging.warning("Youtube: Playlist `" + options.get('--playlist') + "` is unknown.") + logging.warning("If you want to create it, set the --playlistCreate option.") + playlist_id = "" + else: + playlist_id = "" + # Call the API's videos.insert method to create and upload the video. insert_request = youtube.videos().insert( part=','.join(body.keys()), @@ -133,9 +145,77 @@ def initialize_upload(youtube, options): if video_id and options.get('--thumbnail'): set_thumbnail(youtube, options.get('--thumbnail'), videoId=video_id) + # If we get a video_id, upload is successful and we are able to set playlist + if video_id and options.get('--playlist'): + set_playlist(youtube, playlist_id, video_id) + + +def get_playlist_by_name(youtube, playlist_name): + response = youtube.playlists().list( + part='snippet,id', + mine=True, + maxResults=50 + ).execute() + for playlist in response["items"]: + if playlist["snippet"]['title'] == playlist_name: + return playlist['id'] + + +def create_playlist(youtube, playlist_name): + template = ('Youtube: Playlist %s does not exist, creating it.') + logging.info(template % (str(playlist_name))) + resources = build_resource({'snippet.title': playlist_name, + 'snippet.description': '', + 'status.privacyStatus': 'public'}) + response = youtube.playlists().insert( + body=resources, + part='status,snippet,id' + ).execute() + return response["id"] + + +def build_resource(properties): + resource = {} + for p in properties: + # Given a key like "snippet.title", split into "snippet" and "title", where + # "snippet" will be an object and "title" will be a property in that object. + prop_array = p.split('.') + ref = resource + for pa in range(0, len(prop_array)): + is_array = False + key = prop_array[pa] + + # For properties that have array values, convert a name like + # "snippet.tags[]" to snippet.tags, and set a flag to handle + # the value as an array. + if key[-2:] == '[]': + key = key[0:len(key)-2:] + is_array = True + + if pa == (len(prop_array) - 1): + # Leave properties without values out of inserted resource. + if properties[p]: + if is_array: + ref[key] = properties[p].split(',') + else: + ref[key] = properties[p] + elif key not in ref: + # For example, the property is "snippet.title", but the resource does + # not yet have a "snippet" object. Create the snippet object here. + # Setting "ref = ref[key]" means that in the next time through the + # "for pa in range ..." loop, we will be setting a property in the + # resource's "snippet" object. + ref[key] = {} + ref = ref[key] + else: + # For example, the property is "snippet.description", and the resource + # already has a "snippet" object. + ref = ref[key] + return resource + def set_thumbnail(youtube, media_file, **kwargs): - kwargs = utils.remove_empty_kwargs(**kwargs) # See full sample for function + kwargs = utils.remove_empty_kwargs(**kwargs) request = youtube.thumbnails().set( media_body=MediaFileUpload(media_file, chunksize=-1, resumable=True), @@ -146,6 +226,26 @@ def set_thumbnail(youtube, media_file, **kwargs): return resumable_upload(request, 'thumbnail', 'set') +def set_playlist(youtube, playlist_id, video_id): + logging.info('Youtube: Configuring playlist...') + resource = build_resource({'snippet.playlistId': playlist_id, + 'snippet.resourceId.kind': 'youtube#video', + 'snippet.resourceId.videoId': video_id, + 'snippet.position': ''} + ) + try: + youtube.playlistItems().insert( + body=resource, + part='snippet' + ).execute() + except Exception as e: + if hasattr(e, 'message'): + logging.error("Youtube: Error: " + str(e.message)) + else: + logging.error("Youtube: Error: " + str(e)) + logging.info('Youtube: Video is correclty added to the playlist.') + + # This method implements an exponential backoff strategy to resume a # failed upload. def resumable_upload(request, resource, method): From 75aa36e1aa83b5f81f5f223e6e9f627dc615bfcd Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Thu, 18 Oct 2018 22:52:06 +0200 Subject: [PATCH 17/18] Check if scopes are corrects in youtube credentials --- lib/yt_upload.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/yt_upload.py b/lib/yt_upload.py index 0574aa1..82632a5 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -9,6 +9,7 @@ import time import copy import json from os.path import splitext, basename, exists +import os import google.oauth2.credentials import datetime import pytz @@ -80,6 +81,16 @@ def get_authenticated_service(): return build(API_SERVICE_NAME, API_VERSION, credentials=credentials, cache_discovery=False) +def check_authenticated_scopes(): + if exists(CREDENTIALS_PATH): + with open(CREDENTIALS_PATH, 'r') as f: + credential_params = json.load(f) + # Check if all scopes are present + if credential_params["_scopes"] != SCOPES: + logging.warning("Youtube: Credentials are obsolete, need to re-authenticate.") + os.remove(CREDENTIALS_PATH) + + def initialize_upload(youtube, options): path = options.get('--file') tags = None From 20d1ab6a112c38cf12f69c29782f56c7934b62d0 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Thu, 18 Oct 2018 22:53:45 +0200 Subject: [PATCH 18/18] Use console instead of local_server for authentication in Youtube to enable scripting --- 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 82632a5..daebdc9 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -73,7 +73,7 @@ def get_authenticated_service(): client_secret=credential_params["_client_secret"] ) else: - credentials = flow.run_local_server() + credentials = flow.run_console() with open(CREDENTIALS_PATH, 'w') as f: p = copy.deepcopy(vars(credentials)) del p["expiry"]