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.

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