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.

76 lines
3.0 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. from ConfigParser import RawConfigParser
  9. from requests_oauthlib import OAuth2Session
  10. from oauthlib.oauth2 import LegacyApplicationClient
  11. from requests_toolbelt.multipart.encoder import MultipartEncoder
  12. PEERTUBE_SECRETS_FILE = 'peertube_secret'
  13. def get_authenticated_service(config):
  14. oauth = OAuth2Session(client=LegacyApplicationClient(client_id=str(config.get('peertube', 'client_id'))))
  15. oauth.fetch_token(token_url=str(config.get('peertube', 'peertube_url')) + '/api/v1/users/token',
  16. username=str(config.get('peertube', 'username').lower()), #lower as peertube does not store uppecase for pseudo
  17. password=str(config.get('peertube', 'password')),
  18. client_id=str(config.get('peertube', 'client_id')),
  19. client_secret=str(config.get('peertube', 'client_secret'))
  20. )
  21. return oauth
  22. def upload_video(oauth, config, options):
  23. def get_userinfo():
  24. user_info = json.loads(oauth.get(url+"/api/v1/users/me").content)
  25. return str(user_info["id"])
  26. def get_videofile(path):
  27. mimetypes.init()
  28. return (os.path.basename(path), open(os.path.abspath(path), 'rb'),
  29. mimetypes.types_map[os.path.splitext(path)[1]])
  30. path = options.get('--file')
  31. url = config.get('peertube', 'peertube_url')
  32. fields = {
  33. "name": options.get('--name') or os.path.splitext(os.path.basename(path))[0],
  34. "category": str(options.get('--category') or 1), # look at the list numbers at /videos/categories
  35. "licence": str(options.get('--licence') or 1), # look at the list numbers at /videos/licences
  36. "description": options.get('--description') or "",
  37. "privacy": str(options.get('--privacy') or 3), # look at the list numbers at /videos/privacies
  38. "nsfw": str(options.get('--nsfw') or 0),
  39. "commentsEnabled": "1",
  40. "channelId": get_userinfo(),
  41. "videofile": get_videofile(path) # beware, see validateVideo for supported types
  42. }
  43. multipart_data = MultipartEncoder(fields=fields)
  44. headers = {
  45. 'Content-Type': multipart_data.content_type
  46. }
  47. response = oauth.post(config.get('peertube', 'peertube_url')+"/api/v1/videos/upload", data=multipart_data, headers=headers)
  48. if response is not None:
  49. if response.status_code == 200:
  50. print 'Peertube : Video was successfully uploaded.'
  51. else:
  52. exit('Peertube : The upload failed with an unexpected response: %s' % response)
  53. def run(options):
  54. config = RawConfigParser()
  55. config.read(PEERTUBE_SECRETS_FILE)
  56. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
  57. oauth = get_authenticated_service(config)
  58. try:
  59. print 'Peertube : Uploading file...'
  60. upload_video(oauth, config, options)
  61. except Exception as e:
  62. if hasattr(e, 'message'):
  63. print(e.message)
  64. else:
  65. print(e)