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.

310 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 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_channel_by_name(user_info, options):
  48. for channel in user_info["videoChannels"]:
  49. if channel['displayName'].encode('utf8') == str(options.get('--channel')):
  50. return channel['id']
  51. def create_channel(oauth, url, options):
  52. template = ('Peertube: Channel %s does not exist, creating it.')
  53. logging.info(template % (str(options.get('--channel'))))
  54. channel_name = utils.cleanString(str(options.get('--channel')))
  55. # Peertube allows 20 chars max for channel name
  56. channel_name = channel_name[:19]
  57. data = '{"name":"' + channel_name +'", \
  58. "displayName":"' + str(options.get('--channel')) +'", \
  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 channel, please beware '
  79. 'Peertube internal name is compiled from 20 firsts characters of channel name.'
  80. ' Please check your channel name and retry.')
  81. exit(1)
  82. else:
  83. logging.error(('Peertube: Creating channel failed with an unexpected response: '
  84. '%s') % response)
  85. exit(1)
  86. def get_default_playlist(user_info):
  87. return user_info['videoChannels'][0]['id']
  88. def get_playlist_by_name(user_playlists, options):
  89. for playlist in user_playlists["data"]:
  90. if playlist['displayName'].encode('utf8') == str(options.get('--playlist')):
  91. return playlist['id']
  92. def create_playlist(oauth, url, options, channel):
  93. template = ('Peertube: Playlist %s does not exist, creating it.')
  94. logging.info(template % (str(options.get('--playlist'))))
  95. # We use files for form-data Content
  96. # see https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file
  97. # None is used to mute "filename" field
  98. files = {'displayName': (None, str(options.get('--playlist'))),
  99. 'privacy': (None, "1"),
  100. 'description': (None, "null"),
  101. 'videoChannelId': (None, str(channel)),
  102. 'thumbnailfile': (None, "null")}
  103. try:
  104. response = oauth.post(url + "/api/v1/video-playlists/",
  105. files=files)
  106. except Exception as e:
  107. if hasattr(e, 'message'):
  108. logging.error("Error: " + str(e.message))
  109. else:
  110. logging.error("Error: " + str(e))
  111. if response is not None:
  112. if response.status_code == 200:
  113. jresponse = response.json()
  114. jresponse = jresponse['videoPlaylist']
  115. return jresponse['id']
  116. else:
  117. logging.error(('Peertube: Creating the playlist failed with an unexpected response: '
  118. '%s') % response)
  119. exit(1)
  120. def set_playlist(oauth, url, video_id, playlist_id):
  121. logging.info('Peertube: add video to playlist.')
  122. data = '{"videoId":"' + str(video_id) + '"}'
  123. headers = {
  124. 'Content-Type': "application/json"
  125. }
  126. try:
  127. response = oauth.post(url + "/api/v1/video-playlists/"+str(playlist_id)+"/videos",
  128. data=data,
  129. headers=headers)
  130. except Exception as e:
  131. if hasattr(e, 'message'):
  132. logging.error("Error: " + str(e.message))
  133. else:
  134. logging.error("Error: " + str(e))
  135. if response is not None:
  136. if response.status_code == 200:
  137. logging.info('Peertube: Video is successfully added to the playlist.')
  138. else:
  139. logging.error(('Peertube: Configuring the playlist failed with an unexpected response: '
  140. '%s') % response)
  141. exit(1)
  142. def upload_video(oauth, secret, options):
  143. def get_userinfo():
  144. return json.loads(oauth.get(url+"/api/v1/users/me").content)
  145. def get_file(path):
  146. mimetypes.init()
  147. return (basename(path), open(abspath(path), 'rb'),
  148. mimetypes.types_map[splitext(path)[1]])
  149. def get_playlist(username):
  150. return json.loads(oauth.get(url+"/api/v1/accounts/"+username+"/video-playlists").content)
  151. path = options.get('--file')
  152. url = str(secret.get('peertube', 'peertube_url')).rstrip('/')
  153. user_info = get_userinfo()
  154. user_playlists = get_playlist(str(secret.get('peertube', 'username').lower()))
  155. # We need to transform fields into tuple to deal with tags as
  156. # MultipartEncoder does not support list refer
  157. # https://github.com/requests/toolbelt/issues/190 and
  158. # https://github.com/requests/toolbelt/issues/205
  159. fields = [
  160. ("name", options.get('--name') or splitext(basename(options.get('--file')))[0]),
  161. ("licence", "1"),
  162. ("description", options.get('--description') or "default description"),
  163. ("nsfw", str(int(options.get('--nsfw')) or "0")),
  164. ("videofile", get_file(path))
  165. ]
  166. if options.get('--tags'):
  167. tags = options.get('--tags').split(',')
  168. for strtag in tags:
  169. # Empty tag crashes Peertube, so skip them
  170. if strtag == "":
  171. continue
  172. # Tag more than 30 chars crashes Peertube, so exit and check tags
  173. if len(strtag) >= 30:
  174. logging.warning("Peertube: Sorry, Peertube does not support tag with more than 30 characters, please reduce tag: " + strtag)
  175. exit(1)
  176. fields.append(("tags[]", strtag))
  177. if options.get('--category'):
  178. fields.append(("category", str(utils.getCategory(options.get('--category'), 'peertube'))))
  179. else:
  180. # if no category, set default to 2 (Films)
  181. fields.append(("category", "2"))
  182. if options.get('--language'):
  183. fields.append(("language", str(utils.getLanguage(options.get('--language'), "peertube"))))
  184. else:
  185. # if no language, set default to 1 (English)
  186. fields.append(("language", "en"))
  187. if options.get('--disable-comments'):
  188. fields.append(("commentsEnabled", "0"))
  189. else:
  190. fields.append(("commentsEnabled", "1"))
  191. privacy = None
  192. if options.get('--privacy'):
  193. privacy = options.get('--privacy').lower()
  194. if options.get('--publishAt'):
  195. publishAt = options.get('--publishAt')
  196. publishAt = datetime.datetime.strptime(publishAt, '%Y-%m-%dT%H:%M:%S')
  197. tz = get_localzone()
  198. tz = pytz.timezone(str(tz))
  199. publishAt = tz.localize(publishAt).isoformat()
  200. fields.append(("scheduleUpdate[updateAt]", publishAt))
  201. fields.append(("scheduleUpdate[privacy]", str(PEERTUBE_PRIVACY["public"])))
  202. fields.append(("privacy", str(PEERTUBE_PRIVACY["private"])))
  203. else:
  204. fields.append(("privacy", str(PEERTUBE_PRIVACY[privacy or "private"])))
  205. if options.get('--thumbnail'):
  206. fields.append(("thumbnailfile", get_file(options.get('--thumbnail'))))
  207. fields.append(("previewfile", get_file(options.get('--thumbnail'))))
  208. if options.get('--channel'):
  209. channel_id = get_channel_by_name(user_info, options)
  210. if not channel_id and options.get('--channelCreate'):
  211. channel_id = create_channel(oauth, url, options)
  212. elif not channel_id:
  213. logging.warning("Channel `" + options.get('--channel') + "` is unknown, using default channel.")
  214. channel_id = get_default_channel(user_info)
  215. else:
  216. channel_id = get_default_channel(user_info)
  217. fields.append(("channelId", str(channel_id)))
  218. if options.get('--playlist'):
  219. playlist_id = get_playlist_by_name(user_playlists, options)
  220. if not playlist_id and options.get('--playlistCreate'):
  221. playlist_id = create_playlist(oauth, url, options, channel_id)
  222. elif not playlist_id:
  223. logging.warning("Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate"
  224. " if you want to create it")
  225. exit(1)
  226. multipart_data = MultipartEncoder(fields)
  227. headers = {
  228. 'Content-Type': multipart_data.content_type
  229. }
  230. response = oauth.post(url + "/api/v1/videos/upload",
  231. data=multipart_data,
  232. headers=headers)
  233. if response is not None:
  234. if response.status_code == 200:
  235. jresponse = response.json()
  236. jresponse = jresponse['video']
  237. uuid = jresponse['uuid']
  238. video_id = str(jresponse['id'])
  239. logging.info('Peertube : Video was successfully uploaded.')
  240. template = 'Peertube: Watch it at %s/videos/watch/%s.'
  241. logging.info(template % (url, uuid))
  242. # Upload is successful we may set playlist
  243. if options.get('--playlist'):
  244. set_playlist(oauth, url, video_id, playlist_id)
  245. else:
  246. logging.error(('Peertube: The upload failed with an unexpected response: '
  247. '%s') % response)
  248. exit(1)
  249. def run(options):
  250. secret = RawConfigParser()
  251. try:
  252. secret.read(PEERTUBE_SECRETS_FILE)
  253. except Exception as e:
  254. logging.error("Peertube: Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e))
  255. exit(1)
  256. insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
  257. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
  258. oauth = get_authenticated_service(secret)
  259. try:
  260. logging.info('Peertube: Uploading video...')
  261. upload_video(oauth, secret, options)
  262. except Exception as e:
  263. if hasattr(e, 'message'):
  264. logging.error("Peertube: Error: " + str(e.message))
  265. else:
  266. logging.error("Peertube: Error: " + str(e))