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.

213 lines
6.3 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. ### 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.iteritems():
  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(options):
  116. video_directory = dirname(options.get('--file')) + "/"
  117. if options.get('--nfo'):
  118. try:
  119. logging.info("Using " + options.get('--nfo') + " as NFO, loading...")
  120. if isfile(options.get('--nfo')):
  121. nfo = RawConfigParser()
  122. nfo.read(options.get('--nfo'))
  123. return nfo
  124. else:
  125. logging.error("Given NFO file does not exist, please check your path.")
  126. exit(1)
  127. except Exception as e:
  128. logging.error("Problem with NFO file: " + str(e))
  129. exit(1)
  130. else:
  131. if options.get('--name'):
  132. nfo_file = video_directory + options.get('--name') + ".txt"
  133. if isfile(nfo_file):
  134. try:
  135. logging.info("Using " + nfo_file + " as NFO, loading...")
  136. nfo = RawConfigParser()
  137. nfo.read(nfo_file)
  138. return nfo
  139. except Exception as e:
  140. logging.error("Problem with NFO file: " + str(e))
  141. exit(1)
  142. # if --nfo and --name does not exist, use --file as default
  143. video_file = splitext(basename(options.get('--file')))[0]
  144. nfo_file = video_directory + video_file + ".txt"
  145. if isfile(nfo_file):
  146. try:
  147. logging.info("Using " + nfo_file + " as NFO, loading...")
  148. nfo = RawConfigParser()
  149. nfo.read(nfo_file)
  150. return nfo
  151. except Exception as e:
  152. logging.error("Problem with nfo file: " + str(e))
  153. exit(1)
  154. logging.info("No suitable NFO found, skipping.")
  155. return False
  156. def parseNFO(options):
  157. nfo = loadNFO(options)
  158. if nfo:
  159. # We need to check all options and replace it with the nfo value if not defined (None or False)
  160. for key, value in options.iteritems():
  161. key = key.replace("-", "")
  162. try:
  163. # get string options
  164. if value is None and nfo.get('video', key):
  165. options['--' + key] = nfo.get('video', key)
  166. # get boolean options
  167. elif value is False and nfo.getboolean('video', key):
  168. options['--' + key] = nfo.getboolean('video', key)
  169. except NoOptionError:
  170. continue
  171. except NoSectionError:
  172. logging.error("Given NFO file miss section [video], please check syntax of your NFO.")
  173. exit(1)
  174. return options
  175. def upcaseFirstLetter(s):
  176. return s[0].upper() + s[1:]
  177. def cleanString(toclean):
  178. toclean = toclean.decode('utf-8')
  179. toclean = unidecode.unidecode(toclean)
  180. cleaned = re.sub('[^A-Za-z0-9]+', '', toclean)
  181. return cleaned
  182. def decodeArgumentStrings(options, encoding):
  183. # Python crash when decoding from UTF-8 to UTF-8, so we prevent this
  184. if "utf-8" == encoding.lower():
  185. return;
  186. if options["--name"] is not None:
  187. options["--name"] = options["--name"].decode(encoding)
  188. if options["--description"] is not None:
  189. options["--description"] = options["--description"].decode(encoding)
  190. if options["--tags"] is not None:
  191. options["--tags"] = options["--tags"].decode(encoding)