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.

238 lines
9.2 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/env python2
  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 -f <FILE> --tags=STRING [--mt options]
  8. prismedia_upload.py -h | --help
  9. prismedia_upload.py --version
  10. Options:
  11. -f, --file=STRING Path to the video file to upload in mp4
  12. --name=NAME Name of the video to upload. (default to video filename)
  13. -d, --description=STRING Description of the video. (default: default description)
  14. -t, --tags=STRING Tags for the video. comma separated.
  15. WARN: tags with space and special characters (!, ', ", ?, ...)
  16. are not supported by Mastodon to be published from Peertube
  17. use mastodon compatibility below
  18. --mt Force Mastodon compatibility for tags (drop every incompatible characters inside tags)
  19. This option requires --tags
  20. -c, --category=STRING Category for the videos, see below. (default: Films)
  21. --cca License should be CreativeCommon Attribution (affects Youtube upload only)
  22. -p, --privacy=STRING Choose between public, unlisted or private. (default: private)
  23. --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
  24. --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
  25. --nfo=STRING Configure a specific nfo file to set options for the video.
  26. By default Prismedia search a .txt based on video name
  27. See nfo_example.txt for more details
  28. --platform=STRING List of platform(s) to upload to, comma separated.
  29. Supported platforms are youtube and peertube (default is both)
  30. --language=STRING Specify the default language for video. See below for supported language. (default is English)
  31. --publishAt=DATE Publish the video at the given DATE using local server timezone.
  32. DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00
  33. DATE should be in the future
  34. For Peertube, requires the "atd" and "curl utilities installed on the system
  35. --thumbnail=STRING Path to a file to use as a thumbnail for the video.
  36. Supported types are jpg and jpeg.
  37. By default, prismedia search for an image based on video name followed by .jpg or .jpeg
  38. --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube.
  39. If the playlist is not found, spawn an error except if --playlist-create is set.
  40. --playlistCreate Create the playlist if not exists. (default do not create)
  41. Only relevant if --playlist is set.
  42. -h --help Show this help.
  43. --version Show version.
  44. Categories:
  45. Category is the type of video you upload. Default is films.
  46. Here are available categories from Peertube and Youtube:
  47. music, films, vehicles,
  48. sports, travels, gaming, people,
  49. comedy, entertainment, news,
  50. how to, education, activism, science & technology,
  51. science, technology, animals
  52. Languages:
  53. Language of the video (audio track), choose one. Default is English
  54. Here are available languages from Peertube and Youtube:
  55. Arabic, English, French, German, Hindi, Italian,
  56. Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish
  57. """
  58. from os.path import dirname, realpath
  59. import sys
  60. import datetime
  61. import logging
  62. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  63. from docopt import docopt
  64. # Allows a relative import from the parent folder
  65. sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
  66. import yt_upload
  67. import pt_upload
  68. import utils
  69. try:
  70. # noinspection PyUnresolvedReferences
  71. from schema import Schema, And, Or, Optional, SchemaError
  72. except ImportError:
  73. logging.error('This program requires that the `schema` data-validation library'
  74. ' is installed: \n'
  75. 'see https://github.com/halst/schema\n')
  76. exit(1)
  77. try:
  78. # noinspection PyUnresolvedReferences
  79. import magic
  80. except ImportError:
  81. logging.error('This program requires that the `python-magic` library'
  82. ' is installed, NOT the Python bindings to libmagic API \n'
  83. 'see https://github.com/ahupp/python-magic\n')
  84. exit(1)
  85. VERSION = "prismedia v0.6"
  86. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  87. VALID_CATEGORIES = (
  88. "music", "films", "vehicles",
  89. "sports", "travels", "gaming", "people",
  90. "comedy", "entertainment", "news",
  91. "how to", "education", "activism", "science & technology",
  92. "science", "technology", "animals"
  93. )
  94. VALID_PLATFORM = ('youtube', 'peertube')
  95. VALID_LANGUAGES = ('arabic', 'english', 'french',
  96. 'german', 'hindi', 'italian',
  97. 'japanese', 'korean', 'mandarin',
  98. 'portuguese', 'punjabi', 'russian', 'spanish')
  99. def validateVideo(path):
  100. supported_types = ['video/mp4']
  101. if magic.from_file(path, mime=True) in supported_types:
  102. return path
  103. else:
  104. return False
  105. def validateCategory(category):
  106. if category.lower() in VALID_CATEGORIES:
  107. return True
  108. else:
  109. return False
  110. def validatePrivacy(privacy):
  111. if privacy.lower() in VALID_PRIVACY_STATUSES:
  112. return True
  113. else:
  114. return False
  115. def validatePlatform(platform):
  116. for plfrm in platform.split(','):
  117. if plfrm.lower().replace(" ", "") not in VALID_PLATFORM:
  118. return False
  119. return True
  120. def validateLanguage(language):
  121. if language.lower() in VALID_LANGUAGES:
  122. return True
  123. else:
  124. return False
  125. def validatePublish(publish):
  126. # Check date format and if date is future
  127. try:
  128. now = datetime.datetime.now()
  129. publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S')
  130. if now >= publishAt:
  131. return False
  132. except ValueError:
  133. return False
  134. return True
  135. def validateThumbnail(thumbnail):
  136. supported_types = ['image/jpg', 'image/jpeg']
  137. if magic.from_file(thumbnail, mime=True) in supported_types:
  138. return thumbnail
  139. else:
  140. return False
  141. if __name__ == '__main__':
  142. options = docopt(__doc__, version=VERSION)
  143. schema = Schema({
  144. '--file': And(str, validateVideo, error='file is not supported, please use mp4'),
  145. Optional('--name'): Or(None, And(
  146. str,
  147. lambda x: not x.isdigit(),
  148. error="The video name should be a string")
  149. ),
  150. Optional('--description'): Or(None, And(
  151. str,
  152. lambda x: not x.isdigit(),
  153. error="The video name should be a string")
  154. ),
  155. Optional('--tags'): Or(None, And(
  156. str,
  157. lambda x: not x.isdigit(),
  158. error="Tags should be a string")
  159. ),
  160. Optional('--mt'): bool,
  161. Optional('--category'): Or(None, And(
  162. str,
  163. validateCategory,
  164. error="Category not recognized, please see --help")
  165. ),
  166. Optional('--language'): Or(None, And(
  167. str,
  168. validateLanguage,
  169. error="Language not recognized, please see --help")
  170. ),
  171. Optional('--privacy'): Or(None, And(
  172. str,
  173. validatePrivacy,
  174. error="Please use recognized privacy between public, unlisted or private")
  175. ),
  176. Optional('--nfo'): Or(None, str),
  177. Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")),
  178. Optional('--publishAt'): Or(None, And(
  179. str,
  180. validatePublish,
  181. error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
  182. ),
  183. Optional('--cca'): bool,
  184. Optional('--disable-comments'): bool,
  185. Optional('--nsfw'): bool,
  186. Optional('--thumbnail'): Or(None, And(
  187. str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
  188. ),
  189. Optional('--playlist'): Or(None, str),
  190. Optional('--playlistCreate'): bool,
  191. '--help': bool,
  192. '--version': bool
  193. })
  194. options = utils.parseNFO(options)
  195. if not options.get('--thumbnail'):
  196. options = utils.searchThumbnail(options)
  197. try:
  198. options = schema.validate(options)
  199. except SchemaError as e:
  200. exit(e)
  201. if options.get('--platform') is None or "youtube" in options.get('--platform'):
  202. yt_upload.run(options)
  203. if options.get('--platform') is None or "peertube" in options.get('--platform'):
  204. pt_upload.run(options)