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.

231 lines
8.8 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. --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. -h --help Show this help.
  38. --version Show version.
  39. Categories:
  40. Category is the type of video you upload. Default is films.
  41. Here are available categories from Peertube and Youtube:
  42. music, films, vehicles,
  43. sports, travels, gaming, people,
  44. comedy, entertainment, news,
  45. how to, education, activism, science & technology,
  46. science, technology, animals
  47. Languages:
  48. Language of the video (audio track), choose one. Default is English
  49. Here are available languages from Peertube and Youtube:
  50. Arabic, English, French, German, Hindi, Italian,
  51. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  52. """
  53. from os.path import dirname, realpath
  54. import sys
  55. import datetime
  56. import logging
  57. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  58. from docopt import docopt
  59. # Allows a relative import from the parent folder
  60. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  61. import yt_upload
  62. import pt_upload
  63. import utils
  64. try:
  65. # noinspection PyUnresolvedReferences
  66. from schema import Schema, And, Or, Optional, SchemaError
  67. except ImportError:
  68. logging.error('This program requires that the `schema` data-validation library'
  69. ' is installed: \n'
  70. 'see https://github.com/halst/schema\n')
  71. exit(1)
  72. try:
  73. # noinspection PyUnresolvedReferences
  74. import magic
  75. except ImportError:
  76. logging.error('This program requires that the `python-magic` library'
  77. ' is installed, NOT the Python bindings to libmagic API \n'
  78. 'see https://github.com/ahupp/python-magic\n')
  79. exit(1)
  80. VERSION = "prismedia v0.5"
  81. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  82. VALID_CATEGORIES = (
  83. "music", "films", "vehicles",
  84. "sports", "travels", "gaming", "people",
  85. "comedy", "entertainment", "news",
  86. "how to", "education", "activism", "science & technology",
  87. "science", "technology", "animals"
  88. )
  89. VALID_PLATFORM = ('youtube', 'peertube')
  90. VALID_LANGUAGES = ('arabic', 'english', 'french',
  91. 'german', 'hindi', 'italian',
  92. 'japanese', 'korean', 'mandarin',
  93. 'portuguese', 'punjabi', 'russian', 'spanish')
  94. def validateVideo(path):
  95. supported_types = ['video/mp4']
  96. if magic.from_file(path, mime=True) in supported_types:
  97. return path
  98. else:
  99. return False
  100. def validateCategory(category):
  101. if category.lower() in VALID_CATEGORIES:
  102. return True
  103. else:
  104. return False
  105. def validatePrivacy(privacy):
  106. if privacy.lower() in VALID_PRIVACY_STATUSES:
  107. return True
  108. else:
  109. return False
  110. def validatePlatform(platform):
  111. for plfrm in platform.split(','):
  112. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  113. return False
  114. return True
  115. def validateLanguage(language):
  116. if language.lower() in VALID_LANGUAGES:
  117. return True
  118. else:
  119. return False
  120. def validatePublish(publish):
  121. # Check date format and if date is future
  122. try:
  123. now = datetime.datetime.now()
  124. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  125. if now >= publishAt:
  126. return False
  127. except ValueError:
  128. return False
  129. return True
  130. def validateThumbnail(thumbnail):
  131. supported_types = ['image/jpg', 'image/jpeg']
  132. if magic.from_file(thumbnail, mime=True) in supported_types:
  133. return thumbnail
  134. else:
  135. return False
  136. if __name__ == '__main__':
  137. options = docopt(__doc__, version=VERSION)
  138. schema = Schema({
  139. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  140. Optional('--name'): Or(None, And(
  141. str,
  142. lambda x: not x.isdigit(),
  143. error="The video name should be a string")
  144. ),
  145. Optional('--description'): Or(None, And(
  146. str,
  147. lambda x: not x.isdigit(),
  148. error="The video name should be a string")
  149. ),
  150. Optional('--tags'): Or(None, And(
  151. str,
  152. lambda x: not x.isdigit(),
  153. error="Tags should be a string")
  154. ),
  155. Optional('--mt'): bool,
  156. Optional('--category'): Or(None, And(
  157. str,
  158. validateCategory,
  159. error="Category not recognized, please see --help")
  160. ),
  161. Optional('--language'): Or(None, And(
  162. str,
  163. validateLanguage,
  164. error="Language not recognized, please see --help")
  165. ),
  166. Optional('--privacy'): Or(None, And(
  167. str,
  168. validatePrivacy,
  169. error="Please use recognized privacy between public, unlisted or private")
  170. ),
  171. Optional('--nfo'): Or(None, str),
  172. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  173. Optional('--publishAt'): Or(None, And(
  174. str,
  175. validatePublish,
  176. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  177. ),
  178. Optional('--cca'): bool,
  179. Optional('--disable-comments'): bool,
  180. Optional('--nsfw'): bool,
  181. Optional('--thumbnail'): Or(None, And(
  182. str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
  183. ),
  184. '--help': bool,
  185. '--version': bool
  186. })
  187. options = utils.parseNFO(options)
  188. if not options.get('--thumbnail'):
  189. options = utils.searchThumbnail(options)
  190. try:
  191. options = schema.validate(options)
  192. except SchemaError as e:
  193. exit(e)
  194. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  195. yt_upload.run(options)
  196. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  197. pt_upload.run(options)