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.

89 lines
3.5 KiB

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