Subtitle downloader
Downloading subtitles for movies can be a trivial task. Just when you found the
subtitles the delay is wrong or its in chinese.
Open Subtitles
made as service to make this task simple. Hash the file and connect with xmlrpc
and open subtitles will find the correct file.
I decieded to make this in python... Just for fun. Here is goes my first python
program.
@Parameters
-f The name of the avi file or directory. If -f is not specified
subit will look in the current directory.
-l A comma delimitered string specifying the language of
the subtitles in prioritied order.
Usage: SubIt.py -f "M:\MyAvis" -l dan,eng
Full source download |
Windows exe download
import time
from array import array
import struct
import os
import urllib
import sys
from zipfile import *
from zipfile import *
from xmlrpclib import ServerProxy, Error
from urllib2 import Request, urlopen, URLError, HTTPError
from optparse import OptionParser
server = ServerProxy("http://www.opensubtitles.org/xml-rpc")
token=""
def SubIt():
filename=""
lang=""
movieFound=False
parser = OptionParser()
parser.add_option("-f", help="name of the file or directory to get subtitles for. If omitted SubIt will look for .avi files in the current directory", dest="filename")
parser.add_option("-l", help="comma delimitered string with languages. eg dan,eng. If omitted the default will be english", dest="languages")
(options, args) = parser.parse_args()
if options.filename:
filename = options.filename
if options.languages:
lang=options.languages
else:
lang="eng"
if filename=="" or os.path.isdir(filename) == True:
if(os.path.isdir(filename) == True):
directory = filename
else:
directory=os.getcwd()
for root, dirs, files in os.walk(directory):
for fname in files:
extension = os.path.splitext( fname )[1]
if extension==".avi":
movieFound=True
FindSubtitles(root +"\\"+ fname,lang)
else:
FindSubtitles(filename,lang)
movieFound=True
if not movieFound:
print "No movies found in '"+os.getcwd()+"'"
raw_input("Done... press enter ")
def FindSubtitles(videoname,lang):
print "Contacting www.opensubtitles.org ("+videoname+")"
filename = os.path.dirname(videoname) + "\\" +os.path.splitext(os.path.basename(videoname))[0]+ ".srt"
langs=lang.split(",")
data=GetSubtitles(videoname);
found=False
if data:
for l in langs:
for item in data:
if item['SubLanguageID']== l and not found:
print "Found", item['LanguageName'], "subtitle ..."
zipname=Download(item['ZipDownloadLink'],item['SubFileName'])
print "Extracting subtitle ",filename
Unzip(zipname,filename)
os.remove(zipname)
found=True
else:
print "No Subtitles found"
def GetSubtitles(moviepath ):
token=server.LogIn("","","","SubIt")["token"]
moviebytesize = os.path.getsize(moviepath)
hash=Compute(moviepath)
movieInfo = {'sublanguageid' : 'all','moviehash' : hash, 'moviebytesize' : moviebytesize}
movies=[movieInfo]
data=server.SearchSubtitles(token,movies)['data']
return data
def Compute(name):
try:
longlongformat = 'q' # long long
bytesize = struct.calcsize(longlongformat)
f = file(name, "rb")
filesize = os.path.getsize(name)
hash = filesize
if filesize < 65536 * 2:
return "SizeError"
for x in range(65536/bytesize):
buffer = f.read(bytesize)
(l_value,)= struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
f.seek(max(0,filesize-65536),0)
for x in range(65536/bytesize):
buffer = f.read(bytesize)
(l_value,)= struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF
f.close()
returnedhash = "%016x" % hash
return returnedhash
except(IOError):
return "IOError"
def Unzip(zipname,unzipname):
z = ZipFile(zipname)
for filename in z.namelist():
if os.path.splitext(os.path.basename(filename))[1] == ".srt":
outfile = file(unzipname, "w")
outfile.write(z.read(filename))
outfile.close()
def Download(url,filename):
req = Request(url)
f = urlopen(req)
print "downloading " + url
# Open our local file for writing
local_file = open(filename+".zip", "w" + "b")
#Write to our local file
local_file.write(f.read())
local_file.close()
return filename+".zip"
SubIt()