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.

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