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.

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