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.

422 lines
18 KiB

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. """
  4. prismedia - tool to upload videos to Peertube and Youtube
  5. Usage:
  6. prismedia --file=<FILE> [options]
  7. prismedia -f <FILE> --tags=STRING [options]
  8. prismedia --hearthbeat
  9. prismedia -h | --help
  10. prismedia --version
  11. Options:
  12. -f, --file=STRING Path to the video file to upload. This is the only mandatory option.
  13. --name=NAME Name of the video to upload. (default to video filename)
  14. -d, --description=STRING Description of the video. (default: default description)
  15. -t, --tags=STRING Tags for the video. comma separated.
  16. WARN: tags with punctuation (!, ', ", ?, ...)
  17. are not supported by Mastodon to be published from Peertube
  18. -c, --category=STRING Category for the videos, see below. (default: Films)
  19. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  20. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  21. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  22. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  23. --nfo=STRING Configure a specific nfo file to set options for the video.
  24. By default Prismedia search a .txt based on the video name and will
  25. decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded)
  26. See nfo_example.txt for more details
  27. --platform=STRING List of platform(s) to upload to, comma separated.
  28. Supported platforms are youtube and peertube (default is both)
  29. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  30. --publishAt=DATE Publish the video at the given DATE using local server timezone.
  31. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  32. DATE should be in the future
  33. --peertubeAt=DATE
  34. --youtubeAt=DATE Override publishAt for the corresponding platform. Allow to create preview on specific platform
  35. --originalDate=DATE Configure the video as initially recorded at DATE
  36. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  37. DATE should be in the past
  38. --auto-originalDate Automatically use the file modification time as original date
  39. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
  40. By default, prismedia search for an image based on video name followed by .jpg, .jpeg or .png
  41. --channel=STRING Set the channel to use for the video (Peertube only)
  42. If the channel is not found, spawn an error except if --channelCreate is set.
  43. --channelCreate Create the channel if not exists. (Peertube only, default do not create)
  44. Only relevant if --channel is set.
  45. --playlist=STRING Set the playlist to use for the video.
  46. If the playlist is not found, spawn an error except if --playlistCreate is set.
  47. --playlistCreate Create the playlist if not exists. (default do not create)
  48. Only relevant if --playlist is set.
  49. --progress=STRING Set the progress bar view, one of percentage, bigFile (MB), accurate (KB).
  50. --hearthbeat Use some credits to show some activity for you apikey so the platform know it is used and would not put your quota to 0 (only Youtube currently)
  51. -h --help Show this help.
  52. --version Show version.
  53. Logging options
  54. -q --quiet Suppress any log except Critical (alias for --log=critical).
  55. --log=STRING Log level, between debug, info, warning, error, critical. Ignored if --quiet is set (default to info)
  56. -u --url-only Display generated URL after upload directly on stdout, implies --quiet
  57. --batch Display generated URL after upload with platform information for easier parsing. Implies --quiet
  58. Be careful --batch and --url-only are mutually exclusives.
  59. Strict options:
  60. Strict options allow you to force some option to be present when uploading a video. It's useful to be sure you do not
  61. forget something when uploading a video, for example if you use multiples NFO. You may force the presence of description,
  62. tags, thumbnail, ...
  63. All strict option are optionals and are provided only to avoid errors when uploading :-)
  64. All strict options can be specified in NFO directly, the only strict option mandatory on cli is --withNFO
  65. All strict options are off by default
  66. --withNFO Prevent the upload without a NFO, either specified via cli or found in the directory
  67. --withThumbnail Prevent the upload without a thumbnail
  68. --withName Prevent the upload if no name are found
  69. --withDescription Prevent the upload without description
  70. --withTags Prevent the upload without tags
  71. --withPlaylist Prevent the upload if no playlist
  72. --withPublishAt Prevent the upload if no schedule
  73. --withOriginalDate Prevent the upload if no original date configured
  74. --withPlatform Prevent the upload if at least one platform is not specified
  75. --withCategory Prevent the upload if no category
  76. --withLanguage Prevent upload if no language
  77. --withChannel Prevent upload if no channel
  78. Categories:
  79. Category is the type of video you upload. Default is films.
  80. Here are available categories from Peertube and Youtube:
  81. music, films, vehicles,
  82. sports, travels, gaming, people,
  83. comedy, entertainment, news,
  84. how to, education, activism, science & technology,
  85. science, technology, animals
  86. Languages:
  87. Language of the video (audio track), choose one. Default is English
  88. Here are available languages from Peertube and Youtube:
  89. Arabic, English, French, German, Hindi, Italian,
  90. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  91. """
  92. import sys
  93. if sys.version_info[0] < 3:
  94. raise Exception("Python 3 or a more recent version is required.")
  95. import os
  96. import datetime
  97. import logging
  98. logger = logging.getLogger('Prismedia')
  99. from docopt import docopt
  100. from . import yt_upload
  101. from . import pt_upload
  102. from . import utils
  103. try:
  104. # noinspection PyUnresolvedReferences
  105. from schema import Schema, And, Or, Optional, SchemaError, Hook, Use
  106. except ImportError:
  107. logger.critical('This program requires that the `schema` data-validation library'
  108. ' is installed: \n'
  109. 'see https://github.com/halst/schema\n')
  110. exit(1)
  111. VERSION = "prismedia v0.12.1"
  112. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  113. VALID_CATEGORIES = (
  114. "music", "films", "vehicles",
  115. "sports", "travels", "gaming", "people",
  116. "comedy", "entertainment", "news",
  117. "how to", "education", "activism", "science & technology",
  118. "science", "technology", "animals"
  119. )
  120. VALID_PLATFORM = ('youtube', 'peertube', 'none')
  121. VALID_LANGUAGES = ('arabic', 'english', 'french',
  122. 'german', 'hindi', 'italian',
  123. 'japanese', 'korean', 'mandarin',
  124. 'portuguese', 'punjabi', 'russian', 'spanish')
  125. VALID_PROGRESS = ('percentage', 'bigfile', 'accurate')
  126. def validateCategory(category):
  127. if category.lower() in VALID_CATEGORIES:
  128. return True
  129. else:
  130. return False
  131. def validatePrivacy(privacy):
  132. if privacy.lower() in VALID_PRIVACY_STATUSES:
  133. return True
  134. else:
  135. return False
  136. def validatePlatform(platform):
  137. for plfrm in platform.split(','):
  138. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  139. return False
  140. return True
  141. def validateLanguage(language):
  142. if language.lower() in VALID_LANGUAGES:
  143. return True
  144. else:
  145. return False
  146. def validatePublishDate(publishDate):
  147. # Check date format and if date is future
  148. try:
  149. now = datetime.datetime.now()
  150. publishAt = datetime.datetime.strptime(publishDate, '%Y-%m-%dT%H:%M:%S')
  151. if now >= publishAt:
  152. return False
  153. except ValueError:
  154. return False
  155. return True
  156. def validateOriginalDate(originalDate):
  157. # Check date format and if date is past
  158. try:
  159. now = datetime.datetime.now()
  160. originalDate = datetime.datetime.strptime(originalDate, '%Y-%m-%dT%H:%M:%S')
  161. if now <= originalDate:
  162. return False
  163. except ValueError:
  164. return False
  165. return True
  166. def validateLogLevel(loglevel):
  167. numeric_level = getattr(logging, loglevel, None)
  168. if not isinstance(numeric_level, int):
  169. return False
  170. return True
  171. def validateProgress(progress):
  172. for prgs in progress.split(','):
  173. if prgs.lower().replace(" ", "") not in VALID_PROGRESS:
  174. return False
  175. return True
  176. def _optionnalOrStrict(key, scope, error):
  177. option = key.replace('-', '')
  178. option = option[0].upper() + option[1:]
  179. if scope["--with" + option] is True and scope[key] is None:
  180. logger.critical("Prismedia: you have required the strict presence of " + key + " but none is found")
  181. exit(1)
  182. return True
  183. def configureLogs(options):
  184. if options.get('--batch') and options.get('--url-only'):
  185. logger.critical("Prismedia: Please use either --batch OR --url-only, not both.")
  186. exit(1)
  187. # batch and url-only implies quiet
  188. if options.get('--batch') or options.get('--url-only'):
  189. options['--quiet'] = True
  190. for handler in logger.handlers or logger.parent.handlers:
  191. if options.get('--quiet'):
  192. # We need to set both log level in the same time
  193. logger.setLevel(50)
  194. handler.setLevel(50)
  195. elif options.get('--log'):
  196. numeric_level = getattr(logging, options["--log"], None)
  197. # We need to set both log level in the same time
  198. logger.setLevel(numeric_level)
  199. handler.setLevel(numeric_level)
  200. elif options.get('--debug'):
  201. # Deprecated,
  202. logger.setLevel(10)
  203. handler.setLevel(10)
  204. def configureStdoutLogs():
  205. logger_stdout = logging.getLogger('stdoutlogs')
  206. logger_stdout.setLevel(logging.INFO)
  207. ch_stdout = logging.StreamHandler(stream=sys.stdout)
  208. ch_stdout.setLevel(logging.INFO)
  209. # Default stdout logs is url only
  210. formatter_stdout = logging.Formatter('%(message)s')
  211. ch_stdout.setFormatter(formatter_stdout)
  212. logger_stdout.addHandler(ch_stdout)
  213. def main():
  214. options = docopt(__doc__, version=VERSION)
  215. earlyoptionSchema = Schema({
  216. Optional('--log'): Or(None, And(
  217. str,
  218. Use(str.upper),
  219. validateLogLevel,
  220. error="Log level not recognized")
  221. ),
  222. Optional('--quiet', default=False): bool,
  223. Optional('--debug'): bool,
  224. Optional('--url-only', default=False): bool,
  225. Optional('--batch', default=False): bool,
  226. Optional('--withNFO', default=False): bool,
  227. Optional('--withThumbnail', default=False): bool,
  228. Optional('--withName', default=False): bool,
  229. Optional('--withDescription', default=False): bool,
  230. Optional('--withTags', default=False): bool,
  231. Optional('--withPlaylist', default=False): bool,
  232. Optional('--withPublishAt', default=False): bool,
  233. Optional('--withOriginalDate', default=False): bool,
  234. Optional('--withPlatform', default=False): bool,
  235. Optional('--withCategory', default=False): bool,
  236. Optional('--withLanguage', default=False): bool,
  237. Optional('--withChannel', default=False): bool,
  238. # This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys
  239. object: object
  240. })
  241. schema = Schema({
  242. '--file': And(str, os.path.exists, error='file does not exists, please check path'),
  243. # Strict option checks - at the moment Schema needs to check Hook and Optional separately #
  244. Hook('--name', handler=_optionnalOrStrict): object,
  245. Hook('--description', handler=_optionnalOrStrict): object,
  246. Hook('--tags', handler=_optionnalOrStrict): object,
  247. Hook('--category', handler=_optionnalOrStrict): object,
  248. Hook('--language', handler=_optionnalOrStrict): object,
  249. Hook('--platform', handler=_optionnalOrStrict): object,
  250. Hook('--publishAt', handler=_optionnalOrStrict): object,
  251. Hook('--originalDate', handler=_optionnalOrStrict): object,
  252. Hook('--thumbnail', handler=_optionnalOrStrict): object,
  253. Hook('--channel', handler=_optionnalOrStrict): object,
  254. Hook('--playlist', handler=_optionnalOrStrict): object,
  255. # Validate checks #
  256. Optional('--name'): Or(None, And(
  257. str,
  258. lambda x: not x.isdigit(),
  259. error="The video name should be a string")
  260. ),
  261. Optional('--description'): Or(None, And(
  262. str,
  263. lambda x: not x.isdigit(),
  264. error="The video description should be a string")
  265. ),
  266. Optional('--tags'): Or(None, And(
  267. str,
  268. lambda x: not x.isdigit(),
  269. error="Tags should be a string")
  270. ),
  271. Optional('--category'): Or(None, And(
  272. str,
  273. validateCategory,
  274. error="Category not recognized, please see --help")
  275. ),
  276. Optional('--language'): Or(None, And(
  277. str,
  278. validateLanguage,
  279. error="Language not recognized, please see --help")
  280. ),
  281. Optional('--privacy'): Or(None, And(
  282. str,
  283. validatePrivacy,
  284. error="Please use recognized privacy between public, unlisted or private")
  285. ),
  286. Optional('--nfo'): Or(None, str),
  287. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  288. Optional('--publishAt'): Or(None, And(
  289. str,
  290. validatePublishDate,
  291. error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  292. ),
  293. Optional('--peertubeAt'): Or(None, And(
  294. str,
  295. validatePublishDate,
  296. error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  297. ),
  298. Optional('--youtubeAt'): Or(None, And(
  299. str,
  300. validatePublishDate,
  301. error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  302. ),
  303. Optional('--originalDate'): Or(None, And(
  304. str,
  305. validateOriginalDate,
  306. error="Original date should be the form YYYY-MM-DDThh:mm:ss and has to be in the past")
  307. ),
  308. Optional('--auto-originalDate'): bool,
  309. Optional('--cca'): bool,
  310. Optional('--disable-comments'): bool,
  311. Optional('--nsfw'): bool,
  312. Optional('--thumbnail'): Or(None, And(
  313. str, os.path.exists, error='Thumbnail does not exists, please check the path.'),
  314. ),
  315. Optional('--channel'): Or(None, str),
  316. Optional('--channelCreate'): bool,
  317. Optional('--playlist'): Or(None, str),
  318. Optional('--playlistCreate'): bool,
  319. Optional('--progress'): Or(None, And(str, validateProgress, error="Sorry, progress visualisation not supported")),
  320. '--hearthbeat': bool,
  321. '--help': bool,
  322. '--version': bool,
  323. # This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys
  324. object: object
  325. })
  326. if options.get('--hearthbeat'):
  327. yt_upload.hearthbeat()
  328. exit(0)
  329. # We need to validate early options first as withNFO and logs options should be prioritized
  330. try:
  331. options = earlyoptionSchema.validate(options)
  332. configureLogs(options)
  333. except SchemaError as e:
  334. logger.critical(e)
  335. exit(1)
  336. if options.get('--url-only') or options.get('--batch'):
  337. configureStdoutLogs()
  338. options = utils.parseNFO(options)
  339. # If after loading NFO we still has no original date and --auto-originalDate is enabled,
  340. # then we need to search from the file
  341. # We need to do that before the strict validation in case --withOriginalDate is enabled
  342. if not options.get('--originalDate') and options.get('--auto-originalDate'):
  343. options['--originalDate'] = utils.searchOriginalDate(options)
  344. # Once NFO are loaded, we need to revalidate strict options in case some were in NFO
  345. try:
  346. options = earlyoptionSchema.validate(options)
  347. except SchemaError as e:
  348. logger.critical(e)
  349. exit(1)
  350. if not options.get('--thumbnail'):
  351. options = utils.searchThumbnail(options)
  352. try:
  353. options = schema.validate(options)
  354. except SchemaError as e:
  355. logger.critical(e)
  356. exit(1)
  357. logger.debug("Python " + sys.version)
  358. logger.debug(options)
  359. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  360. pt_upload.run(options)
  361. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  362. yt_upload.run(options)
  363. if __name__ == '__main__':
  364. logger.warning("DEPRECATION: use 'python -m prismedia', not 'python -m prismedia.upload'")
  365. main()