Scripting way to upload videos to peertube and youtube
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. import os
  4. import mimetypes
  5. import json
  6. from os.path import splitext, basename, abspath
  7. from ConfigParser import RawConfigParser
  8. from requests_oauthlib import OAuth2Session
  9. from oauthlib.oauth2 import LegacyApplicationClient
  10. from requests_toolbelt.multipart.encoder import MultipartEncoder
  11. import utils
  12. PEERTUBE_SECRETS_FILE = 'peertube_secret'
  13. def get_authenticated_service(config):
  14. peertube_url = str(config.get('peertube', 'peertube_url'))
  15. oauth_client = LegacyApplicationClient(
  16. client_id=str(config.get('peertube', 'client_id'))
  17. )
  18. oauth = OAuth2Session(client=oauth_client)
  19. oauth.fetch_token(
  20. token_url=peertube_url + '/api/v1/users/token',
  21. # lower as peertube does not store uppecase for pseudo
  22. username=str(config.get('peertube', 'username').lower()),
  23. password=str(config.get('peertube', 'password')),
  24. client_id=str(config.get('peertube', 'client_id')),
  25. client_secret=str(config.get('peertube', 'client_secret'))
  26. )
  27. return oauth
  28. def upload_video(oauth, config, options):
  29. def get_userinfo():
  30. user_info = json.loads(oauth.get(url + "/api/v1/users/me").content)
  31. return str(user_info["id"])
  32. def get_videofile(path):
  33. mimetypes.init()
  34. return (basename(path), open(abspath(path), 'rb'),
  35. mimetypes.types_map[splitext(path)[1]])
  36. path = options.get('--file')
  37. url = config.get('peertube', 'peertube_url')
  38. tags = None
  39. # We need to transform fields into tuple to deal with tags as
  40. # MultipartEncoder does not support list refer
  41. # https://github.com/requests/toolbelt/issues/190 and
  42. # https://github.com/requests/toolbelt/issues/205
  43. fields = [
  44. ("name", options.get('--name') or splitext(basename(path))[0]),
  45. # look at the list numbers at /videos/licences
  46. ("licence", str(options.get('--licence') or 1)),
  47. ("description", options.get('--description') or "default description"),
  48. # look at the list numbers at /videos/privacies
  49. ("privacy", str(options.get('--privacy') or 3)),
  50. ("nsfw", str(options.get('--nsfw') or 0)),
  51. ("commentsEnabled", "1"),
  52. ("channelId", get_userinfo()),
  53. ("videofile", get_videofile(path))
  54. ]
  55. if options.get('--tags'):
  56. tags = options.get('--tags').split(',')
  57. for strtags in tags:
  58. fields.append(("tags", strtags))
  59. if options.get('--category'):
  60. fields.append(("category", str(utils.getCategory(options.get('--category'), 'peertube'))))
  61. else:
  62. #if no category, set default to 2 (Films)
  63. fields.append(("category", "2"))
  64. multipart_data = MultipartEncoder(fields)
  65. headers = {
  66. 'Content-Type': multipart_data.content_type
  67. }
  68. response = oauth.post(url + "/api/v1/videos/upload",
  69. data=multipart_data,
  70. headers=headers)
  71. if response is not None:
  72. if response.status_code == 200:
  73. print('Peertube : Video was successfully uploaded.')
  74. else:
  75. exit(('Peertube : The upload failed with an unexpected response: '
  76. '%s') % response)
  77. def run(options):
  78. config = RawConfigParser()
  79. config.read(PEERTUBE_SECRETS_FILE)
  80. insecure_transport = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
  81. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
  82. oauth = get_authenticated_service(config)
  83. try:
  84. print('Peertube : Uploading file...')
  85. upload_video(oauth, config, options)
  86. except Exception as e:
  87. if hasattr(e, 'message'):
  88. print(e.message)
  89. else:
  90. print(e)