#14 Fix #9 peertube default playlist

Merged
LecygneNoir merged 6 commits from Zykino/prismedia:feature/playlist into feature/playlist 5 years ago
  1. +3
    -1
      README.md
  2. +55
    -6
      lib/pt_upload.py
  3. +6
    -0
      prismedia_upload.py

+ 3
- 1
README.md View File

@ -157,6 +157,8 @@ Languages:
- [x] thumbnail/preview - [x] thumbnail/preview
- [x] multiple lines description (see [issue 4](https://git.lecygnenoir.info/LecygneNoir/prismedia/issues/4)) - [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) - [ ] 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] 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] 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/)) - [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/))
@ -169,4 +171,4 @@ Languages:
If your server uses peertube before 1.0.0-beta4, use the version inside tag 1.0.0-beta3! If your server uses peertube before 1.0.0-beta4, use the version inside tag 1.0.0-beta3!
## Sources ## 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)

+ 55
- 6
lib/pt_upload.py View File

@ -48,18 +48,58 @@ def get_authenticated_service(secret):
return oauth return oauth
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']
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 upload_video(oauth, secret, options):
def get_userinfo(): 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): def get_file(path):
mimetypes.init() mimetypes.init()
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]])
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 # We need to transform fields into tuple to deal with tags as
# MultipartEncoder does not support list refer # MultipartEncoder does not support list refer
@ -70,8 +110,7 @@ def upload_video(oauth, secret, options):
("licence", "1"), ("licence", "1"),
("description", options.get('--description') or "default description"), ("description", options.get('--description') or "default description"),
("nsfw", str(int(options.get('--nsfw')) or "0")), ("nsfw", str(int(options.get('--nsfw')) or "0")),
("channelId", get_userinfo()),
("videofile", get_file(options.get('--file')))
("videofile", get_file(path))
] ]
if options.get('--tags'): if options.get('--tags'):
@ -115,12 +154,22 @@ def upload_video(oauth, secret, options):
fields.append(("thumbnailfile", get_file(options.get('--thumbnail')))) fields.append(("thumbnailfile", get_file(options.get('--thumbnail'))))
fields.append(("previewfile", 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)
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)))
multipart_data = MultipartEncoder(fields) multipart_data = MultipartEncoder(fields)
headers = { headers = {
'Content-Type': multipart_data.content_type 'Content-Type': multipart_data.content_type
} }
response = oauth.post(url + "/api/v1/videos/upload", response = oauth.post(url + "/api/v1/videos/upload",
data=multipart_data, data=multipart_data,
headers=headers) headers=headers)

+ 6
- 0
prismedia_upload.py View File

@ -37,6 +37,10 @@ Options:
--thumbnail=STRING Path to a file to use as a thumbnail for the video. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
Supported types are jpg and jpeg. Supported types are jpg and jpeg.
By default, prismedia search for an image based on video name followed by .jpg or .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. -h --help Show this help.
--version Show version. --version Show version.
@ -211,6 +215,8 @@ if __name__ == '__main__':
Optional('--thumbnail'): Or(None, And( Optional('--thumbnail'): Or(None, And(
str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'), str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
), ),
Optional('--playlist'): Or(None, str),
Optional('--playlistCreate'): bool,
'--help': bool, '--help': bool,
'--version': bool '--version': bool
}) })

Loading…
Cancel
Save