diff --git a/CHANGELOG.md b/CHANGELOG.md index a507abd..403319f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,19 @@ # 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 +This fix prepares the python3 compatibility. +**Warning** you need a new prerequisites: python-unidecode - Remove mastodon tags (mt) options as it's deprecated. Compatibility between Peertube and Mastodon is complete. + - Simplify python2 specific functions ## v0.6.1 diff --git a/README.md b/README.md index cd712e9..923a411 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Search in your package manager, otherwise use ``pip install --upgrade`` - python-magic-bin - requests-toolbelt - tzlocal + - unidecode ## Configuration @@ -98,7 +99,8 @@ Options: --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled) --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe) --nfo=STRING Configure a specific nfo file to set options for the video. - By default Prismedia search a .txt based on video name + By default Prismedia search a .txt based on the video name and will + decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded) See nfo_example.txt for more details --platform=STRING List of platform(s) to upload to, comma separated. Supported platforms are youtube and peertube (default is both) @@ -149,8 +151,8 @@ Languages: - [x] set default language - [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 - - [x] add videos to playlist for Youtube + - [x] add videos to playlist + - [x] create playlist - [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 @@ -163,4 +165,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.rigelk.eu/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) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index ad0a36a..4b38b22 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,44 @@ 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}' + # 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, "1"), + 'description': (None, "null"), + 'videoChannelId': (None, "null"), + 'thumbnailfile': (None, "null")} + try: + response = oauth.post(url + "/api/v1/video-playlists/", + files=files) + 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['videoPlaylist'] + return jresponse['id'] + else: + 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-channels/", - data=data, - headers=headers) + 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)) @@ -85,16 +113,9 @@ def create_playlist(oauth, url, options): logging.error("Error: " + str(e)) if response is not None: if response.status_code == 200: - jresponse = response.json() - jresponse = jresponse['videoChannel'] - 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) + logging.info('Peertube: Video is successfully added to the playlist.') else: - logging.error(('Peertube: The upload failed with an unexpected response: ' + logging.error(('Peertube: Configuring the playlist failed with an unexpected response: ' '%s') % response) exit(1) @@ -109,9 +130,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 +200,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))) + 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 +224,13 @@ 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)) + # 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) diff --git a/lib/utils.py b/lib/utils.py index 63083b0..1602ab6 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -124,7 +124,6 @@ def searchThumbnail(options): options['--thumbnail'] = video_directory + video_file + ".jpeg" return options - # return the nfo as a RawConfigParser object def loadNFO(options): video_directory = dirname(options.get('--file')) + "/" @@ -169,7 +168,6 @@ def loadNFO(options): logging.info("No suitable NFO found, skipping.") return False - def parseNFO(options): nfo = loadNFO(options) if nfo: @@ -190,14 +188,26 @@ def parseNFO(options): exit(1) return options - def upcaseFirstLetter(s): return s[0].upper() + s[1:] - def cleanString(toclean): toclean = toclean.decode('utf-8') toclean = unidecode.unidecode(toclean) cleaned = re.sub('[^A-Za-z0-9]+', '', toclean) return cleaned + +def decodeArgumentStrings(options, encoding): + # Python crash when decoding from UTF-8 to UTF-8, so we prevent this + if "utf-8" == encoding.lower(): + return; + + if options["--name"] is not None: + options["--name"] = options["--name"].decode(encoding) + + if options["--description"] is not None: + options["--description"] = options["--description"].decode(encoding) + + if options["--tags"] is not None: + options["--tags"] = options["--tags"].decode(encoding) diff --git a/lib/yt_upload.py b/lib/yt_upload.py index daebdc9..0dfbc57 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -254,7 +254,7 @@ def set_playlist(youtube, playlist_id, video_id): logging.error("Youtube: Error: " + str(e.message)) else: logging.error("Youtube: Error: " + str(e)) - logging.info('Youtube: Video is correclty added to the playlist.') + logging.info('Youtube: Video is correctly added to the playlist.') # This method implements an exponential backoff strategy to resume a diff --git a/prismedia_upload.py b/prismedia_upload.py index ba86b86..71ba287 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -62,12 +62,12 @@ Languages: from os.path import dirname, realpath import sys import datetime +import locale import logging logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) from docopt import docopt - # Allows a relative import from the parent folder sys.path.insert(0, dirname(realpath(__file__)) + "/lib") @@ -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 = ( @@ -108,7 +108,6 @@ VALID_LANGUAGES = ('arabic', 'english', 'french', 'japanese', 'korean', 'mandarin', 'portuguese', 'punjabi', 'russian', 'spanish') - def validateVideo(path): supported_types = ['video/mp4'] if magic.from_file(path, mime=True) in supported_types: @@ -116,21 +115,18 @@ def validateVideo(path): else: return False - def validateCategory(category): if category.lower() in VALID_CATEGORIES: return True else: return False - def validatePrivacy(privacy): if privacy.lower() in VALID_PRIVACY_STATUSES: return True else: return False - def validatePlatform(platform): for plfrm in platform.split(','): if plfrm.lower().replace(" ", "") not in VALID_PLATFORM: @@ -138,14 +134,12 @@ def validatePlatform(platform): return True - def validateLanguage(language): if language.lower() in VALID_LANGUAGES: return True else: return False - def validatePublish(publish): # Check date format and if date is future try: @@ -171,17 +165,17 @@ if __name__ == '__main__': schema = Schema({ '--file': And(str, validateVideo, error='file is not supported, please use mp4'), Optional('--name'): Or(None, And( - str, + basestring, lambda x: not x.isdigit(), error="The video name should be a string") ), Optional('--description'): Or(None, And( - str, + basestring, lambda x: not x.isdigit(), - error="The video name should be a string") + error="The video description should be a string") ), Optional('--tags'): Or(None, And( - str, + basestring, lambda x: not x.isdigit(), error="Tags should be a string") ), @@ -219,6 +213,7 @@ if __name__ == '__main__': '--version': bool }) + utils.decodeArgumentStrings(options, locale.getpreferredencoding()) options = utils.parseNFO(options) if not options.get('--thumbnail'):