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.

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