From e4ef17975c4d670099e50da7c232b427ef0c3c05 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Mon, 3 May 2010 21:28:16 +0100 Subject: [PATCH] stop parsing the html page once the parser has left the of the page --- deluge/ui/tracker_icons.py | 7 +++++++ tests/test_tracker_icons.py | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py index eb6f00c3c..9c8a3685b 100644 --- a/deluge/ui/tracker_icons.py +++ b/deluge/ui/tracker_icons.py @@ -256,6 +256,8 @@ class TrackerIcons(Component): parser = FaviconParser() for line in f: parser.feed(line) + if parser.left_head: + break parser.close() f.close() os.remove(page) @@ -410,6 +412,7 @@ class FaviconParser(HTMLParser): """ def __init__(self): self.icons = [] + self.left_head = False HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): @@ -424,6 +427,10 @@ class FaviconParser(HTMLParser): if href and type: self.icons.append((href, type)) + def handle_endtag(self, tag): + if tag == "head": + self.left_head = True + def get_icons(self): """ Returns a list of favicons extracted from the HTML page diff --git a/tests/test_tracker_icons.py b/tests/test_tracker_icons.py index 9a68dc935..702e648fe 100644 --- a/tests/test_tracker_icons.py +++ b/tests/test_tracker_icons.py @@ -36,3 +36,11 @@ class TrackerIconsTestCase(unittest.TestCase): d.addCallback(self.assertNotIdentical, None) d.addCallback(self.assertEquals, icon) return d + + def test_get_ubuntu_ico(self): + # ubuntu.com has inline css which causes HTMLParser issues + icon = TrackerIcon("../ubuntu.png") + d = icons.get("www.ubuntu.com") + d.addCallback(self.assertNotIdentical, None) + d.addCallback(self.assertEquals, icon) + return d