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.

210 lines
7.5 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
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. import os
  4. import mimetypes
  5. import json
  6. import logging
  7. from os.path import splitext, basename, abspath
  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. import utils
  13. PEERTUBE_SECRETS_FILE = 'peertube_secret'
  14. PEERTUBE_PRIVACY = {
  15. "public": 1,
  16. "unlisted": 2,
  17. "private": 3
  18. }
  19. def get_authenticated_service(secret):
  20. peertube_url = str(secret.get('peertube', 'peertube_url')).rstrip("/")
  21. oauth_client = LegacyApplicationClient(
  22. client_id=str(secret.get('peertube', 'client_id'))
  23. )
  24. try:
  25. oauth = OAuth2Session(client=oauth_client)
  26. oauth.fetch_token(
  27. token_url=str(peertube_url + '/api/v1/users/token'),
  28. # lower as peertube does not store uppercase for pseudo
  29. username=str(secret.get('peertube', 'username').lower()),
  30. password=str(secret.get('peertube', 'password')),
  31. client_id=str(secret.get('peertube', 'client_id')),
  32. client_secret=str(secret.get('peertube', 'client_secret'))
  33. )
  34. except Exception as e:
  35. if hasattr(e, 'message'):
  36. logging.error("Peertube: Error: " + str(e.message))
  37. exit(1)
  38. else:
  39. logging.error("Peertube: Error: " + str(e))
  40. exit(1)
  41. return oauth
  42. def get_default_playlist(user_info):
  43. return user_info['videoChannels'][0]['id']
  44. def get_playlist_by_name(user_info, options):
  45. for playlist in user_info["videoChannels"]:
  46. if playlist['displayName'] == options.get('--playlist'):
  47. return playlist['id']
  48. def create_playlist(oauth, url, options):
  49. template = ('Peertube : Playlist %s does not exist, creating it.')
  50. logging.info(template % (str(options.get('--playlist'))))
  51. data = '{"displayName":"' + str(options.get('--playlist')) +'", \
  52. "description":null}'
  53. headers = {
  54. 'Content-Type': "application/json"
  55. }
  56. try:
  57. response = oauth.post(url + "/api/v1/video-channels/",
  58. data=data,
  59. headers=headers)
  60. except Exception as e:
  61. if hasattr(e, 'message'):
  62. logging.error("Error: " + str(e.message))
  63. else:
  64. logging.error("Error: " + str(e))
  65. if response is not None:
  66. if response.status_code == 200:
  67. jresponse = response.json()
  68. jresponse = jresponse['videoChannel']
  69. return jresponse['id']
  70. else:
  71. logging.error(('Peertube : The upload failed with an unexpected response: '
  72. '%s') % response)
  73. exit(1)
  74. def upload_video(oauth, secret, options):
  75. def get_userinfo():
  76. return json.loads(oauth.get(url+"/api/v1/users/me").content)
  77. def get_file(path):
  78. mimetypes.init()
  79. return (basename(path), open(abspath(path), 'rb'),
  80. mimetypes.types_map[splitext(path)[1]])
  81. path = options.get('--file')
  82. url = secret.get('peertube', 'peertube_url')
  83. user_info = get_userinfo()
  84. # We need to transform fields into tuple to deal with tags as
  85. # MultipartEncoder does not support list refer
  86. # https://github.com/requests/toolbelt/issues/190 and
  87. # https://github.com/requests/toolbelt/issues/205
  88. fields = [
  89. ("name", options.get('--name') or splitext(basename(options.get('--file')))[0]),
  90. ("licence", "1"),
  91. ("description", options.get('--description') or "default description"),
  92. ("nsfw", str(int(options.get('--nsfw')) or "0")),
  93. ("videofile", get_videofile(path))
  94. ]
  95. if options.get('--tags'):
  96. tags = options.get('--tags').split(',')
  97. for strtag in tags:
  98. # Empty tag crashes Peertube, so skip them
  99. if strtag == "":
  100. continue
  101. # Tag more than 30 chars crashes Peertube, so exit and check tags
  102. if len(strtag) >= 30:
  103. logging.warning("Peertube: Sorry, Peertube does not support tag with more than 30 characters, please reduce your tag size")
  104. exit(1)
  105. # If Mastodon compatibility is enabled, clean tags from special characters
  106. if options.get('--mt'):
  107. strtag = utils.mastodonTag(strtag)
  108. fields.append(("tags", strtag))
  109. if options.get('--category'):
  110. fields.append(("category", str(utils.getCategory(options.get('--category'), 'peertube'))))
  111. else:
  112. # if no category, set default to 2 (Films)
  113. fields.append(("category", "2"))
  114. if options.get('--language'):
  115. fields.append(("language", str(utils.getLanguage(options.get('--language'), "peertube"))))
  116. else:
  117. # if no language, set default to 1 (English)
  118. fields.append(("language", "en"))
  119. if options.get('--privacy'):
  120. fields.append(("privacy", str(PEERTUBE_PRIVACY[options.get('--privacy').lower()])))
  121. else:
  122. fields.append(("privacy", "3"))
  123. if options.get('--disable-comments'):
  124. fields.append(("commentsEnabled", "0"))
  125. else:
  126. fields.append(("commentsEnabled", "1"))
  127. if options.get('--thumbnail'):
  128. fields.append(("thumbnailfile", get_file(options.get('--thumbnail'))))
  129. fields.append(("previewfile", get_file(options.get('--thumbnail'))))
  130. if options.get('--playlist'):
  131. playlist_id = get_playlist_by_name(user_info, options)
  132. if not playlist_id and options.get('--playlistCreate'):
  133. playlist_id = create_playlist(oauth, url, options)
  134. elif not playlist_id:
  135. logging.warning("Playlist `" + options.get('--playlist') + "` is unknown, using default playlist.")
  136. playlist_id = get_default_playlist(user_info)
  137. else:
  138. playlist_id = get_default_playlist(user_info)
  139. fields.append(("channelId", str(playlist_id)))
  140. multipart_data = MultipartEncoder(fields)
  141. headers = {
  142. 'Content-Type': multipart_data.content_type
  143. }
  144. response = oauth.post(url + "/api/v1/videos/upload",
  145. data=multipart_data,
  146. headers=headers)
  147. if response is not None:
  148. if response.status_code == 200:
  149. jresponse = response.json()
  150. jresponse = jresponse['video']
  151. uuid = jresponse['uuid']
  152. idvideo = str(jresponse['id'])
  153. logging.info('Peertube : Video was successfully uploaded.')
  154. template = 'Peertube: Watch it at %s/videos/watch/%s.'
  155. logging.info(template % (url, uuid))
  156. if options.get('--publishAt'):
  157. utils.publishAt(str(options.get('--publishAt')), oauth, url, idvideo, secret)
  158. else:
  159. logging.error(('Peertube: The upload failed with an unexpected response: '
  160. '%s') % response)
  161. exit(1)
  162. def run(options):
  163. secret = RawConfigParser()
  164. try:
  165. secret.read(PEERTUBE_SECRETS_FILE)
  166. except Exception as e:
  167. logging.error("Peertube: Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e))
  168. exit(1)
  169. insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
  170. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
  171. oauth = get_authenticated_service(secret)
  172. try:
  173. logging.info('Peertube: Uploading video...')
  174. upload_video(oauth, secret, options)
  175. except Exception as e:
  176. if hasattr(e, 'message'):
  177. logging.error("Peertube: Error: " + str(e.message))
  178. else:
  179. logging.error("Peertube: Error: " + str(e))