Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # coding:utf-8:
- import os
- import sys
- import shutil
- import re
- import urllib
- print os.path.dirname(os.path.realpath(__file__))
- video = ['mp4', 'mpg', 'mpeg', 'avi', 'wmv', 'mov', 'mkv', 'm4v']
- subtitle = ['srt', 'sub']
- package = ['zip', 'rar']
- ALLOWED = 'abcdefghijklmnopqrstuvwxyz_.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
- class NoHaySubtitulosException(Exception):
- pass
- def extension_is(fn, exts):
- for ext in exts:
- if fn.lower().endswith('.' + ext):
- return ext
- return False
- def renamed(fn):
- fn2 = ''
- for c in fn:
- if c in ALLOWED:
- fn2 += c
- return fn2
- def look_for_video_file():
- nvideos = 0
- for fn in os.listdir('.'):
- if extension_is(fn, video):
- video_fn = fn
- nvideos += 1
- if nvideos == 0:
- sys.stderr.write('No hay videos.\n')
- sys.exit(1)
- elif nvideos > 1:
- sys.stderr.write('Hay varios videos.\n')
- sys.exit(1)
- video_fn2 = renamed(video_fn)
- shutil.move(video_fn, video_fn2)
- video_fn = video_fn2
- return video_fn
- def subtitle_for(video_fn):
- return '.'.join(video_fn.split('.')[:-1]) + '.sub'
- def ask_yes_no(question):
- while True:
- response = raw_input(question + ' (s/n) ')
- if response in ['s', 'n']:
- return response == 's'
- def auto_add_subtitle():
- video_fn = look_for_video_file()
- prefixes = ['Crazy4TV.com-']
- for prefix in prefixes:
- if video_fn.startswith(prefix):
- video_fn = video_fn[len(prefix):]
- match = re.search('^([' + ALLOWED + ']+)([sS][0-9][0-9][eE][0-9][0-9])', video_fn)
- if match is None:
- season_episode = ''
- sys.stderr.write('Ojo: no reconozco temporada y episodio.\n¡Ya sé, debe ser una película!')
- match = re.search('^([' + ALLOWED + ']+)(20[0-9][0-9]|19[0-9][0-9])', video_fn)
- if match is None:
- name = '.'.join(video_fn.split('.')[:-1])
- else:
- name = match.expand('\\1')
- else:
- name = match.expand('\\1')
- season_episode = match.expand('\\2').lower()
- name = name.replace('.', ' ').replace('_', ' ').replace('-', ' ')
- name = (name.lower().strip() + ' ' + season_episode).strip()
- lookup_name = '+'.join(name.split(' '))
- sys.stderr.write(lookup_name + '\n')
- # Search for subtitles
- contents = urllib.urlopen('http://www.subdivx.com/index.php?q=' + lookup_name + '&accion=5&masdesc=&subtitulos=1&realiza_b=1').read()
- for match in re.findall('bajar.php[?]id=([0-9]+&u=[0-9]+)', contents):
- print('Probando subtitulo %s' % (match,))
- #os.system('rm -f ' + subtitle_for(video_fn))
- contents = urllib.urlopen('http://www.subdivx.com/bajar.php?id=' + match).read()
- f = open('tmp', 'w')
- f.write(contents)
- f.close()
- file_tmp = os.popen('file tmp').read()
- if 'zip' in file_tmp.lower():
- package_fn = 'tmp.zip'
- elif 'rar' in file_tmp.lower():
- package_fn = 'tmp.rar'
- else:
- sys.stderr.write('Tipo de archivo no reconocido.\n')
- continue
- os.system('mv tmp ' + package_fn)
- try:
- add_subtitle(package_fn)
- except NoHaySubtitulosException:
- continue
- if ask_yes_no('El subtitulo esta bien?'):
- return
- sys.stderr.write('No hay mas subtitulos para probar =(.\n')
- def fix(fn):
- f = open(fn, 'r')
- c = f.read()
- f.close()
- c = c.replace('á', 'a')
- c = c.replace('é', 'e')
- c = c.replace('í', 'i')
- c = c.replace('ó', 'o')
- c = c.replace('ú', 'u')
- c = c.replace('ü', 'u')
- c = c.replace('ñ', 'n~')
- c = c.replace('Á', 'A')
- c = c.replace('É', 'E')
- c = c.replace('Í', 'I')
- c = c.replace('Ó', 'O')
- c = c.replace('Ú', 'U')
- c = c.replace('Ü', 'U')
- c = c.replace('Ñ', 'N~')
- c = c.replace('¿', '')
- c = c.replace('¡', '')
- f = open(fn, 'w')
- f.write(c)
- f.close()
- def es_subtitulo(fn):
- return fn.endswith('sub') or fn.endswith('srt')
- def add_subtitle(package_fn):
- video_fn = look_for_video_file()
- for fn in os.listdir('.'):
- if fn == video_fn or fn == package_fn:
- continue
- if es_subtitulo(fn):
- try:
- #os.remove(fn)
- pass
- except OSError:
- print('Warning: no se puede borrar %s' % (fn,))
- ext = extension_is(package_fn, package)
- if ext == 'zip':
- shutil.move(package_fn, 'tmp.zip')
- os.system('unzip tmp.zip')
- #os.remove('tmp.zip')
- elif ext == 'rar':
- shutil.move(package_fn, 'tmp.rar')
- os.system('unrar x tmp.rar')
- #os.remove('tmp.rar')
- nsubs = 0
- for fn in os.listdir('.'):
- if extension_is(fn, subtitle):
- subtitle_fn = fn
- nsubs += 1
- if nsubs == 0:
- raise NoHaySubtitulosException()
- #sys.stderr.write('No hay subtitulos.\n')
- #sys.exit(1)
- elif nsubs > 1:
- sys.stderr.write('Ojo: hay varios subtitulos.\n')
- for fn in os.listdir('.'):
- if fn not in [video_fn, subtitle_fn] and not extension_is(fn, subtitle):
- try:
- #os.remove(fn)
- pass
- except OSError:
- print('Warning: no se puede borrar %s' % (fn,))
- subtitle_fn2 = subtitle_for(video_fn)
- os.rename(subtitle_fn, subtitle_fn2)
- fix(subtitle_fn2)
- f = open('do.sh', 'w')
- f.write('#!/bin/bash\n')
- f.write('mplayer -af volume=20.1:0 "%s" # -subdelay 1\n' % (video_fn,))
- f.write('\n')
- f.write('# Black box around subtitles\n')
- f.write('# -sub-bg-alpha 10 -sub-bg-color 0\n')
- f.write('# Expand lower section\n')
- f.write('# -sub-bg-alpha 10 -sub-bg-color 0 -vf expand=0:-100:0:0\n')
- f.close()
- os.system('chmod +x do.sh')
- os.system('./do.sh')
- if len(sys.argv) == 2:
- add_subtitle(sys.argv[1])
- elif len(sys.argv) == 1:
- auto_add_subtitle()
- else:
- sys.stderr.write('Uso: %s [archivo_del_subtitulo]\n' % (sys.argv[0],))
- sys.exit(1)
Add Comment
Please, Sign In to add comment