From b07a6202b434892e2e3354cd37e5f40fe9a9250d Mon Sep 17 00:00:00 2001 From: dave Date: Sat, 31 Oct 2015 16:42:02 -0700 Subject: [PATCH] Allow titling all content types, refine size display --- pyircbot/modules/LinkTitler.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pyircbot/modules/LinkTitler.py b/pyircbot/modules/LinkTitler.py index db100bf..670ea3a 100755 --- a/pyircbot/modules/LinkTitler.py +++ b/pyircbot/modules/LinkTitler.py @@ -96,12 +96,19 @@ class LinkTitler(ModuleBase): title = self.url_htmltitle(match[0]) if title: self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02" % (sender.nick, title)) - - if "image/" in headers["Content-Type"]: - self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02, %s" % (sender.nick, headers["Content-Type"], str(int(int(headers["Content-Length"])/1024))+"KB" if "Content-Length" in headers else "unknown size")) + else: + # Unknown types, just print type and size + self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02, %s" % (sender.nick, headers["Content-Type"], self.nicesize(int(headers["Content-Length"])) if "Content-Length" in headers else "unknown size")) return + def nicesize(self, numBytes): + "Return kb or plain bytes" + if numBytes > 1024: + return "%skb" % str(int(numBytes/1024)) + else: + return "<1kb" + def url_headers(self, url): "HEAD requests a url to check content type & length, returns something like: {'type': 'image/jpeg', 'size': '90583'}" self.log.debug("url_headers(%s)" % (url,)) @@ -121,7 +128,7 @@ class LinkTitler(ModuleBase): break titleMatches = re.findall(r'([^<]+)', data, re.I) - if len(titleMatches)>0 and resp.status_code==200: + if len(titleMatches)>0:# and resp.status_code==200: h = html.parser.HTMLParser() title = h.unescape(titleMatches[0]).strip() if len(title)>0: