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.

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