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.

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