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.

183 lines
6.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 -h | --help
  8. prismedia_upload.py --version
  9. Options:
  10. --name=NAME Name of the video to upload. (default to video filename)
  11. -d, --description=STRING Description of the video. (default: default description)
  12. -t, --tags=STRING Tags for the video. comma separated
  13. -c, --category=STRING Category for the videos, see below. (default: Films)
  14. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  15. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  16. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  17. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  18. --nfo=STRING Configure a specific nfo file to set options for the video.
  19. By default Prismedia search a .txt based on video name
  20. See nfo_example.txt for more details
  21. --platform=STRING List of platform(s) to upload to, comma separated.
  22. Supported platforms are youtube and peertube (default is both)
  23. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  24. -h --help Show this help.
  25. --version Show version.
  26. Categories:
  27. Category is the type of video you upload. Default is films.
  28. Here are available categories from Peertube and Youtube:
  29. music, films, vehicles,
  30. sports, travels, gaming, people,
  31. comedy, entertainment, news,
  32. how to, education, activism, science & technology,
  33. science, technology, animals
  34. Languages:
  35. Language of the video (audio track), choose one. Default is English
  36. Here are available languages from Peertube and Youtube:
  37. Arabic, English, French, German, Hindi, Italian,
  38. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  39. """
  40. from os.path import dirname, realpath
  41. import sys
  42. from docopt import docopt
  43. # Allows a relative import from the parent folder
  44. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  45. import yt_upload
  46. import pt_upload
  47. import utils
  48. try:
  49. # noinspection PyUnresolvedReferences
  50. from schema import Schema, And, Or, Optional, SchemaError
  51. except ImportError:
  52. exit('This program requires that the `schema` data-validation library'
  53. ' is installed: \n'
  54. 'see https://github.com/halst/schema\n')
  55. try:
  56. # noinspection PyUnresolvedReferences
  57. import magic
  58. except ImportError:
  59. exit('This program requires that the `python-magic` library'
  60. ' is installed, NOT the Python bindings to libmagic API \n'
  61. 'see https://github.com/ahupp/python-magic\n')
  62. VERSION = "prismedia v0.3"
  63. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  64. VALID_CATEGORIES = (
  65. "music", "films", "vehicles",
  66. "sports", "travels", "gaming", "people",
  67. "comedy", "entertainment", "news",
  68. "how to", "education", "activism", "science & technology",
  69. "science", "technology", "animals"
  70. )
  71. VALID_PLATFORM = ('youtube', 'peertube')
  72. VALID_LANGUAGES = ('arabic', 'english', 'french',
  73. 'german', 'hindi', 'italian',
  74. 'japanese', 'korean', 'mandarin',
  75. 'portuguese', 'punjabi', 'russian', 'spanish')
  76. def validateVideo(path):
  77. supported_types = ['video/mp4']
  78. if magic.from_file(path, mime=True) in supported_types:
  79. return path
  80. else:
  81. return False
  82. def validateCategory(category):
  83. if category.lower() in VALID_CATEGORIES:
  84. return True
  85. else:
  86. return False
  87. def validatePrivacy(privacy):
  88. if privacy.lower() in VALID_PRIVACY_STATUSES:
  89. return True
  90. else:
  91. return False
  92. def validatePlatform(platform):
  93. for plfrm in platform.split(','):
  94. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  95. return False
  96. return True
  97. def validateLanguage(language):
  98. if language.lower() in VALID_LANGUAGES:
  99. return True
  100. else:
  101. return False
  102. if __name__ == '__main__':
  103. options = docopt(__doc__, version=VERSION)
  104. schema = Schema({
  105. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  106. Optional('--name'): Or(None, And(
  107. str,
  108. lambda x: not x.isdigit(),
  109. error="The video name should be a string")
  110. ),
  111. Optional('--description'): Or(None, And(
  112. str,
  113. lambda x: not x.isdigit(),
  114. error="The video name should be a string")
  115. ),
  116. Optional('--tags'): Or(None, And(
  117. str,
  118. lambda x: not x.isdigit(),
  119. error="Tags should be a string")
  120. ),
  121. Optional('--category'): Or(None, And(
  122. str,
  123. validateCategory,
  124. error="Category not recognized, please see --help")
  125. ),
  126. Optional('--language'): Or(None, And(
  127. str,
  128. validateLanguage,
  129. error="Language not recognized, please see --help")
  130. ),
  131. Optional('--privacy'): Or(None, And(
  132. str,
  133. validatePrivacy,
  134. error="Please use recognized privacy between public, unlisted or private")
  135. ),
  136. Optional('--nfo'): Or(None, str),
  137. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  138. Optional('--cca'): bool,
  139. Optional('--disable-comments'): bool,
  140. Optional('--nsfw'): bool,
  141. '--help': bool,
  142. '--version': bool
  143. })
  144. options = utils.parseNFO(options)
  145. try:
  146. options = schema.validate(options)
  147. except SchemaError as e:
  148. exit(e)
  149. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  150. yt_upload.run(options)
  151. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  152. pt_upload.run(options)