summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-05-03 14:40:15 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-05-03 14:40:15 +0200
commitb3b77c9341bf6433ba301f39841757d31055ffdb (patch)
tree228416e31e287ec4570bc27c4ceb7b89718e440d
parentcd8d98256e7d8cdcdd1f69d4ed9856571274df10 (diff)
Added a 5 second timeout for image fetching in the url_titles module.
-rw-r--r--modules/url_titles.py18
1 files changed, 12 insertions, 6 deletions
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])