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.

224 lines
6.5 KiB

5 years ago
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
  4. from os.path import dirname, splitext, basename, isfile
  5. import re
  6. from os import devnull
  7. from subprocess import check_call, CalledProcessError, STDOUT
  8. import unidecode
  9. import logging
  10. from six.moves import configparser
  11. ConfigParser = configparser.RawConfigParser
  12. # CATEGORIES #
  13. YOUTUBE_CATEGORY = {
  14. "music": 10,
  15. "films": 1,
  16. "vehicles": 2,
  17. "sport": 17,
  18. "travels": 19,
  19. "gaming": 20,
  20. "people": 22,
  21. "comedy": 23,
  22. "entertainment": 24,
  23. "news": 25,
  24. "how to": 26,
  25. "education": 27,
  26. "activism": 29,
  27. "science & technology": 28,
  28. "science": 28,
  29. "technology": 28,
  30. "animals": 15
  31. }
  32. PEERTUBE_CATEGORY = {
  33. "music": 1,
  34. "films": 2,
  35. "vehicles": 3,
  36. "sport": 5,
  37. "travels": 6,
  38. "gaming": 7,
  39. "people": 8,
  40. "comedy": 9,
  41. "entertainment": 10,
  42. "news": 11,
  43. "how to": 12,
  44. "education": 13,
  45. "activism": 14,
  46. "science & technology": 15,
  47. "science": 15,
  48. "technology": 15,
  49. "animals": 16
  50. }
  51. ### LANGUAGES ###
  52. YOUTUBE_LANGUAGE = {
  53. "arabic": 'ar',
  54. "english": 'en',
  55. "french": 'fr',
  56. "german": 'de',
  57. "hindi": 'hi',
  58. "italian": 'it',
  59. "japanese": 'ja',
  60. "korean": 'ko',
  61. "mandarin": 'zh-CN',
  62. "portuguese": 'pt-PT',
  63. "punjabi": 'pa',
  64. "russian": 'ru',
  65. "spanish": 'es'
  66. }
  67. PEERTUBE_LANGUAGE = {
  68. "arabic": "ar",
  69. "english": "en",
  70. "french": "fr",
  71. "german": "de",
  72. "hindi": "hi",
  73. "italian": "it",
  74. "japanese": "ja",
  75. "korean": "ko",
  76. "mandarin": "zh",
  77. "portuguese": "pt",
  78. "punjabi": "pa",
  79. "russian": "ru",
  80. "spanish": "es"
  81. }
  82. ######################
  83. def getCategory(category, platform):
  84. if platform == "youtube":
  85. return YOUTUBE_CATEGORY[category.lower()]
  86. else:
  87. return PEERTUBE_CATEGORY[category.lower()]
  88. def getLanguage(language, platform):
  89. if platform == "youtube":
  90. return YOUTUBE_LANGUAGE[language.lower()]
  91. else:
  92. return PEERTUBE_LANGUAGE[language.lower()]
  93. def remove_empty_kwargs(**kwargs):
  94. good_kwargs = {}
  95. if kwargs is not None:
  96. for key, value in viewitems(kwargs):
  97. if value:
  98. good_kwargs[key] = value
  99. return good_kwargs
  100. def searchThumbnail(options):
  101. video_directory = dirname(options.get('--file')) + "/"
  102. # First, check for thumbnail based on videoname
  103. if options.get('--name'):
  104. if isfile(video_directory + options.get('--name') + ".jpg"):
  105. options['--thumbnail'] = video_directory + options.get('--name') + ".jpg"
  106. elif isfile(video_directory + options.get('--name') + ".jpeg"):
  107. options['--thumbnail'] = video_directory + options.get('--name') + ".jpeg"
  108. # Then, if we still not have thumbnail, check for thumbnail based on videofile name
  109. if not options.get('--thumbnail'):
  110. video_file = splitext(basename(options.get('--file')))[0]
  111. if isfile(video_directory + video_file + ".jpg"):
  112. options['--thumbnail'] = video_directory + video_file + ".jpg"
  113. elif isfile(video_directory + video_file + ".jpeg"):
  114. options['--thumbnail'] = video_directory + video_file + ".jpeg"
  115. return options
  116. # return the nfo as a ConfigParser object
  117. def loadNFO(options):
  118. video_directory = dirname(options.get('--file')) + "/"
  119. if options.get('--nfo'):
  120. try:
  121. logging.info("Using " + options.get('--nfo') + " as NFO, loading...")
  122. if isfile(options.get('--nfo')):
  123. nfo = ConfigParser()
  124. nfo.read(options.get('--nfo'))
  125. return nfo
  126. else:
  127. logging.error("Given NFO file does not exist, please check your path.")
  128. exit(1)
  129. except Exception as e:
  130. logging.error("Problem with NFO file: " + str(e))
  131. exit(1)
  132. else:
  133. if options.get('--name'):
  134. nfo_file = video_directory + options.get('--name') + ".txt"
  135. if isfile(nfo_file):
  136. try:
  137. logging.info("Using " + nfo_file + " as NFO, loading...")
  138. nfo = ConfigParser()
  139. nfo.read(nfo_file)
  140. return nfo
  141. except Exception as e:
  142. logging.error("Problem with NFO file: " + str(e))
  143. exit(1)
  144. # if --nfo and --name does not exist, use --file as default
  145. video_file = splitext(basename(options.get('--file')))[0]
  146. nfo_file = video_directory + video_file + ".txt"
  147. if isfile(nfo_file):
  148. try:
  149. logging.info("Using " + nfo_file + " as NFO, loading...")
  150. nfo = ConfigParser()
  151. nfo.read(nfo_file)
  152. return nfo
  153. except Exception as e:
  154. logging.error("Problem with nfo file: " + str(e))
  155. exit(1)
  156. logging.info("No suitable NFO found, skipping.")
  157. return False
  158. def parseNFO(options):
  159. nfo = loadNFO(options)
  160. if nfo:
  161. # We need to check all options and replace it with the nfo value if not
  162. # defined (None or False)
  163. for key, value in viewitems(options):
  164. key = key.replace("-", "")
  165. try:
  166. # get string options
  167. if value is None and nfo.get('video', key):
  168. options['--' + key] = nfo.get('video', key)
  169. # get boolean options
  170. elif value is False and nfo.getboolean('video', key):
  171. options['--' + key] = nfo.getboolean('video', key)
  172. except configparser.NoOptionError:
  173. continue
  174. except configparser.NoSectionError:
  175. logging.error("Given NFO file misses section [video], please check the syntax of your NFO.")
  176. exit(1)
  177. return options
  178. def upcaseFirstLetter(s):
  179. return s[0].upper() + s[1:]
  180. def cleanString(toclean):
  181. toclean = toclean.decode('utf-8')
  182. toclean = unidecode.unidecode(toclean)
  183. cleaned = re.sub('[^A-Za-z0-9]+', '', toclean)
  184. return cleaned
  185. def decodeArgumentStrings(options, encoding):
  186. # Python crash when decoding from UTF-8 to UTF-8, so we prevent this
  187. if "utf-8" == encoding.lower():
  188. return
  189. if options["--name"] is not None:
  190. options["--name"] = options["--name"].decode(encoding)
  191. if options["--description"] is not None:
  192. options["--description"] = options["--description"].decode(encoding)
  193. if options["--tags"] is not None:
  194. options["--tags"] = options["--tags"].decode(encoding)