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.

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