From 2e8f019cdb7a402659a2f39d42c7f5fd93662995 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Wed, 7 Mar 2018 14:15:52 +0100 Subject: [PATCH] peertube upload now supported --- README.md | 2 +- lib/pt_upload.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++- ptyt_upload.py | 3 +- 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f8f3d7b..9ce4c61 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Options: ## Features - [X] Youtube upload -- [ ] Peertube upload +- [X] Peertube upload - [ ] Support of all videos arguments (description, tags, category, licence, ...) - [ ] Use file to retrieve videos arguments - [ ] Record and forget: put the video in a directory, and the script uploads it for you diff --git a/lib/pt_upload.py b/lib/pt_upload.py index c2b40fb..d262a08 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -1,2 +1,77 @@ #!/usr/bin/python -# coding: utf-8 \ No newline at end of file +# coding: utf-8 + +import os +import mimetypes +import httplib +import httplib2 +import json + +from ConfigParser import RawConfigParser +from requests_oauthlib import OAuth2Session +from oauthlib.oauth2 import LegacyApplicationClient +from requests_toolbelt.multipart.encoder import MultipartEncoder + +PEERTUBE_SECRETS_FILE = 'peertube_secret' + +def get_authenticated_service(config): + oauth = OAuth2Session(client=LegacyApplicationClient(client_id=str(config.get('peertube', 'client_id')))) + oauth.fetch_token(token_url=str(config.get('peertube', 'peertube_url')) + '/api/v1/users/token', + username=str(config.get('peertube', 'username').lower()), #lower as peertube does not store uppecase for pseudo + password=str(config.get('peertube', 'password')), + client_id=str(config.get('peertube', 'client_id')), + client_secret=str(config.get('peertube', 'client_secret')) + ) + return oauth + +def upload_video(oauth, config, options): + + def get_userinfo(): + user_info = json.loads(oauth.get(url+"/api/v1/users/me").content) + return str(user_info["id"]) + + def get_videofile(path): + mimetypes.init() + return (os.path.basename(path), open(os.path.abspath(path), 'rb'), + mimetypes.types_map[os.path.splitext(path)[1]]) + + path = options.get('--file') + url = config.get('peertube', 'peertube_url') + fields = { + "name": options.get('--name') or os.path.splitext(os.path.basename(path))[0], + "category": str(options.get('--category') or 1), # look at the list numbers at /videos/categories + "licence": str(options.get('--licence') or 1), # look at the list numbers at /videos/licences + "description": options.get('--description') or "", + "privacy": str(options.get('--privacy') or 3), # look at the list numbers at /videos/privacies + "nsfw": str(options.get('--nsfw') or 0), + "commentsEnabled": "1", + "channelId": get_userinfo(), + "videofile": get_videofile(path) # beware, see validateVideo for supported types + } + + multipart_data = MultipartEncoder(fields=fields) + headers = { + 'Content-Type': multipart_data.content_type + } + + response = oauth.post(config.get('peertube', 'peertube_url')+"/api/v1/videos/upload", data=multipart_data, headers=headers) + if response is not None: + if response.status_code == 200: + print 'Peertube : Video was successfully uploaded.' + else: + exit('Peertube : The upload failed with an unexpected response: %s' % response) + + +def run(options): + config = RawConfigParser() + config.read(PEERTUBE_SECRETS_FILE) + os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT') + oauth = get_authenticated_service(config) + try: + print 'Peertube : Uploading file...' + upload_video(oauth, config, options) + except Exception as e: + if hasattr(e, 'message'): + print(e.message) + else: + print(e) \ No newline at end of file diff --git a/ptyt_upload.py b/ptyt_upload.py index b6a8a77..0fa881c 100755 --- a/ptyt_upload.py +++ b/ptyt_upload.py @@ -69,4 +69,5 @@ if __name__ == '__main__': except SchemaError as e: exit(e) - yt_upload.run(options) \ No newline at end of file + yt_upload.run(options) + pt_upload.run(options) \ No newline at end of file