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.

397 lines
14 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
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
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. # From Youtube samples: https://raw.githubusercontent.com/youtube/api-samples/master/python/upload_video.py # noqa
  4. import http.client
  5. import httplib2
  6. import random
  7. import time
  8. import copy
  9. import json
  10. from os.path import splitext, basename, exists
  11. import os
  12. import google.oauth2.credentials
  13. import datetime
  14. import pytz
  15. import logging
  16. from tzlocal import get_localzone
  17. from googleapiclient.discovery import build
  18. from googleapiclient.errors import HttpError
  19. from googleapiclient.http import MediaFileUpload
  20. from google_auth_oauthlib.flow import InstalledAppFlow
  21. from . import utils
  22. logger = logging.getLogger('Prismedia')
  23. # Explicitly tell the underlying HTTP transport library not to retry, since
  24. # we are handling retry logic ourselves.
  25. httplib2.RETRIES = 1
  26. # Maximum number of times to retry before giving up.
  27. MAX_RETRIES = 10
  28. # Youtube retriables cases
  29. RETRIABLE_EXCEPTIONS = (
  30. IOError,
  31. httplib2.HttpLib2Error,
  32. http.client.NotConnected,
  33. http.client.IncompleteRead,
  34. http.client.ImproperConnectionState,
  35. http.client.CannotSendRequest,
  36. http.client.CannotSendHeader,
  37. http.client.ResponseNotReady,
  38. http.client.BadStatusLine,
  39. )
  40. RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
  41. CLIENT_SECRETS_FILE = 'youtube_secret.json'
  42. CREDENTIALS_PATH = ".youtube_credentials.json"
  43. SCOPES = ['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube.force-ssl']
  44. API_SERVICE_NAME = 'youtube'
  45. API_VERSION = 'v3'
  46. CATEGORY = {
  47. "music": 10,
  48. "films": 1,
  49. "vehicles": 2,
  50. "sport": 17,
  51. "travels": 19,
  52. "gaming": 20,
  53. "people": 22,
  54. "comedy": 23,
  55. "entertainment": 24,
  56. "news": 25,
  57. "how to": 26,
  58. "education": 27,
  59. "activism": 29,
  60. "science & technology": 28,
  61. "science": 28,
  62. "technology": 28,
  63. "animals": 15
  64. }
  65. LANGUAGE = {
  66. "arabic": 'ar',
  67. "english": 'en',
  68. "french": 'fr',
  69. "german": 'de',
  70. "hindi": 'hi',
  71. "italian": 'it',
  72. "japanese": 'ja',
  73. "korean": 'ko',
  74. "mandarin": 'zh-CN',
  75. "portuguese": 'pt-PT',
  76. "punjabi": 'pa',
  77. "russian": 'ru',
  78. "spanish": 'es'
  79. }
  80. # Authorize the request and store authorization credentials.
  81. def get_authenticated_service():
  82. check_authenticated_scopes()
  83. flow = InstalledAppFlow.from_client_secrets_file(
  84. CLIENT_SECRETS_FILE, SCOPES)
  85. if exists(CREDENTIALS_PATH):
  86. with open(CREDENTIALS_PATH, 'r') as f:
  87. credential_params = json.load(f)
  88. credentials = google.oauth2.credentials.Credentials(
  89. credential_params["token"],
  90. refresh_token=credential_params["_refresh_token"],
  91. token_uri=credential_params["_token_uri"],
  92. client_id=credential_params["_client_id"],
  93. client_secret=credential_params["_client_secret"]
  94. )
  95. else:
  96. credentials = flow.run_console()
  97. with open(CREDENTIALS_PATH, 'w') as f:
  98. p = copy.deepcopy(vars(credentials))
  99. del p["expiry"]
  100. json.dump(p, f)
  101. return build(API_SERVICE_NAME, API_VERSION, credentials=credentials, cache_discovery=False)
  102. def check_authenticated_scopes():
  103. if exists(CREDENTIALS_PATH):
  104. with open(CREDENTIALS_PATH, 'r') as f:
  105. credential_params = json.load(f)
  106. # Check if all scopes are present
  107. if credential_params["_scopes"] != SCOPES:
  108. logger.warning("Youtube: Credentials are obsolete, need to re-authenticate.")
  109. os.remove(CREDENTIALS_PATH)
  110. def convert_youtube_date(date):
  111. # Youtube needs microsecond and the local timezone from ISO 8601
  112. date = date + ".000001"
  113. date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f')
  114. tz = get_localzone()
  115. tz = pytz.timezone(str(tz))
  116. return tz.localize(date).isoformat()
  117. def initialize_upload(youtube, options):
  118. path = options.get('--file')
  119. tags = None
  120. if options.get('--tags'):
  121. tags = options.get('--tags').split(',')
  122. category = None
  123. if options.get('--category'):
  124. category = CATEGORY[options.get('--category').lower()]
  125. language = None
  126. if options.get('--language'):
  127. language = LANGUAGE[options.get('--language').lower()]
  128. license = None
  129. if options.get('--cca'):
  130. license = "creativeCommon"
  131. # We set recordingDetails empty because it's easier to add options if it already exists
  132. # and if empty, it does not cause problem during upload
  133. body = {
  134. "snippet": {
  135. "title": options.get('--name'),
  136. "description": options.get('--description') or "default description",
  137. "tags": tags,
  138. # if no category, set default to 1 (Films)
  139. "categoryId": str(category or 1),
  140. "defaultAudioLanguage": str(language or 'en')
  141. },
  142. "status": {
  143. "privacyStatus": str(options.get('--privacy') or "private"),
  144. "license": str(license or "youtube"),
  145. },
  146. "recordingDetails": {
  147. }
  148. }
  149. # If peertubeAt exists, use instead of publishAt
  150. if options.get('--youtubeAt'):
  151. publishAt = options.get('--youtubeAt')
  152. elif options.get('--publishAt'):
  153. publishAt = options.get('--publishAt')
  154. # Check if publishAt variable exists in local variables
  155. if 'publishAt' in locals():
  156. publishAt = convert_youtube_date(publishAt)
  157. body['status']['publishAt'] = str(publishAt)
  158. # Set originalDate except if the user force no originalDate
  159. if options.get('--originalDate'):
  160. originalDate = convert_youtube_date(options.get('--originalDate'))
  161. body['recordingDetails']['recordingDate'] = str(originalDate)
  162. if options.get('--playlist'):
  163. playlist_id = get_playlist_by_name(youtube, options.get('--playlist'))
  164. if not playlist_id and options.get('--playlistCreate'):
  165. playlist_id = create_playlist(youtube, options.get('--playlist'))
  166. elif not playlist_id:
  167. logger.warning("Youtube: Playlist `" + options.get('--playlist') + "` is unknown.")
  168. logger.warning("Youtube: If you want to create it, set the --playlistCreate option.")
  169. playlist_id = ""
  170. else:
  171. playlist_id = ""
  172. # Call the API's videos.insert method to create and upload the video.
  173. insert_request = youtube.videos().insert(
  174. part=','.join(list(body.keys())),
  175. body=body,
  176. media_body=MediaFileUpload(path, chunksize=-1, resumable=True)
  177. )
  178. video_id = resumable_upload(insert_request, 'video', 'insert', options)
  179. # If we get a video_id, upload is successful and we are able to set thumbnail
  180. if video_id and options.get('--thumbnail'):
  181. set_thumbnail(options, youtube, options.get('--thumbnail'), videoId=video_id)
  182. # If we get a video_id and a playlist_id, upload is successful and we are able to set playlist
  183. if video_id and playlist_id != "":
  184. set_playlist(youtube, playlist_id, video_id)
  185. def get_playlist_by_name(youtube, playlist_name):
  186. pageToken = ""
  187. while pageToken != None:
  188. response = youtube.playlists().list(
  189. part='snippet,id',
  190. mine=True,
  191. maxResults=50,
  192. pageToken=pageToken
  193. ).execute()
  194. for playlist in response["items"]:
  195. if playlist["snippet"]["title"] == playlist_name:
  196. return playlist["id"]
  197. # Ask next page if there are any
  198. if "nextPageToken" in response:
  199. pageToken = response["nextPageToken"]
  200. else:
  201. pageToken = None
  202. def create_playlist(youtube, playlist_name):
  203. template = 'Youtube: Playlist %s does not exist, creating it.'
  204. logger.info(template % (str(playlist_name)))
  205. resources = build_resource({'snippet.title': playlist_name,
  206. 'snippet.description': '',
  207. 'status.privacyStatus': 'public'})
  208. response = youtube.playlists().insert(
  209. body=resources,
  210. part='status,snippet,id'
  211. ).execute()
  212. return response["id"]
  213. def build_resource(properties):
  214. resource = {}
  215. for p in properties:
  216. # Given a key like "snippet.title", split into "snippet" and "title", where
  217. # "snippet" will be an object and "title" will be a property in that object.
  218. prop_array = p.split('.')
  219. ref = resource
  220. for pa in range(0, len(prop_array)):
  221. is_array = False
  222. key = prop_array[pa]
  223. # For properties that have array values, convert a name like
  224. # "snippet.tags[]" to snippet.tags, and set a flag to handle
  225. # the value as an array.
  226. if key[-2:] == '[]':
  227. key = key[0:len(key)-2:]
  228. is_array = True
  229. if pa == (len(prop_array) - 1):
  230. # Leave properties without values out of inserted resource.
  231. if properties[p]:
  232. if is_array:
  233. ref[key] = properties[p].split(',')
  234. else:
  235. ref[key] = properties[p]
  236. elif key not in ref:
  237. # For example, the property is "snippet.title", but the resource does
  238. # not yet have a "snippet" object. Create the snippet object here.
  239. # Setting "ref = ref[key]" means that in the next time through the
  240. # "for pa in range ..." loop, we will be setting a property in the
  241. # resource's "snippet" object.
  242. ref[key] = {}
  243. ref = ref[key]
  244. else:
  245. # For example, the property is "snippet.description", and the resource
  246. # already has a "snippet" object.
  247. ref = ref[key]
  248. return resource
  249. def set_thumbnail(options, youtube, media_file, **kwargs):
  250. kwargs = utils.remove_empty_kwargs(**kwargs)
  251. request = youtube.thumbnails().set(
  252. media_body=MediaFileUpload(media_file, chunksize=-1,
  253. resumable=True),
  254. **kwargs
  255. )
  256. return resumable_upload(request, 'thumbnail', 'set', options)
  257. def set_playlist(youtube, playlist_id, video_id):
  258. logger.info('Youtube: Configuring playlist...')
  259. resource = build_resource({'snippet.playlistId': playlist_id,
  260. 'snippet.resourceId.kind': 'youtube#video',
  261. 'snippet.resourceId.videoId': video_id,
  262. 'snippet.position': ''}
  263. )
  264. try:
  265. youtube.playlistItems().insert(
  266. body=resource,
  267. part='snippet'
  268. ).execute()
  269. except Exception as e:
  270. logger.critical("Youtube: " + utils.get_exception_string(e))
  271. exit(1)
  272. logger.info('Youtube: Video is correctly added to the playlist.')
  273. # This method implements an exponential backoff strategy to resume a
  274. # failed upload.
  275. def resumable_upload(request, resource, method, options):
  276. response = None
  277. error = None
  278. retry = 0
  279. logger_stdout = None
  280. if options.get('--url-only') or options.get('--batch'):
  281. logger_stdout = logging.getLogger('stdoutlogs')
  282. while response is None:
  283. try:
  284. template = 'Youtube: Uploading %s...'
  285. logger.info(template % resource)
  286. status, response = request.next_chunk()
  287. if response is not None:
  288. if method == 'insert' and 'id' in response:
  289. logger.info('Youtube: Video was successfully uploaded.')
  290. template = 'Youtube: Watch it at https://youtu.be/%s (post-encoding could take some time)'
  291. logger.info(template % response['id'])
  292. template_stdout = 'https://youtu.be/%s'
  293. if options.get('--url-only'):
  294. logger_stdout.info(template_stdout % response['id'])
  295. elif options.get('--batch'):
  296. logger_stdout.info("Youtube: " + template_stdout % response['id'])
  297. return response['id']
  298. elif method != 'insert' or "id" not in response:
  299. logger.info('Youtube: Thumbnail was successfully set.')
  300. else:
  301. template = ('Youtube: The upload failed with an '
  302. 'unexpected response: %s')
  303. logger.critical(template % response)
  304. exit(1)
  305. except HttpError as e:
  306. if e.resp.status in RETRIABLE_STATUS_CODES:
  307. template = 'Youtube: A retriable HTTP error %d occurred:\n%s'
  308. error = template % (e.resp.status, e.content)
  309. else:
  310. raise
  311. except RETRIABLE_EXCEPTIONS as e:
  312. error = 'Youtube: A retriable error occurred: %s' % e
  313. if error is not None:
  314. logger.warning(error)
  315. retry += 1
  316. if retry > MAX_RETRIES:
  317. logger.error('Youtube: No longer attempting to retry.')
  318. max_sleep = 2 ** retry
  319. sleep_seconds = random.random() * max_sleep
  320. logger.warning('Youtube: Sleeping %f seconds and then retrying...'
  321. % sleep_seconds)
  322. time.sleep(sleep_seconds)
  323. def heartbeat():
  324. """Use the minimums credits possibles of the API so google does not readuce to 0 the allowed credits.
  325. This apparently happens after 90 days without any usage of credits.
  326. For more info see the official documentations:
  327. - General informations about quotas: https://developers.google.com/youtube/v3/getting-started#quota
  328. - Quota costs for API requests: https://developers.google.com/youtube/v3/determine_quota_cost
  329. - ToS (Americas) #Usage and Quotas: https://developers.google.com/youtube/terms/api-services-terms-of-service#usage-and-quotas"""
  330. youtube = get_authenticated_service()
  331. try:
  332. get_playlist_by_name(youtube, "Foo")
  333. except HttpError as e:
  334. logger.error('Youtube: An HTTP error %d occurred on heartbeat:\n%s' %
  335. (e.resp.status, e.content))
  336. def run(options):
  337. youtube = get_authenticated_service()
  338. try:
  339. initialize_upload(youtube, options)
  340. except HttpError as e:
  341. logger.error('Youtube: An HTTP error %d occurred:\n%s' % (e.resp.status,
  342. e.content))