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.

327 lines
12 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 python
  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. from . import utils
  16. logger = logging.getLogger('Prismedia')
  17. PEERTUBE_SECRETS_FILE = 'peertube_secret'
  18. PEERTUBE_PRIVACY = {
  19. "public": 1,
  20. "unlisted": 2,
  21. "private": 3
  22. }
  23. def get_authenticated_service(secret):
  24. peertube_url = str(secret.get('peertube', 'peertube_url')).rstrip("/")
  25. oauth_client = LegacyApplicationClient(
  26. client_id=str(secret.get('peertube', 'client_id'))
  27. )
  28. try:
  29. oauth = OAuth2Session(client=oauth_client)
  30. oauth.fetch_token(
  31. token_url=str(peertube_url + '/api/v1/users/token'),
  32. # lower as peertube does not store uppercase for pseudo
  33. username=str(secret.get('peertube', 'username').lower()),
  34. password=str(secret.get('peertube', 'password')),
  35. client_id=str(secret.get('peertube', 'client_id')),
  36. client_secret=str(secret.get('peertube', 'client_secret'))
  37. )
  38. except Exception as e:
  39. if hasattr(e, 'message'):
  40. logger.critical("Peertube: " + str(e.message))
  41. exit(1)
  42. else:
  43. logger.critical("Peertube: " + str(e))
  44. exit(1)
  45. return oauth
  46. def get_default_channel(user_info):
  47. return user_info['videoChannels'][0]['id']
  48. def get_channel_by_name(user_info, options):
  49. for channel in user_info["videoChannels"]:
  50. if channel['displayName'] == options.get('--channel'):
  51. return channel['id']
  52. def create_channel(oauth, url, options):
  53. template = ('Peertube: Channel %s does not exist, creating it.')
  54. logger.info(template % (str(options.get('--channel'))))
  55. channel_name = utils.cleanString(str(options.get('--channel')))
  56. # Peertube allows 20 chars max for channel name
  57. channel_name = channel_name[:19]
  58. data = '{"name":"' + channel_name + '", \
  59. "displayName":"' + options.get('--channel') + '", \
  60. "description":null, \
  61. "support":null}'
  62. headers = {
  63. 'Content-Type': "application/json; charset=UTF-8"
  64. }
  65. try:
  66. response = oauth.post(url + "/api/v1/video-channels/",
  67. data=data.encode('utf-8'),
  68. headers=headers)
  69. except Exception as e:
  70. if hasattr(e, 'message'):
  71. logger.error("Peertube: " + str(e.message))
  72. else:
  73. logger.error("Peertube: " + str(e))
  74. if response is not None:
  75. if response.status_code == 200:
  76. jresponse = response.json()
  77. jresponse = jresponse['videoChannel']
  78. return jresponse['id']
  79. if response.status_code == 409:
  80. logger.critical('Peertube: It seems there is a conflict with an existing channel named '
  81. + channel_name + '.'
  82. ' Please beware Peertube internal name is compiled from 20 firsts characters of channel name.'
  83. ' Also note that channel name are not case sensitive (no uppercase nor accent)'
  84. ' Please check your channel name and retry.')
  85. exit(1)
  86. else:
  87. logger.critical(('Peertube: Creating channel failed with an unexpected response: '
  88. '%s') % response)
  89. exit(1)
  90. def get_default_playlist(user_info):
  91. return user_info['videoChannels'][0]['id']
  92. def get_playlist_by_name(user_playlists, options):
  93. for playlist in user_playlists["data"]:
  94. if playlist['displayName'] == options.get('--playlist'):
  95. return playlist['id']
  96. def create_playlist(oauth, url, options, channel):
  97. template = ('Peertube: Playlist %s does not exist, creating it.')
  98. logger.info(template % (str(options.get('--playlist'))))
  99. # We use files for form-data Content
  100. # see https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file
  101. # None is used to mute "filename" field
  102. files = {'displayName': (None, str(options.get('--playlist'))),
  103. 'privacy': (None, "1"),
  104. 'description': (None, "null"),
  105. 'videoChannelId': (None, str(channel)),
  106. 'thumbnailfile': (None, "null")}
  107. try:
  108. response = oauth.post(url + "/api/v1/video-playlists/",
  109. files=files)
  110. except Exception as e:
  111. if hasattr(e, 'message'):
  112. logger.error("Peertube: " + str(e.message))
  113. else:
  114. logger.error("Peertube: " + str(e))
  115. if response is not None:
  116. if response.status_code == 200:
  117. jresponse = response.json()
  118. jresponse = jresponse['videoPlaylist']
  119. return jresponse['id']
  120. else:
  121. logger.critical(('Peertube: Creating the playlist failed with an unexpected response: '
  122. '%s') % response)
  123. exit(1)
  124. def set_playlist(oauth, url, video_id, playlist_id):
  125. logger.info('Peertube: add video to playlist.')
  126. data = '{"videoId":"' + str(video_id) + '"}'
  127. headers = {
  128. 'Content-Type': "application/json"
  129. }
  130. try:
  131. response = oauth.post(url + "/api/v1/video-playlists/"+str(playlist_id)+"/videos",
  132. data=data,
  133. headers=headers)
  134. except Exception as e:
  135. if hasattr(e, 'message'):
  136. logger.error("Peertube: " + str(e.message))
  137. else:
  138. logger.error("Peertube: " + str(e))
  139. if response is not None:
  140. if response.status_code == 200:
  141. logger.info('Peertube: Video is successfully added to the playlist.')
  142. else:
  143. logger.critical(('Peertube: Configuring the playlist failed with an unexpected response: '
  144. '%s') % response)
  145. exit(1)
  146. def upload_video(oauth, secret, options):
  147. def get_userinfo():
  148. return json.loads(oauth.get(url+"/api/v1/users/me").content)
  149. def get_file(path):
  150. mimetypes.init()
  151. return (basename(path), open(abspath(path), 'rb'),
  152. mimetypes.types_map[splitext(path)[1]])
  153. def get_playlist(username):
  154. return json.loads(oauth.get(url+"/api/v1/accounts/"+username+"/video-playlists").content)
  155. path = options.get('--file')
  156. url = str(secret.get('peertube', 'peertube_url')).rstrip('/')
  157. user_info = get_userinfo()
  158. user_playlists = get_playlist(str(secret.get('peertube', 'username').lower()))
  159. # We need to transform fields into tuple to deal with tags as
  160. # MultipartEncoder does not support list refer
  161. # https://github.com/requests/toolbelt/issues/190 and
  162. # https://github.com/requests/toolbelt/issues/205
  163. fields = [
  164. ("name", options.get('--name') or splitext(basename(options.get('--file')))[0]),
  165. ("licence", "1"),
  166. ("description", options.get('--description') or "default description"),
  167. ("nsfw", str(int(options.get('--nsfw')) or "0")),
  168. ("videofile", get_file(path))
  169. ]
  170. if options.get('--tags'):
  171. tags = options.get('--tags').split(',')
  172. for strtag in tags:
  173. # Empty tag crashes Peertube, so skip them
  174. if strtag == "":
  175. continue
  176. # Tag more than 30 chars crashes Peertube, so exit and check tags
  177. if len(strtag) >= 30:
  178. logger.error("Peertube: Sorry, Peertube does not support tag with more than 30 characters, please reduce tag: " + strtag)
  179. logger.error("Peertube: Meanwhile, this tag will be skipped")
  180. continue
  181. fields.append(("tags[]", strtag))
  182. if options.get('--category'):
  183. fields.append(("category", str(utils.getCategory(options.get('--category'), 'peertube'))))
  184. else:
  185. # if no category, set default to 2 (Films)
  186. fields.append(("category", "2"))
  187. if options.get('--language'):
  188. fields.append(("language", str(utils.getLanguage(options.get('--language'), "peertube"))))
  189. else:
  190. # if no language, set default to 1 (English)
  191. fields.append(("language", "en"))
  192. if options.get('--disable-comments'):
  193. fields.append(("commentsEnabled", "0"))
  194. else:
  195. fields.append(("commentsEnabled", "1"))
  196. privacy = None
  197. if options.get('--privacy'):
  198. privacy = options.get('--privacy').lower()
  199. # If peertubeAt exists, use instead of publishAt
  200. if options.get('--peertubeAt'):
  201. publishAt = options.get('--peertubeAt')
  202. elif options.get('--publishAt'):
  203. publishAt = options.get('--publishAt')
  204. if 'publishAt' in locals():
  205. publishAt = datetime.datetime.strptime(publishAt, '%Y-%m-%dT%H:%M:%S')
  206. tz = get_localzone()
  207. tz = pytz.timezone(str(tz))
  208. publishAt = tz.localize(publishAt).isoformat()
  209. fields.append(("scheduleUpdate[updateAt]", publishAt))
  210. fields.append(("scheduleUpdate[privacy]", str(PEERTUBE_PRIVACY["public"])))
  211. fields.append(("privacy", str(PEERTUBE_PRIVACY["private"])))
  212. else:
  213. fields.append(("privacy", str(PEERTUBE_PRIVACY[privacy or "private"])))
  214. if options.get('--thumbnail'):
  215. fields.append(("thumbnailfile", get_file(options.get('--thumbnail'))))
  216. fields.append(("previewfile", get_file(options.get('--thumbnail'))))
  217. if options.get('--channel'):
  218. channel_id = get_channel_by_name(user_info, options)
  219. if not channel_id and options.get('--channelCreate'):
  220. channel_id = create_channel(oauth, url, options)
  221. elif not channel_id:
  222. logger.warning("Peertube: Channel `" + options.get('--channel') + "` is unknown, using default channel.")
  223. channel_id = get_default_channel(user_info)
  224. else:
  225. channel_id = get_default_channel(user_info)
  226. fields.append(("channelId", str(channel_id)))
  227. if options.get('--playlist'):
  228. playlist_id = get_playlist_by_name(user_playlists, options)
  229. if not playlist_id and options.get('--playlistCreate'):
  230. playlist_id = create_playlist(oauth, url, options, channel_id)
  231. elif not playlist_id:
  232. logger.critical("Peertube: Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate"
  233. " if you want to create it")
  234. exit(1)
  235. logger_stdout = None
  236. if options.get('--print-url'):
  237. logger_stdout = logging.getLogger('stdoutlogs')
  238. multipart_data = MultipartEncoder(fields)
  239. headers = {
  240. 'Content-Type': multipart_data.content_type
  241. }
  242. response = oauth.post(url + "/api/v1/videos/upload",
  243. data=multipart_data,
  244. headers=headers)
  245. if response is not None:
  246. if response.status_code == 200:
  247. jresponse = response.json()
  248. jresponse = jresponse['video']
  249. uuid = jresponse['uuid']
  250. video_id = str(jresponse['id'])
  251. logger.info('Peertube : Video was successfully uploaded.')
  252. template = 'Peertube: Watch it at %s/videos/watch/%s.'
  253. logger.info(template % (url, uuid))
  254. if logger_stdout:
  255. template_stdout = '%s/videos/watch/%s'
  256. logger_stdout.info(template_stdout % (url, uuid))
  257. # Upload is successful we may set playlist
  258. if options.get('--playlist'):
  259. set_playlist(oauth, url, video_id, playlist_id)
  260. else:
  261. logger.critical(('Peertube: The upload failed with an unexpected response: '
  262. '%s') % response)
  263. exit(1)
  264. def run(options):
  265. secret = RawConfigParser()
  266. try:
  267. secret.read(PEERTUBE_SECRETS_FILE)
  268. except Exception as e:
  269. logger.critical("Peertube: Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e))
  270. exit(1)
  271. insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
  272. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
  273. oauth = get_authenticated_service(secret)
  274. try:
  275. logger.info('Peertube: Uploading video...')
  276. upload_video(oauth, secret, options)
  277. except Exception as e:
  278. if hasattr(e, 'message'):
  279. logger.error("Peertube: " + str(e.message))
  280. else:
  281. logger.error("Peertube: " + str(e))