scripting your 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.

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