Created
November 21, 2011 12:53
-
-
Save GuilhermeBarile/1382548 to your computer and use it in GitHub Desktop.
Extract torrent/subtitles from rar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
#coding: utf-8 | |
TRANSMISSION_HOST='xbmc' | |
DOWNLOAD_DIR='/Users/guigouz/tmp/Downloads' | |
# | |
# Processa um arquivo rar, inicia o download do filme e copia a legenda junto com o avi | |
# | |
import os, sys, rarfile, transmissionrpc, base64, os.path | |
rf = rarfile.RarFile(sys.argv[1]) | |
c = transmissionrpc.Client(TRANSMISSION_HOST) | |
torrent = None | |
subtitles = [] | |
# processa os arquivos do rar | |
for f in rf.infolist(): | |
if f.filename.endswith('.torrent'): | |
torrent = c.add(base64.b64encode(rf.read(f))) | |
elif f.filename.endswith('.srt'): | |
subtitles.append(f) | |
if not torrent: | |
print 'Torrent nao encontrado no arquivo' | |
exit | |
# se o arquivo possui legendas, copiar para o diretório correspondente | |
if len(subtitles): | |
torrent_id = torrent.keys()[0] | |
files = c.get_files(torrent_id)[torrent_id].values() | |
videos = [] | |
for f in files: | |
if f['name'].endswith('.avi') or f['name'].endswith('.mkv'): | |
videos.append(f['name']) | |
if not len(videos): | |
print 'Nenhum vídeo encontrado no torrent' | |
exit | |
# TODO verificar se as legendas não conflitam com algum arquivo do torrent | |
# copiar as legendas para junto dos vídeos | |
dest = DOWNLOAD_DIR + '/' + os.path.dirname(videos[0]) | |
for s in subtitles: | |
print 'Copiada legenda %s para %s' % (s.filename, dest) | |
rf.extract(s, dest) | |
# se temos somente uma legenda e um vídeo, renomear a legenda para o nome do vídeo | |
if len(subtitles) == 1 and len(videos) == 1: | |
subtitle_dest = os.path.basename(videos[0])[0:-4] + '.srt' | |
if subtitles[0].filename != subtitle_dest: | |
os.rename(dest + '/' + subtitles[0].filename, dest + '/' + subtitle_dest) | |
print 'Legenda renomeada de %s para %s' % (subtitles[0].filename, subtitle_dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment