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.

222 lines
8.5 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
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. """
  4. prismedia_upload - tool to upload videos to Peertube and Youtube
  5. Usage:
  6. prismedia_upload.py --file=<FILE> [options]
  7. prismedia_upload.py --file=<FILE> --tags=STRING [--mt options]
  8. prismedia_upload.py -h | --help
  9. prismedia_upload.py --version
  10. Options:
  11. --name=NAME Name of the video to upload. (default to video filename)
  12. -d, --description=STRING Description of the video. (default: default description)
  13. -t, --tags=STRING Tags for the video. comma separated.
  14. WARN: tags with space and special characters (!, ', ", ?, ...)
  15. are not supported by Mastodon to be published from Peertube
  16. use mastodon compatibility below
  17. --mt Force Mastodon compatibility for tags (drop every incompatible characters inside tags)
  18. This option requires --tags
  19. -c, --category=STRING Category for the videos, see below. (default: Films)
  20. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  21. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  22. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  23. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  24. --nfo=STRING Configure a specific nfo file to set options for the video.
  25. By default Prismedia search a .txt based on video name
  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. For Peertube, requires the "atd" and "curl utilities installed on the system
  34. --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube.
  35. If the playlist is not found, spawn an error except if --playlist-create is set.
  36. --playlistCreate Create the playlist if not exists. (default do not create)
  37. Only relevant if --playlist is set.
  38. -h --help Show this help.
  39. --version Show version.
  40. Categories:
  41. Category is the type of video you upload. Default is films.
  42. Here are available categories from Peertube and Youtube:
  43. music, films, vehicles,
  44. sports, travels, gaming, people,
  45. comedy, entertainment, news,
  46. how to, education, activism, science & technology,
  47. science, technology, animals
  48. Languages:
  49. Language of the video (audio track), choose one. Default is English
  50. Here are available languages from Peertube and Youtube:
  51. Arabic, English, French, German, Hindi, Italian,
  52. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  53. """
  54. from os.path import dirname, realpath
  55. import sys
  56. import datetime
  57. import logging
  58. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  59. from docopt import docopt
  60. # Allows a relative import from the parent folder
  61. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  62. import yt_upload
  63. import pt_upload
  64. import utils
  65. try:
  66. # noinspection PyUnresolvedReferences
  67. from schema import Schema, And, Or, Optional, SchemaError
  68. except ImportError:
  69. logging.error('This program requires that the `schema` data-validation library'
  70. ' is installed: \n'
  71. 'see https://github.com/halst/schema\n')
  72. exit(1)
  73. try:
  74. # noinspection PyUnresolvedReferences
  75. import magic
  76. except ImportError:
  77. logging.error('This program requires that the `python-magic` library'
  78. ' is installed, NOT the Python bindings to libmagic API \n'
  79. 'see https://github.com/ahupp/python-magic\n')
  80. exit(1)
  81. VERSION = "prismedia v0.5"
  82. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  83. VALID_CATEGORIES = (
  84. "music", "films", "vehicles",
  85. "sports", "travels", "gaming", "people",
  86. "comedy", "entertainment", "news",
  87. "how to", "education", "activism", "science & technology",
  88. "science", "technology", "animals"
  89. )
  90. VALID_PLATFORM = ('youtube', 'peertube')
  91. VALID_LANGUAGES = ('arabic', 'english', 'french',
  92. 'german', 'hindi', 'italian',
  93. 'japanese', 'korean', 'mandarin',
  94. 'portuguese', 'punjabi', 'russian', 'spanish')
  95. def validateVideo(path):
  96. supported_types = ['video/mp4']
  97. if magic.from_file(path, mime=True) in supported_types:
  98. return path
  99. else:
  100. return False
  101. def validateCategory(category):
  102. if category.lower() in VALID_CATEGORIES:
  103. return True
  104. else:
  105. return False
  106. def validatePrivacy(privacy):
  107. if privacy.lower() in VALID_PRIVACY_STATUSES:
  108. return True
  109. else:
  110. return False
  111. def validatePlatform(platform):
  112. for plfrm in platform.split(','):
  113. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  114. return False
  115. return True
  116. def validateLanguage(language):
  117. if language.lower() in VALID_LANGUAGES:
  118. return True
  119. else:
  120. return False
  121. def validatePublish(publish):
  122. # Check date format and if date is future
  123. try:
  124. now = datetime.datetime.now()
  125. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  126. if now >= publishAt:
  127. return False
  128. except ValueError:
  129. return False
  130. return True
  131. if __name__ == '__main__':
  132. options = docopt(__doc__, version=VERSION)
  133. schema = Schema({
  134. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  135. Optional('--name'): Or(None, And(
  136. str,
  137. lambda x: not x.isdigit(),
  138. error="The video name should be a string")
  139. ),
  140. Optional('--description'): Or(None, And(
  141. str,
  142. lambda x: not x.isdigit(),
  143. error="The video name should be a string")
  144. ),
  145. Optional('--tags'): Or(None, And(
  146. str,
  147. lambda x: not x.isdigit(),
  148. error="Tags should be a string")
  149. ),
  150. Optional('--mt'): bool,
  151. Optional('--category'): Or(None, And(
  152. str,
  153. validateCategory,
  154. error="Category not recognized, please see --help")
  155. ),
  156. Optional('--language'): Or(None, And(
  157. str,
  158. validateLanguage,
  159. error="Language not recognized, please see --help")
  160. ),
  161. Optional('--privacy'): Or(None, And(
  162. str,
  163. validatePrivacy,
  164. error="Please use recognized privacy between public, unlisted or private")
  165. ),
  166. Optional('--nfo'): Or(None, str),
  167. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  168. Optional('--publishAt'): Or(None, And(
  169. str,
  170. validatePublish,
  171. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  172. ),
  173. Optional('--cca'): bool,
  174. Optional('--disable-comments'): bool,
  175. Optional('--nsfw'): bool,
  176. Optional('--playlist'): Or(None, str),
  177. Optional('--playlistCreate'): bool,
  178. '--help': bool,
  179. '--version': bool
  180. })
  181. options = utils.parseNFO(options)
  182. try:
  183. options = schema.validate(options)
  184. except SchemaError as e:
  185. exit(e)
  186. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  187. yt_upload.run(options)
  188. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  189. pt_upload.run(options)