scripting your 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.

prismedia_upload.py 9.3KB

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