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)