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.

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