Guest User

Untitled

a guest
May 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # coding:utf-8:
  3. import os
  4. import sys
  5. import shutil
  6. import re
  7. import urllib
  8.  
  9. print os.path.dirname(os.path.realpath(__file__))
  10.  
  11. video = ['mp4', 'mpg', 'mpeg', 'avi', 'wmv', 'mov', 'mkv', 'm4v']
  12. subtitle = ['srt', 'sub']
  13. package = ['zip', 'rar']
  14. ALLOWED = 'abcdefghijklmnopqrstuvwxyz_.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
  15.  
  16. class NoHaySubtitulosException(Exception):
  17. pass
  18.  
  19. def extension_is(fn, exts):
  20. for ext in exts:
  21. if fn.lower().endswith('.' + ext):
  22. return ext
  23. return False
  24.  
  25. def renamed(fn):
  26. fn2 = ''
  27. for c in fn:
  28. if c in ALLOWED:
  29. fn2 += c
  30. return fn2
  31.  
  32. def look_for_video_file():
  33. nvideos = 0
  34. for fn in os.listdir('.'):
  35. if extension_is(fn, video):
  36. video_fn = fn
  37. nvideos += 1
  38.  
  39. if nvideos == 0:
  40. sys.stderr.write('No hay videos.\n')
  41. sys.exit(1)
  42. elif nvideos > 1:
  43. sys.stderr.write('Hay varios videos.\n')
  44. sys.exit(1)
  45.  
  46. video_fn2 = renamed(video_fn)
  47. shutil.move(video_fn, video_fn2)
  48. video_fn = video_fn2
  49. return video_fn
  50.  
  51. def subtitle_for(video_fn):
  52. return '.'.join(video_fn.split('.')[:-1]) + '.sub'
  53.  
  54. def ask_yes_no(question):
  55. while True:
  56. response = raw_input(question + ' (s/n) ')
  57. if response in ['s', 'n']:
  58. return response == 's'
  59.  
  60. def auto_add_subtitle():
  61. video_fn = look_for_video_file()
  62. prefixes = ['Crazy4TV.com-']
  63. for prefix in prefixes:
  64. if video_fn.startswith(prefix):
  65. video_fn = video_fn[len(prefix):]
  66. match = re.search('^([' + ALLOWED + ']+)([sS][0-9][0-9][eE][0-9][0-9])', video_fn)
  67. if match is None:
  68. season_episode = ''
  69. sys.stderr.write('Ojo: no reconozco temporada y episodio.\n¡Ya sé, debe ser una película!')
  70. match = re.search('^([' + ALLOWED + ']+)(20[0-9][0-9]|19[0-9][0-9])', video_fn)
  71. if match is None:
  72. name = '.'.join(video_fn.split('.')[:-1])
  73. else:
  74. name = match.expand('\\1')
  75. else:
  76. name = match.expand('\\1')
  77. season_episode = match.expand('\\2').lower()
  78. name = name.replace('.', ' ').replace('_', ' ').replace('-', ' ')
  79. name = (name.lower().strip() + ' ' + season_episode).strip()
  80. lookup_name = '+'.join(name.split(' '))
  81. sys.stderr.write(lookup_name + '\n')
  82.  
  83. # Search for subtitles
  84. contents = urllib.urlopen('http://www.subdivx.com/index.php?q=' + lookup_name + '&accion=5&masdesc=&subtitulos=1&realiza_b=1').read()
  85. for match in re.findall('bajar.php[?]id=([0-9]+&u=[0-9]+)', contents):
  86. print('Probando subtitulo %s' % (match,))
  87. #os.system('rm -f ' + subtitle_for(video_fn))
  88. contents = urllib.urlopen('http://www.subdivx.com/bajar.php?id=' + match).read()
  89. f = open('tmp', 'w')
  90. f.write(contents)
  91. f.close()
  92. file_tmp = os.popen('file tmp').read()
  93. if 'zip' in file_tmp.lower():
  94. package_fn = 'tmp.zip'
  95. elif 'rar' in file_tmp.lower():
  96. package_fn = 'tmp.rar'
  97. else:
  98. sys.stderr.write('Tipo de archivo no reconocido.\n')
  99. continue
  100. os.system('mv tmp ' + package_fn)
  101.  
  102. try:
  103. add_subtitle(package_fn)
  104. except NoHaySubtitulosException:
  105. continue
  106. if ask_yes_no('El subtitulo esta bien?'):
  107. return
  108.  
  109. sys.stderr.write('No hay mas subtitulos para probar =(.\n')
  110.  
  111. def fix(fn):
  112. f = open(fn, 'r')
  113. c = f.read()
  114. f.close()
  115. c = c.replace('á', 'a')
  116. c = c.replace('é', 'e')
  117. c = c.replace('í', 'i')
  118. c = c.replace('ó', 'o')
  119. c = c.replace('ú', 'u')
  120. c = c.replace('ü', 'u')
  121. c = c.replace('ñ', 'n~')
  122. c = c.replace('Á', 'A')
  123. c = c.replace('É', 'E')
  124. c = c.replace('Í', 'I')
  125. c = c.replace('Ó', 'O')
  126. c = c.replace('Ú', 'U')
  127. c = c.replace('Ü', 'U')
  128. c = c.replace('Ñ', 'N~')
  129. c = c.replace('¿', '')
  130. c = c.replace('¡', '')
  131. f = open(fn, 'w')
  132. f.write(c)
  133. f.close()
  134.  
  135. def es_subtitulo(fn):
  136. return fn.endswith('sub') or fn.endswith('srt')
  137.  
  138. def add_subtitle(package_fn):
  139. video_fn = look_for_video_file()
  140.  
  141. for fn in os.listdir('.'):
  142. if fn == video_fn or fn == package_fn:
  143. continue
  144. if es_subtitulo(fn):
  145. try:
  146. #os.remove(fn)
  147. pass
  148. except OSError:
  149. print('Warning: no se puede borrar %s' % (fn,))
  150.  
  151. ext = extension_is(package_fn, package)
  152. if ext == 'zip':
  153. shutil.move(package_fn, 'tmp.zip')
  154. os.system('unzip tmp.zip')
  155. #os.remove('tmp.zip')
  156. elif ext == 'rar':
  157. shutil.move(package_fn, 'tmp.rar')
  158. os.system('unrar x tmp.rar')
  159. #os.remove('tmp.rar')
  160.  
  161. nsubs = 0
  162. for fn in os.listdir('.'):
  163. if extension_is(fn, subtitle):
  164. subtitle_fn = fn
  165. nsubs += 1
  166.  
  167. if nsubs == 0:
  168. raise NoHaySubtitulosException()
  169. #sys.stderr.write('No hay subtitulos.\n')
  170. #sys.exit(1)
  171. elif nsubs > 1:
  172. sys.stderr.write('Ojo: hay varios subtitulos.\n')
  173.  
  174. for fn in os.listdir('.'):
  175. if fn not in [video_fn, subtitle_fn] and not extension_is(fn, subtitle):
  176. try:
  177. #os.remove(fn)
  178. pass
  179. except OSError:
  180. print('Warning: no se puede borrar %s' % (fn,))
  181.  
  182. subtitle_fn2 = subtitle_for(video_fn)
  183. os.rename(subtitle_fn, subtitle_fn2)
  184. fix(subtitle_fn2)
  185.  
  186. f = open('do.sh', 'w')
  187. f.write('#!/bin/bash\n')
  188. f.write('mplayer -af volume=20.1:0 "%s" # -subdelay 1\n' % (video_fn,))
  189. f.write('\n')
  190. f.write('# Black box around subtitles\n')
  191. f.write('# -sub-bg-alpha 10 -sub-bg-color 0\n')
  192. f.write('# Expand lower section\n')
  193. f.write('# -sub-bg-alpha 10 -sub-bg-color 0 -vf expand=0:-100:0:0\n')
  194. f.close()
  195.  
  196. os.system('chmod +x do.sh')
  197. os.system('./do.sh')
  198.  
  199. if len(sys.argv) == 2:
  200. add_subtitle(sys.argv[1])
  201. elif len(sys.argv) == 1:
  202. auto_add_subtitle()
  203. else:
  204. sys.stderr.write('Uso: %s [archivo_del_subtitulo]\n' % (sys.argv[0],))
  205. sys.exit(1)
Add Comment
Please, Sign In to add comment