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.

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