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.

175 lines
4.6 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 unicodedata
  6. ### CATEGORIES ###
  7. YOUTUBE_CATEGORY = {
  8. "music": 10,
  9. "films": 1,
  10. "vehicles": 2,
  11. "sport": 17,
  12. "travels": 19,
  13. "gaming": 20,
  14. "people": 22,
  15. "comedy": 23,
  16. "entertainment": 24,
  17. "news": 25,
  18. "how to": 26,
  19. "education": 27,
  20. "activism": 29,
  21. "science & technology": 28,
  22. "science": 28,
  23. "technology": 28,
  24. "animals": 15
  25. }
  26. PEERTUBE_CATEGORY = {
  27. "music": 1,
  28. "films": 2,
  29. "vehicles": 3,
  30. "sport": 5,
  31. "travels": 6,
  32. "gaming": 7,
  33. "people": 8,
  34. "comedy": 9,
  35. "entertainment": 10,
  36. "news": 11,
  37. "how to": 12,
  38. "education": 13,
  39. "activism": 14,
  40. "science & technology": 15,
  41. "science": 15,
  42. "technology": 15,
  43. "animals": 16
  44. }
  45. ### LANGUAGES ###
  46. YOUTUBE_LANGUAGE = {
  47. "arabic": 'ar',
  48. "english": 'en',
  49. "french": 'fr',
  50. "german": 'de',
  51. "hindi": 'hi',
  52. "italian": 'it',
  53. "japanese": 'ja',
  54. "korean": 'ko',
  55. "mandarin": 'zh-CN',
  56. "portuguese": 'pt-PT',
  57. "punjabi": 'pa',
  58. "russian": 'ru',
  59. "spanish": 'es'
  60. }
  61. PEERTUBE_LANGUAGE = {
  62. "arabic": 5,
  63. "english": 1,
  64. "french": 13,
  65. "german": 11,
  66. "hindi": 4,
  67. "italian": 14,
  68. "japanese": 9,
  69. "korean": 12,
  70. "mandarin": 3,
  71. "portuguese": 6,
  72. "punjabi": 10,
  73. "russian": 8,
  74. "spanish": 2
  75. }
  76. ######################
  77. def getCategory(category, platform):
  78. if platform == "youtube":
  79. return YOUTUBE_CATEGORY[category.lower()]
  80. else:
  81. return PEERTUBE_CATEGORY[category.lower()]
  82. def getLanguage(language, platform):
  83. if platform == "youtube":
  84. return YOUTUBE_LANGUAGE[language.lower()]
  85. else:
  86. return PEERTUBE_LANGUAGE[language.lower()]
  87. # return the nfo as a RawConfigParser object
  88. def loadNFO(options):
  89. video_directory = dirname(options.get('--file')) + "/"
  90. if options.get('--nfo'):
  91. try:
  92. print "Using " + options.get('--nfo') + " as NFO, loading..."
  93. if isfile(options.get('--nfo')):
  94. nfo = RawConfigParser()
  95. nfo.read(options.get('--nfo'))
  96. return nfo
  97. else:
  98. exit("Given NFO file does not exist, please check your path.")
  99. except Exception as e:
  100. exit("Problem with NFO file: " + str(e))
  101. else:
  102. if options.get('--name'):
  103. nfo_file = video_directory + options.get('--name') + ".txt"
  104. print nfo_file
  105. if isfile(nfo_file):
  106. try:
  107. print "Using " + nfo_file + " as NFO, loading..."
  108. nfo = RawConfigParser()
  109. nfo.read(nfo_file)
  110. return nfo
  111. except Exception as e:
  112. exit("Problem with NFO file: " + str(e))
  113. # if --nfo and --name does not exist, use --file as default
  114. video_file = splitext(basename(options.get('--file')))[0]
  115. nfo_file = video_directory + video_file + ".txt"
  116. if isfile(nfo_file):
  117. try:
  118. print "Using " + nfo_file + " as NFO, loading..."
  119. nfo = RawConfigParser()
  120. nfo.read(nfo_file)
  121. return nfo
  122. except Exception as e:
  123. exit("Problem with nfo file: " + str(e))
  124. print "No suitable NFO found, skipping."
  125. return False
  126. def parseNFO(options):
  127. nfo = loadNFO(options)
  128. if nfo:
  129. # We need to check all options and replace it with the nfo value if not defined (None or False)
  130. for key, value in options.iteritems():
  131. key = key.replace("-", "")
  132. try:
  133. # get string options
  134. if value is None and nfo.get('video', key):
  135. options['--' + key] = nfo.get('video', key)
  136. # get boolean options
  137. elif value is False and nfo.getboolean('video', key):
  138. options['--' + key] = nfo.getboolean('video', key)
  139. except NoOptionError:
  140. continue
  141. except NoSectionError:
  142. exit("Given NFO file miss section [video], please check syntax of your NFO.")
  143. return options
  144. def upcaseFirstLetter(s):
  145. return s[0].upper() + s[1:]
  146. def mastodonTag(tag):
  147. tags = tag.split(' ')
  148. mtag = ''
  149. for s in tags:
  150. if s == '':
  151. continue
  152. strtag = unicodedata.normalize('NFKD', unicode (s, 'utf-8')).encode('ASCII', 'ignore')
  153. strtag = ''.join(e for e in strtag if e.isalnum())
  154. strtag = upcaseFirstLetter(strtag)
  155. mtag = mtag + strtag
  156. return mtag