From 19c52e22a40d84fb2b96853ad2be74578a9a55fa Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 30 Mar 2007 20:08:19 +0000 Subject: [PATCH] better zipfiles, do not fail if imdb does not return --- scrapeit/imdb.py | 14 ++++++++------ scrapeit/opensubtitles.py | 9 ++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/scrapeit/imdb.py b/scrapeit/imdb.py index d65d95f..3964333 100644 --- a/scrapeit/imdb.py +++ b/scrapeit/imdb.py @@ -390,12 +390,14 @@ def guess(title, director=''): if url.startswith('http://www.imdb.com/title/tt'): return url[28:35] - req = urllib2.Request(imdb_url, None, utils.DEFAULT_HEADERS) - u = urllib2.urlopen(req) - data = u.read() - return_url = u.url - u.close() - + try: + req = urllib2.Request(imdb_url, None, utils.DEFAULT_HEADERS) + u = urllib2.urlopen(req) + data = u.read() + return_url = u.url + u.close() + except: + return None if return_url.startswith('http://www.imdb.com/title/tt'): return return_url[28:35] if data: diff --git a/scrapeit/opensubtitles.py b/scrapeit/opensubtitles.py index 4aeb887..9909a73 100644 --- a/scrapeit/opensubtitles.py +++ b/scrapeit/opensubtitles.py @@ -24,10 +24,13 @@ def getZipFileLink(opensubtitle_id): return "http://www.opensubtitles.org/en/download/sub/%s" % opensubtitle_id def downloadSubtitle(zip_link): + return extractSubtitles(read_url(zip_link)) + +def extractSubtitles(zip_data): srts ={} - zip = StringIO.StringIO() - zip.write(read_url(zip_link)) - zfile = zipfile.ZipFile(zip) + z = StringIO.StringIO() + z.write(zip_data) + zfile = zipfile.ZipFile(z) files = zfile.namelist() def is_not_nfo(file): return not file.endswith('nfo') files = filter(is_not_nfo, files)