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.

217 lines
6.2 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. from os import devnull
  6. from subprocess import check_call, CalledProcessError, STDOUT
  7. import unicodedata
  8. ### CATEGORIES ###
  9. YOUTUBE_CATEGORY = {
  10. "music": 10,
  11. "films": 1,
  12. "vehicles": 2,
  13. "sport": 17,
  14. "travels": 19,
  15. "gaming": 20,
  16. "people": 22,
  17. "comedy": 23,
  18. "entertainment": 24,
  19. "news": 25,
  20. "how to": 26,
  21. "education": 27,
  22. "activism": 29,
  23. "science & technology": 28,
  24. "science": 28,
  25. "technology": 28,
  26. "animals": 15
  27. }
  28. PEERTUBE_CATEGORY = {
  29. "music": 1,
  30. "films": 2,
  31. "vehicles": 3,
  32. "sport": 5,
  33. "travels": 6,
  34. "gaming": 7,
  35. "people": 8,
  36. "comedy": 9,
  37. "entertainment": 10,
  38. "news": 11,
  39. "how to": 12,
  40. "education": 13,
  41. "activism": 14,
  42. "science & technology": 15,
  43. "science": 15,
  44. "technology": 15,
  45. "animals": 16
  46. }
  47. ### LANGUAGES ###
  48. YOUTUBE_LANGUAGE = {
  49. "arabic": 'ar',
  50. "english": 'en',
  51. "french": 'fr',
  52. "german": 'de',
  53. "hindi": 'hi',
  54. "italian": 'it',
  55. "japanese": 'ja',
  56. "korean": 'ko',
  57. "mandarin": 'zh-CN',
  58. "portuguese": 'pt-PT',
  59. "punjabi": 'pa',
  60. "russian": 'ru',
  61. "spanish": 'es'
  62. }
  63. PEERTUBE_LANGUAGE = {
  64. "arabic": 5,
  65. "english": 1,
  66. "french": 13,
  67. "german": 11,
  68. "hindi": 4,
  69. "italian": 14,
  70. "japanese": 9,
  71. "korean": 12,
  72. "mandarin": 3,
  73. "portuguese": 6,
  74. "punjabi": 10,
  75. "russian": 8,
  76. "spanish": 2
  77. }
  78. ######################
  79. def getCategory(category, platform):
  80. if platform == "youtube":
  81. return YOUTUBE_CATEGORY[category.lower()]
  82. else:
  83. return PEERTUBE_CATEGORY[category.lower()]
  84. def getLanguage(language, platform):
  85. if platform == "youtube":
  86. return YOUTUBE_LANGUAGE[language.lower()]
  87. else:
  88. return PEERTUBE_LANGUAGE[language.lower()]
  89. # return the nfo as a RawConfigParser object
  90. def loadNFO(options):
  91. video_directory = dirname(options.get('--file')) + "/"
  92. if options.get('--nfo'):
  93. try:
  94. print "Using " + options.get('--nfo') + " as NFO, loading..."
  95. if isfile(options.get('--nfo')):
  96. nfo = RawConfigParser()
  97. nfo.read(options.get('--nfo'))
  98. return nfo
  99. else:
  100. exit("Given NFO file does not exist, please check your path.")
  101. except Exception as e:
  102. exit("Problem with NFO file: " + str(e))
  103. else:
  104. if options.get('--name'):
  105. nfo_file = video_directory + options.get('--name') + ".txt"
  106. print nfo_file
  107. if isfile(nfo_file):
  108. try:
  109. print "Using " + nfo_file + " as NFO, loading..."
  110. nfo = RawConfigParser()
  111. nfo.read(nfo_file)
  112. return nfo
  113. except Exception as e:
  114. exit("Problem with NFO file: " + str(e))
  115. # if --nfo and --name does not exist, use --file as default
  116. video_file = splitext(basename(options.get('--file')))[0]
  117. nfo_file = video_directory + video_file + ".txt"
  118. if isfile(nfo_file):
  119. try:
  120. print "Using " + nfo_file + " as NFO, loading..."
  121. nfo = RawConfigParser()
  122. nfo.read(nfo_file)
  123. return nfo
  124. except Exception as e:
  125. exit("Problem with nfo file: " + str(e))
  126. print "No suitable NFO found, skipping."
  127. return False
  128. def parseNFO(options):
  129. nfo = loadNFO(options)
  130. if nfo:
  131. # We need to check all options and replace it with the nfo value if not defined (None or False)
  132. for key, value in options.iteritems():
  133. key = key.replace("-", "")
  134. try:
  135. # get string options
  136. if value is None and nfo.get('video', key):
  137. options['--' + key] = nfo.get('video', key)
  138. # get boolean options
  139. elif value is False and nfo.getboolean('video', key):
  140. options['--' + key] = nfo.getboolean('video', key)
  141. except NoOptionError:
  142. continue
  143. except NoSectionError:
  144. exit("Given NFO file miss section [video], please check syntax of your NFO.")
  145. return options
  146. def upcaseFirstLetter(s):
  147. return s[0].upper() + s[1:]
  148. def publishAt(publishAt, oauth, url, idvideo):
  149. try:
  150. FNULL = open(devnull, 'w')
  151. check_call(["at", "-V"], stdout=FNULL, stderr=STDOUT)
  152. except CalledProcessError:
  153. exit("You need to install the atd daemon to use the publishAt option.")
  154. try:
  155. FNULL = open(devnull, 'w')
  156. check_call(["curl", "-V"], stdout=FNULL, stderr=STDOUT)
  157. except CalledProcessError:
  158. exit("You need to install the curl command line to use the publishAt option.")
  159. time = publishAt.split("T")
  160. # Remove leading seconds that atd does not manage
  161. if time[1].count(":") == 2:
  162. time[1] = time[1][:-3]
  163. atTime = time[1] + " " + time[0]
  164. token=str(oauth.__dict__['_client'].__dict__['access_token'])
  165. atFile = "/tmp/peertube_" + idvideo + "_" + publishAt + ".at"
  166. try:
  167. file = open(atFile,"w")
  168. file.write("curl '" + url + "/api/v1/videos/" + idvideo + "' -X PUT -H 'Authorization: Bearer " + token + "' -H 'Content-Type: multipart/form-data' -F 'privacy=1'")
  169. file.write(" ") # atd needs an empty line at the end of the file to load...
  170. file.close()
  171. except Exception as e:
  172. if hasattr(e, 'message'):
  173. print("Error: " + str(e.message))
  174. else:
  175. print("Error: " + str(e))
  176. try:
  177. FNULL = open(devnull, 'w')
  178. check_call(["at", "-M", "-f", atFile, atTime], stdout=FNULL, stderr=STDOUT)
  179. except Exception as e:
  180. if hasattr(e, 'message'):
  181. print("Error: " + str(e.message))
  182. else:
  183. print("Error: " + str(e))
  184. def mastodonTag(tag):
  185. tags = tag.split(' ')
  186. mtag = ''
  187. for s in tags:
  188. if s == '':
  189. continue
  190. strtag = unicodedata.normalize('NFKD', unicode (s, 'utf-8')).encode('ASCII', 'ignore')
  191. strtag = ''.join(e for e in strtag if e.isalnum())
  192. strtag = upcaseFirstLetter(strtag)
  193. mtag = mtag + strtag
  194. return mtag