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.

223 lines
6.8 KiB

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