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.

235 lines
12 KiB

  1. #!/usr/bin/python
  2. # coding: utf-8
  3. # NOTE: Since we use config file to set some defaults values, it is not possible to use the standard syntax with brackets, we use parenthesis instead.
  4. # If we were to use them we would override configuration file values with default values of cli.
  5. # TODO: change `youtube-at` and `peertube-at` that are not easely expendable as options in my opinion
  6. # TODO: remove `--url-only` and `--batch`
  7. """
  8. prismedia - tool to upload videos to different platforms (historicaly Peertube and Youtube)
  9. Usage:
  10. prismedia [options] --file=<file> | [<interface> [<parameters>...]]
  11. prismedia --hearthbeat
  12. prismedia -h | --help | -V | --version
  13. Options:
  14. -f, --file=STRING Path to the video file to upload in mp4. This is the only mandatory option except if you provide the name of a plugin interface (see <interface>).
  15. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
  16. --name=NAME Name of the video to upload. (default to video filename)
  17. -d, --description=STRING Description of the video. (default: default description)
  18. -t, --tag=STRING Tags for the video. comma separated.
  19. WARN: tags with punctuation (!, ', ", ?, ...)
  20. are not supported by Mastodon to be published from Peertube
  21. -c, --category=STRING Category for the videos, see below. (default: Films)
  22. --licence=STRING Creative Common licence tag (for exemple: CC-BY-SA) (default: proprietary)
  23. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  24. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  25. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  26. --nfo=STRING Configure a specific nfo file to set options for the video.
  27. By default Prismedia search a .txt based on the video name and will
  28. decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded)
  29. See nfo_example.txt for more details
  30. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  31. --publish-at=DATE Publish the video at the given DATE using local server timezone.
  32. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  33. DATE should be in the future
  34. --peertube-at=DATE Override publish-at for the corresponding platform. Allow to create preview on specific platform
  35. --youtube-at=DATE Override publish-at for the corresponding platform. Allow to create preview on specific platform
  36. --original-date=DATE Configure the video as initially recorded at DATE
  37. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  38. DATE should be in the past
  39. --auto-original-date Automatically use the file modification time as original date
  40. Supported types are jpg and jpeg.
  41. By default, prismedia search for an image based on video name followed by .jpg or .jpeg
  42. --channel=STRING Set the channel to use for the video (Peertube only)
  43. If the channel is not found, spawn an error except if --channelCreate is set.
  44. --channel-create Create the channel if not exists. (Peertube only, default do not create)
  45. Only relevant if --channel is set.
  46. --playlist=STRING Set the playlist to use for the video.
  47. If the playlist is not found, spawn an error except if --playlistCreate is set.
  48. --playlist-create Create the playlist if not exists. (default do not create)
  49. Only relevant if --playlist is set.
  50. --progress=STRING Set the progress bar view, one of percentage, bigFile, accurate. [default: percentage]
  51. --hearthbeat Use some credits to show some activity for you apikey so the platform know it is used and would not inactivate your keys.
  52. -h, --help Show this help. Note that calling `help` without the `--` calls a plugin showing a different help for the plugins.
  53. -V, --version Show the version.
  54. Plugins options:
  55. <interface> Interface plugin to use to provide the video to upload. Select the interface you want to use. If `--file` is provided instead the interface will be the command line.
  56. --platform=STRING Platforms plugins to use. Usually one platform plugin upload to one platform website (comma separated list) (default: all)
  57. --consumer=STRING Consumers plugins to use. They are executed after an upload has been done (comma separated list) (default: all)
  58. Logging options:
  59. --log=STRING Log level, between debug, info, warning, error, critical. Ignored if --quiet is set (default to info)
  60. -q, --quiet Suppress any log except Critical (alias for --log=critical).
  61. -u, --url-only Display generated URL after upload directly on stdout, implies --quiet
  62. --batch Display generated URL after upload with platform information for easier parsing. Implies --quiet
  63. Be careful --batch and --url-only are mutually exclusives.
  64. Strict options:
  65. Strict options allow you to force some option to be present when uploading a video. It's useful to be sure you do not
  66. forget something when uploading a video, for example if you use multiples NFO. You may force the presence of description,
  67. tags, thumbnail, ...
  68. All strict option are optionals and are provided only to avoid errors when uploading :-)
  69. All strict options can be specified in NFO directly, the only strict option mandatory on cli is --withNFO
  70. All strict options are off by default
  71. --with-NFO Prevent the upload without a NFO, either specified via cli or found in the directory
  72. --with-thumbnail Prevent the upload without a thumbnail
  73. --with-name Prevent the upload if no name are found
  74. --with-description Prevent the upload without description
  75. --with-tag Prevent the upload without tags
  76. --with-playlist Prevent the upload if no playlist
  77. --with-publish-at Prevent the upload if no schedule
  78. --with-original-date Prevent the upload if no original date configured
  79. --with-platform Prevent the upload if at least one platform is not specified
  80. --with-category Prevent the upload if no category
  81. --with-language Prevent upload if no language
  82. --with-channel Prevent upload if no channel
  83. Categories:
  84. Category is the type of video you upload. Default is films.
  85. Here are available categories from Peertube and Youtube:
  86. music, films, vehicles, sports, travels, gaming, people,
  87. comedy, entertainment, news, how to, education, activism,
  88. science & technology, science, technology, animals
  89. Languages:
  90. Language of the video (audio track), choose one. Default is English
  91. Here are available languages from Peertube and Youtube:
  92. Arabic, English, French, German, Hindi, Italian, Japanese,
  93. Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  94. """
  95. import cli
  96. import pluginInterfaces as pi
  97. import utils
  98. import video as vid
  99. from docopt import docopt
  100. from yapsy.PluginManager import PluginManagerSingleton
  101. import os
  102. import logging
  103. # logging.basicConfig(level=logging.DEBUG)
  104. VERSION = "prismedia v1.0.0-plugins-alpha"
  105. def loadPlugins(basePluginsPath):
  106. pluginManager = PluginManagerSingleton.get()
  107. pluginManager.setPluginPlaces(basePluginsPath)
  108. # Define the various categories corresponding to the different
  109. # kinds of plugins you have defined
  110. pluginManager.setCategoriesFilter({
  111. pi.PluginTypes.ALL : pi.IPrismediaBasePlugin,
  112. pi.PluginTypes.INTERFACE : pi.IInterfacePlugin,
  113. pi.PluginTypes.PLATFORM : pi.IPlatformPlugin,
  114. pi.PluginTypes.CONSUMER : pi.IConsumerPlugin,
  115. })
  116. pluginManager.collectPlugins()
  117. # TODO: cut this function into smaller ones
  118. def main():
  119. logger = logging.getLogger('Prismedia')
  120. basePluginsPath = [os.path.dirname(os.path.abspath(__file__)) + "/plugins"]
  121. loadPlugins(basePluginsPath)
  122. pluginManager = PluginManagerSingleton.get()
  123. # TODO: add the arguments’s verification (copy/adapt the Schema table)
  124. options = docopt(__doc__, version=VERSION)
  125. # Helper functionnalities help the user but do not upload anything
  126. if not cli.helperFunctionnality(options):
  127. exit(os.EX_OK)
  128. video = cli.parseOptions(options)
  129. if options["<interface>"]:
  130. interface = pluginManager.getPluginByName(options["<interface>"], pi.PluginTypes.INTERFACE)
  131. try:
  132. if not interface.plugin_object.prepare_options(video, options):
  133. # The plugin asked to stop execution.
  134. exit(os.EX_OK)
  135. except Exception as e:
  136. logger.critical(utils.get_exception_string(e))
  137. exit(os.EX_CONFIG)
  138. list = utils.getOption(options, "--platform", [])
  139. if list:
  140. platforms = pluginManager.getPluginsOf(categories=pi.PluginTypes.PLATFORM, name=[list.split(",")])
  141. else:
  142. platforms = pluginManager.getPluginsOfCategory(pi.PluginTypes.PLATFORM)
  143. list = utils.getOption(options, "--consumer", None)
  144. if list:
  145. consumers = pluginManager.getPluginsOf(categories=pi.PluginTypes.CONSUMER, name=[list.split(",")])
  146. else:
  147. consumers = pluginManager.getPluginsOfCategory(pi.PluginTypes.CONSUMER)
  148. # Let each plugin check its options before starting any upload
  149. # We cannot merge this loop with the one from interface since the interface can change which plugin to use
  150. # We need to create each platform object in video, so we cannot merge this loop with the following one
  151. for plugin in platforms:
  152. # TODO: Check this is needed or not: in case of no plugin or wrong name maybe the list is empty instead of there being a None value
  153. if plugin is None:
  154. # TODO: log instead to error ? critical ?
  155. print("No plugin installed name `" + plugin.name + "`.")
  156. exit(os.EX_USAGE)
  157. try:
  158. video.platform[plugin.name] = vid.Platform()
  159. if not plugin.plugin_object.prepare_options(video, options):
  160. # A plugin found ill formed options, it should have logged the precises infos
  161. print(plugin.name + " found a malformed option.")
  162. exit(os.EX_CONFIG)
  163. except Exception as e:
  164. logger.critical(utils.get_exception_string(e))
  165. exit(os.EX_CONFIG)
  166. for plugin in consumers:
  167. # TODO: Check this is needed or not: in case of no plugin or wrong name maybe the list is empty instead of there being a None value
  168. if plugin is None:
  169. # TODO: log instead to error ? critical ?
  170. print("No plugin installed name `" + plugin.name + "`.")
  171. exit(os.EX_USAGE)
  172. try:
  173. if not plugin.plugin_object.prepare_options(video, options):
  174. # A plugin found ill formed options, it should have logged the precises infos
  175. print(plugin.name + " found a malformed option.")
  176. exit(os.EX_CONFIG)
  177. except Exception as e:
  178. logger.critical(utils.get_exception_string(e))
  179. exit(os.EX_CONFIG)
  180. if video.path == "":
  181. # TODO: log instead to error ? critical ?
  182. print("No valid path to a video file has been provided.")
  183. exit(os.EX_USAGE)
  184. print("All options validated, starting uploads onto platforms")
  185. for platform in platforms:
  186. print("Uploading to: " + platform.name)
  187. try:
  188. platform.plugin_object.upload(video, options)
  189. except Exception as e: # TODO: Maybe not catch every Exception?
  190. logger.critical(utils.get_exception_string(e))
  191. video.platform[platform.name].error = e
  192. video.platform[platform.name].publishAt = None
  193. video.platform[platform.name].url = None
  194. print("All uploads have been done, calling consumers plugins")
  195. for consumer in consumers:
  196. print("Calling consumer: " + consumer.name)
  197. consumer.plugin_object.finished(video, options)
  198. main()