From b3b77c9341bf6433ba301f39841757d31055ffdb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 3 May 2010 14:40:15 +0200 Subject: Added a 5 second timeout for image fetching in the url_titles module. --- modules/url_titles.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/url_titles.py b/modules/url_titles.py index 7297d4d..6fa48f2 100644 --- a/modules/url_titles.py +++ b/modules/url_titles.py @@ -4,7 +4,7 @@ info = { 'description': 'Fetches the title tags off of URLs.', } -import re, urllib2, htmlentitydefs, gzip, cStringIO, spotimeta +import re, urllib2, htmlentitydefs, gzip, cStringIO, spotimeta, time from PIL import ImageFile class Module: @@ -68,6 +68,7 @@ class Module: if any([x in url for x in ('open.spotify.com', 'spotify:track:', 'spotify:artist:', 'spotify:album:')]): titles.append(self.spotify(url).encode('utf8')) continue + t = time.time() try: u = urllib2.urlopen(url) except: @@ -88,7 +89,7 @@ class Module: s = '' m = None buf = u.read(1024) - while buf: + while buf and time.time() - t < 5.0: s += buf m = self.re_title.search(s) if m: @@ -106,15 +107,20 @@ class Module: return '%s %s' % (str(round(size/float(lim/2**10), 2)), suf) p = ImageFile.Parser() size = 0 - while 1: + while time.time() - t < 5.0: s = u.read(1024) size += len(s) if not s: break p.feed(s) - im = p.close() - titles.append('%s image: %dx%d (%s)' % ((im.format,) + tuple(im.size) + (pretty_size(size),))) - del im + try: + im = None + im = p.close() + titles.append('%s image: %dx%d (%s)' % ((im.format,) + tuple(im.size) + (pretty_size(size),))) + except: + pass + finally: + del im u.close() if len(titles) == 1: s = format_text(titles[0]) -- cgit v1.2.3