opensubtitles

This commit is contained in:
j 2007-03-06 22:07:25 +00:00
parent 0a8d0b2170
commit d3f58b44fd
3 changed files with 46 additions and 2 deletions

View file

@ -9,6 +9,6 @@ import mininova
import thepiratebay import thepiratebay
import torrent import torrent
import rottentomatoes import rottentomatoes
import opensubtitles
__version__ = '1.0.0' __version__ = '1.0.0'

View file

@ -1,5 +1,5 @@
# -*- Mode: Python; -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# -*- Mode: Python; -*-
# vi:si:et:sw=2:sts=2:ts=2 # vi:si:et:sw=2:sts=2:ts=2
from urllib import quote from urllib import quote

44
scrapeit/opensubtitles.py Normal file
View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# -*- Mode: Python; -*-
# vi:si:et:sw=2:sts=2:ts=2
from utils import read_url
import feedparser
import StringIP
import zipfile
def searchSubtitlesByIMDb(imdb, parts = 1, language = "eng"):
url = "http://www.opensubtitles.org/en/search/sublanguageid-%s/subsumcd-%s/subformat-srt/imdbid-%s/rss_2_00" % (language, parts, imdb)
data = read_url(url)
fd = feedparser.parse(data)
os_id = None
if fd.entries:
link = fd.entries[0]['links'][0]['href']
os_id = re.compile('subtitles/(.*?)/').findall(link)
if os_id:
os_id = os_id[0]
return os_id
def getZipFileLink(opensubtitle_id):
return "http://www.opensubtitles.org/en/download/sub/%s" % opensubtitle_id
def downloadSubtitle(zip_link):
srts ={}
zip = StringIO.StringIO()
zip.write(read_url(zip_link))
zfile = zipfile.ZipFile(zip)
files = zfile.namelist()
def is_not_nfo(file): return not file.endswith('nfo')
files = filter(is_not_nfo, files)
if len(files) == 1:
srts[files[0]] = zfile.read(files[0])
else:
for f in zfile.namelist():
if f.endswith('.srt'):
srts[f] = zfile.read(f)
return srts
def downloadSubtitleByID(opensubtitle_id):
zip_file = getZipFileLink(opensubtitle_id)
return downloadSubtitle(zip_file)