mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-03 17:39:16 +02:00
Peertube: modify upload to ever use default channel and create true playlist instead. Playlist created as private for the moment
This commit is contained in:
parent
3b38290040
commit
9b3d793975
1 changed files with 33 additions and 29 deletions
|
@ -51,12 +51,16 @@ def get_authenticated_service(secret):
|
||||||
return oauth
|
return oauth
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_channel(user_info):
|
||||||
|
return user_info['videoChannels'][0]['id']
|
||||||
|
|
||||||
|
|
||||||
def get_default_playlist(user_info):
|
def get_default_playlist(user_info):
|
||||||
return user_info['videoChannels'][0]['id']
|
return user_info['videoChannels'][0]['id']
|
||||||
|
|
||||||
|
|
||||||
def get_playlist_by_name(user_info, options):
|
def get_playlist_by_name(user_playlists, options):
|
||||||
for playlist in user_info["videoChannels"]:
|
for playlist in user_playlists["data"]:
|
||||||
if playlist['displayName'].encode('utf8') == str(options.get('--playlist')):
|
if playlist['displayName'].encode('utf8') == str(options.get('--playlist')):
|
||||||
return playlist['id']
|
return playlist['id']
|
||||||
|
|
||||||
|
@ -64,20 +68,16 @@ def get_playlist_by_name(user_info, options):
|
||||||
def create_playlist(oauth, url, 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'))))
|
logging.info(template % (str(options.get('--playlist'))))
|
||||||
playlist_name = utils.cleanString(str(options.get('--playlist')))
|
# We use files for form-data Content
|
||||||
# Peertube allows 20 chars max for playlist name
|
# see https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file
|
||||||
playlist_name = playlist_name[:19]
|
files = {'displayName': (None, str(options.get('--playlist'))),
|
||||||
data = '{"name":"' + playlist_name +'", \
|
'privacy': (None, "3"),
|
||||||
"displayName":"' + str(options.get('--playlist')) +'", \
|
'description': (None, "null"),
|
||||||
"description":null}'
|
'videoChannelId': (None, "null"),
|
||||||
|
'thumbnailfile': (None, "null")}
|
||||||
headers = {
|
|
||||||
'Content-Type': "application/json"
|
|
||||||
}
|
|
||||||
try:
|
try:
|
||||||
response = oauth.post(url + "/api/v1/video-channels/",
|
response = oauth.post(url + "/api/v1/video-playlists/",
|
||||||
data=data,
|
files=files)
|
||||||
headers=headers)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if hasattr(e, 'message'):
|
if hasattr(e, 'message'):
|
||||||
logging.error("Error: " + str(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 is not None:
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
jresponse = response.json()
|
jresponse = response.json()
|
||||||
jresponse = jresponse['videoChannel']
|
jresponse = jresponse['videoPlaylist']
|
||||||
return jresponse['id']
|
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:
|
else:
|
||||||
logging.error(('Peertube: The upload failed with an unexpected response: '
|
logging.error(('Peertube: The upload failed with an unexpected response: '
|
||||||
'%s') % response)
|
'%s') % response)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
def set_playlist(oauth, url, video_id, playlist_id):
|
||||||
|
logging.info('Peertube: add video to playlist.')
|
||||||
|
|
||||||
def upload_video(oauth, secret, options):
|
def upload_video(oauth, secret, options):
|
||||||
|
|
||||||
|
@ -109,9 +106,13 @@ def upload_video(oauth, secret, options):
|
||||||
return (basename(path), open(abspath(path), 'rb'),
|
return (basename(path), open(abspath(path), 'rb'),
|
||||||
mimetypes.types_map[splitext(path)[1]])
|
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')
|
path = options.get('--file')
|
||||||
url = str(secret.get('peertube', 'peertube_url')).rstrip('/')
|
url = str(secret.get('peertube', 'peertube_url')).rstrip('/')
|
||||||
user_info = get_userinfo()
|
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
|
# We need to transform fields into tuple to deal with tags as
|
||||||
# MultipartEncoder does not support list refer
|
# MultipartEncoder does not support list refer
|
||||||
|
@ -175,15 +176,16 @@ def upload_video(oauth, secret, options):
|
||||||
fields.append(("previewfile", get_file(options.get('--thumbnail'))))
|
fields.append(("previewfile", get_file(options.get('--thumbnail'))))
|
||||||
|
|
||||||
if options.get('--playlist'):
|
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'):
|
if not playlist_id and options.get('--playlistCreate'):
|
||||||
playlist_id = create_playlist(oauth, url, options)
|
playlist_id = create_playlist(oauth, url, options)
|
||||||
elif not playlist_id:
|
else:
|
||||||
logging.warning("Playlist `" + options.get('--playlist') + "` is unknown, using default playlist.")
|
logging.warning("Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate"
|
||||||
playlist_id = get_default_playlist(user_info)
|
" if you want to create it")
|
||||||
else:
|
exit(1)
|
||||||
playlist_id = get_default_playlist(user_info)
|
|
||||||
fields.append(("channelId", str(playlist_id)))
|
default_channel = get_default_channel(user_info)
|
||||||
|
fields.append(("channelId", str(default_channel)))
|
||||||
|
|
||||||
multipart_data = MultipartEncoder(fields)
|
multipart_data = MultipartEncoder(fields)
|
||||||
|
|
||||||
|
@ -198,10 +200,12 @@ def upload_video(oauth, secret, options):
|
||||||
jresponse = response.json()
|
jresponse = response.json()
|
||||||
jresponse = jresponse['video']
|
jresponse = jresponse['video']
|
||||||
uuid = jresponse['uuid']
|
uuid = jresponse['uuid']
|
||||||
idvideo = str(jresponse['id'])
|
video_id = str(jresponse['id'])
|
||||||
logging.info('Peertube : Video was successfully uploaded.')
|
logging.info('Peertube : Video was successfully uploaded.')
|
||||||
template = 'Peertube: Watch it at %s/videos/watch/%s.'
|
template = 'Peertube: Watch it at %s/videos/watch/%s.'
|
||||||
logging.info(template % (url, uuid))
|
logging.info(template % (url, uuid))
|
||||||
|
# if options.get('--playlist'):
|
||||||
|
# set_playlist(oauth, url, video_id, playlist_id)
|
||||||
else:
|
else:
|
||||||
logging.error(('Peertube: The upload failed with an unexpected response: '
|
logging.error(('Peertube: The upload failed with an unexpected response: '
|
||||||
'%s') % response)
|
'%s') % response)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue