def keyword_k(self, params=None, **kwargs): """Retrieve kernel.org Bugzilla bug information (ex: K12345)""" if params: params = utils.ensure_int(params) if not params: return query = urllib.urlencode({"id": params}) url = "http://bugzilla.kernel.org/show_bug.cgi?%s" % query response = self.irc.fetch_url(url, self.name) if not response or not isinstance(params, int): return soup = BeautifulSoup(response.read()) desc = utils.decode_entities(soup.head.title.string) try: status = soup.find("span", {"id": "static_bug_status"}).string.strip().capitalize() assignee = utils.decode_entities(soup.findAll("span", {"class": "vcard"})[0].contents[0].string) self.irc.reply("Kernel.org %s [Status: %s, Assignee: %s] %s" % (desc, status, assignee, url)) except TypeError: return
def keyword_k(self, params=None, **kwargs): """Retrieve kernel.org Bugzilla bug information (ex: K12345)""" if params: params = utils.ensure_int(params) if not params: return query = urllib.urlencode({"id": params}) url = "http://bugzilla.kernel.org/show_bug.cgi?%s" % query response = self.irc.fetch_url(url, self.name) if not response or not isinstance(params, int): return soup = BeautifulSoup(response.read()) desc = utils.decode_entities(soup.head.title.string) try: status = soup.find("span", { "id": "static_bug_status" }).string.strip().capitalize() assignee = utils.decode_entities( soup.findAll("span", {"class": "vcard"})[0].contents[0].string) self.irc.reply("Kernel.org %s [Status: %s, Assignee: %s] %s" % (desc, status, assignee, url)) except TypeError: return
def urban(self, params=None, **kwargs): """Search Urban Dictionary (ex: .urban <query>)""" if params: query = urllib.urlencode({"term": params}) url = "http://www.urbandictionary.com/define.php?%s" % query response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) results = soup.findAll("div", {"class": "definition"}) urban = "" if len(results): urban = " ".join(str(x) for x in soup.findAll( "div", {"class": "definition"})[0].contents) if len(urban) > 0: for i, line in enumerate(urban.split("<br/>")): if i <= 4: self.irc.reply(utils.decode_entities(line)) else: self.irc.reply("[...] %s" % url) break else: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.urban.__doc__)
def urban(self, params=None, **kwargs): """Search Urban Dictionary (ex: .urban <query>)""" if params: query = urllib.urlencode({"term": params}) url = "http://www.urbandictionary.com/define.php?%s" % query response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) results = soup.findAll("div", {"class": "definition"}) urban = "" if len(results): urban = " ".join( str(x) for x in soup.findAll( "div", {"class": "definition"})[0].contents) if len(urban) > 0: for i, line in enumerate(urban.split("<br/>")): if i <= 4: self.irc.reply(utils.decode_entities(line)) else: self.irc.reply("[...] %s" % url) break else: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.urban.__doc__)
def imdb(self, params=None, **kwargs): """Search IMDb (ex: .imdb <query>)""" if params: query = urllib.urlencode({"q": params}) url = "http://www.imdb.com/find?s=all&%s" % query response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) results = soup.findAll("td", {"valign": "top"}) i = 0 for result in results: if len(result) > 3 and len(result.contents[2].attrs) > 0: id = result.contents[2].attrs[0][1] title = utils.decode_entities( result.contents[2].contents[0]) year = result.contents[2].nextSibling.strip()[0:6] if not title.startswith("aka") and len(year): self.irc.reply("%s %s: http://www.imdb.com%s" % (title, year, id)) i += 1 elif i >= 4: break if i == 0: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.imdb.__doc__)
def imdb(self, params=None, **kwargs): """Search IMDb (ex: .imdb <query>)""" if params: query = urllib.urlencode({"q": params}) url = "http://www.imdb.com/find?s=all&%s" % query response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) results = soup.findAll("td", {"valign": "top"}) i = 0 for result in results: if len(result) > 3 and len(result.contents[2].attrs) > 0: id = result.contents[2].attrs[0][1] title = utils.decode_entities( result.contents[2].contents[0]) year = result.contents[2].nextSibling.strip()[0:6] if not title.startswith("aka") and len(year): self.irc.reply("%s %s: http://www.imdb.com%s" % ( title, year, id)) i += 1 elif i >= 4: break if i == 0: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.imdb.__doc__)
def grouphug(self, params=None, **kwargs): """Display a random Group Hug (ex: .grouphug)""" url = "http://grouphug.us/random" response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) grouphug = utils.decode_entities( soup.findAll(id=re.compile("node-\d+"))[2].p.contents[0]) self.irc.reply(grouphug)
def grouphug(self, params=None, **kwargs): """Display a random Group Hug (ex: .grouphug)""" url = "http://grouphug.us/random" response = self.irc.fetch_url(url, self.name) html = response.read() r = re.compile("<div class=\"content\">\n\s+<p>(.*)</p>\n\s+</div>") m = r.search(html) if m: line = utils.decode_entities(m.group(1)) self.irc.reply(line) else: self.irc.reply("Unable to parse Group Hug data")
def lastnight(self, params=None, **kwargs): """Display a random Text From Last Night (ex: .lastnight)""" url = ("http://www.textsfromlastnight.com/" "Random-Texts-From-Last-Night.html") response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) lastnight = utils.decode_entities( soup.findAll(href=re.compile( "/Text-Replies-\d+.html"))[0].contents[0]) self.irc.reply(lastnight)
def _find_title(self, url): """Find the title of a given URL""" if not url.startswith("http://"): url = "http://" + url response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) if soup.head: title = utils.decode_entities(soup.head.title.string) self.irc.reply(title) else: self.irc.reply("No title found for %s" % url)
def _find_title(self, url): """Find the title of a given URL""" if not url.startswith("http://"): url = "http://" + url response = self.irc.fetch_url(url, self.name).read() response = re.sub("\n", "", response) response = re.sub(" +", "", response) r = re.compile("<title>(.*)</title>") m = r.search(response) if m: title = utils.decode_entities(m.group(1)) self.irc.reply(title) else: self.irc.reply("No title found for %s" % url)
def twitter(self, params=None, **kwargs): """Search Twitter (ex: .twitter <query>)""" if params: query = urllib.urlencode({"q": params, "rpp": 4}) url = "http://search.twitter.com/search.json?%s" % query response = self.irc.fetch_url(url, self.name) json = simplejson.loads(response.read()) results = json["results"] if results: for r in results: self.irc.reply("@%s: %s" % ( r["from_user"], utils.decode_entities( r["text"].encode("ascii", "ignore")))) else: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.twitter.__doc__)
def _find_title(self, url): """Find the title of a given URL""" if not url.startswith(("http://", "https://")): url = "http://" + url response = self.irc.fetch_url(url, self.name) if not response: return soup = BeautifulSoup(response.read()) if soup.head: title = utils.decode_entities(soup.head.title.string) content_type = response.headers.get("Content-Type").split(";", 1)[0] content_size = response.headers.get("Content-Length") content_size = content_size + " bytes" if content_size else "N/A" self.irc.reply("%s (%s, %s)" % (title, content_type, content_size)) else: self.irc.reply("No title found for %s" % url)
def twitter(self, params=None, **kwargs): """Search Twitter (ex: .twitter <query>)""" if params: query = urllib.urlencode({"q": params, "rpp": 4}) url = "http://search.twitter.com/search.json?%s" % query response = self.irc.fetch_url(url, self.name) if not response: return json_obj = json.loads(response.read()) results = json_obj["results"] if results: for r in results: self.irc.reply("@%s: %s" % (r["from_user"], utils.decode_entities(r["text"].encode( "ascii", "ignore")))) else: self.irc.reply("No results found: '%s'" % params) else: self.irc.reply(self.twitter.__doc__)
def urban(self, params=None, **kwargs): """Search Urban Dictionary (ex: .urban <query>)""" if params: query = urllib.urlencode({"term": params}) url = "http://www.urbandictionary.com/define.php?%s" % query response = self.irc.fetch_url(url, self.name) html = response.read() if re.search("<i>%s</i>\nisn't defined" % params, html): self.irc.reply("No results found: '%s'" % params) else: r = (re.compile("<div class=\"definition\">(.*)</div>" "<div class=\"example\">")) m = r.search(html) for i, line in enumerate(m.group(1).split("<br/>")): if i <= 4: line = utils.decode_entities(line) self.irc.reply(line) else: self.irc.reply("[...] %s" % url) break else: self.irc.reply(self.urban.__doc__)
def test_decode_entities_16(self): test_str = "–" self.assertEqual(utils.decode_entities(test_str), "-")
def test_decode_entities_15(self): test_str = "&" self.assertEqual(utils.decode_entities(test_str), "&")
def test_decode_entities_14(self): test_str = "'" self.assertEqual(utils.decode_entities(test_str), "'")
def test_decode_entities_13(self): test_str = """ self.assertEqual(utils.decode_entities(test_str), '"')
def test_decode_entities_11(self): test_str = "<]*?>" self.assertEqual(utils.decode_entities(test_str), "")
def test_decode_entities_10(self): test_str = "<[lol^>" self.assertEqual(utils.decode_entities(test_str), "")
def test_decode_entities(self): test_str = "<foo>@&bar&@</foo>" self.assertEqual(utils.decode_entities(test_str), "@&bar&@")
def test_decode_entities_8(self): test_str = "”" self.assertEqual(utils.decode_entities(test_str), '"')
def test_decode_entities_3(self): test_str = "&" self.assertEqual(utils.decode_entities(test_str), "&")
def test_decode_entities_4(self): test_str = """ self.assertEqual(utils.decode_entities(test_str), '"')
def test_decode_entities_2(self): test_str = "foo bar" self.assertEqual(utils.decode_entities(test_str), "foo bar")
def test_decode_entities(self): test_str = "<foo>bar</foo>" self.assertEqual(utils.decode_entities(test_str), "bar")
def test_decode_entities_17(self): test_str = "@" self.assertEqual(utils.decode_entities(test_str), "@")
def test_decode_entities_9(self): test_str = "…" self.assertEqual(utils.decode_entities(test_str), "...")
def test_decode_entities_5(self): test_str = "—" self.assertEqual(utils.decode_entities(test_str), "-")
def test_decode_entities_7(self): test_str = "“" self.assertEqual(utils.decode_entities(test_str), "\"")