From 9b3d793975096a6677145eddc9a1c6d5d6061fe6 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Tue, 18 Jun 2019 12:58:02 +0200 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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 = (