From: Thomas Jansen Date: Sun, 4 Oct 2009 08:55:56 +0000 (+0200) Subject: leoslyrics plugin: handle parser exceptions X-Git-Tag: release-0.16~168 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2b21735058e33356f3755a3c2ab3f0b14e908e01;p=ncmpc.git leoslyrics plugin: handle parser exceptions Should fix bug #2286. Added exception handling for both calls of the SAX XML parser. Also removed trailing newlines from the result to keep us from scrolling down further than the last line. --- diff --git a/lyrics/03-leoslyrics.py b/lyrics/03-leoslyrics.py index b55f0ec..2a40f37 100755 --- a/lyrics/03-leoslyrics.py +++ b/lyrics/03-leoslyrics.py @@ -21,7 +21,7 @@ # Load lyrics from leoslyrics.com # -from sys import argv, exit +from sys import argv, exit, stderr from urllib import urlencode, urlopen from xml.sax import make_parser, SAXException from xml.sax.handler import ContentHandler @@ -47,7 +47,11 @@ def search(artist, title): handler = SearchContentHandler() parser = make_parser() parser.setContentHandler(handler) - parser.parse(f) + try: + parser.parse(f) + except SAXException: + stderr.write("Failed to parse the search result!\n") + exit(1) return handler.hid class LyricsContentHandler(ContentHandler): @@ -75,10 +79,14 @@ def lyrics(hid): handler = LyricsContentHandler() parser = make_parser() parser.setContentHandler(handler) - parser.parse(f) + try: + parser.parse(f) + except SAXException: + stderr.write("Failed to parse the lyrics!\n") + exit(1) return handler.text hid = search(argv[1], argv[2]) if hid is None: exit(69) -print lyrics(hid).encode('utf-8') +print lyrics(hid).encode('utf-8').rstrip()