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.

158 lines
5.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. -h --help Show this help.
  24. --version Show version.
  25. Categories:
  26. Category is the type of video you upload. Default is films.
  27. Here are available categories from Peertube and Youtube:
  28. music, films, vehicles,
  29. sports, travels, gaming, people,
  30. comedy, entertainment, news,
  31. how to, education, activism, science & technology,
  32. science, technology, animals
  33. """
  34. from os.path import dirname, realpath
  35. import sys
  36. from docopt import docopt
  37. # Allows a relative import from the parent folder
  38. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  39. import yt_upload
  40. import pt_upload
  41. import utils
  42. try:
  43. # noinspection PyUnresolvedReferences
  44. from schema import Schema, And, Or, Optional, SchemaError
  45. except ImportError:
  46. exit('This program requires that the `schema` data-validation library'
  47. ' is installed: \n'
  48. 'see https://github.com/halst/schema\n')
  49. try:
  50. # noinspection PyUnresolvedReferences
  51. import magic
  52. except ImportError:
  53. exit('This program requires that the `python-magic` library'
  54. ' is installed, NOT the Python bindings to libmagic API \n'
  55. 'see https://github.com/ahupp/python-magic\n')
  56. VERSION = "prismedia 0.3"
  57. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  58. VALID_CATEGORIES = (
  59. "music", "films", "vehicles",
  60. "sports", "travels", "gaming", "people",
  61. "comedy", "entertainment", "news",
  62. "how to", "education", "activism", "science & technology",
  63. "science", "technology", "animals"
  64. )
  65. VALID_PLATFORM = ('youtube', 'peertube')
  66. def validateVideo(path):
  67. supported_types = ['video/mp4']
  68. if magic.from_file(path, mime=True) in supported_types:
  69. return path
  70. else:
  71. return False
  72. def validateCategory(category):
  73. if category.lower() in VALID_CATEGORIES:
  74. return True
  75. else:
  76. return False
  77. def validatePrivacy(privacy):
  78. if privacy.lower() in VALID_PRIVACY_STATUSES:
  79. return True
  80. else:
  81. return False
  82. def validatePlatform(platform):
  83. for plfrm in platform.split(','):
  84. if plfrm not in VALID_PLATFORM:
  85. return False
  86. return True
  87. if __name__ == '__main__':
  88. options = docopt(__doc__, version=VERSION)
  89. schema = Schema({
  90. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  91. Optional('--name'): Or(None, And(
  92. str,
  93. lambda x: not x.isdigit(),
  94. error="The video name should be a string")
  95. ),
  96. Optional('--description'): Or(None, And(
  97. str,
  98. lambda x: not x.isdigit(),
  99. error="The video name should be a string")
  100. ),
  101. Optional('--tags'): Or(None, And(
  102. str,
  103. lambda x: not x.isdigit(),
  104. error="Tags should be a string")
  105. ),
  106. Optional('--category'): Or(None, And(
  107. str,
  108. validateCategory,
  109. error="Category not recognized, please see --help")
  110. ),
  111. Optional('--privacy'): Or(None, And(
  112. str,
  113. validatePrivacy,
  114. error="Please use recognized privacy between public, unlisted or private")
  115. ),
  116. Optional('--nfo'): Or(None, str),
  117. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  118. Optional('--cca'): bool,
  119. Optional('--disable-comments'): bool,
  120. Optional('--nsfw'): bool,
  121. '--help': bool,
  122. '--version': bool
  123. })
  124. try:
  125. options = schema.validate(options)
  126. except SchemaError as e:
  127. exit(e)
  128. options = utils.parseNFO(options)
  129. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  130. yt_upload.run(options)
  131. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  132. pt_upload.run(options)