本文整理汇总了Python中pyhole.utils.decode_entities函数的典型用法代码示例。如果您正苦于以下问题:Python decode_entities函数的具体用法?Python decode_entities怎么用?Python decode_entities使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode_entities函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: keyword_k
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
示例2: imdb
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__)
示例3: urban
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__)
示例4: 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)
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)
示例5: 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")
示例6: lastnight
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)
示例7: _find_title
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)
示例8: _find_title
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)
示例9: twitter
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__)
示例10: _find_title
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)
示例11: urban
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__)
示例12: test_decode_entities_14
def test_decode_entities_14(self):
test_str = "'"
self.assertEqual(utils.decode_entities(test_str), "'")
示例13: test_decode_entities_13
def test_decode_entities_13(self):
test_str = """
self.assertEqual(utils.decode_entities(test_str), '"')
示例14: test_decode_entities_11
def test_decode_entities_11(self):
test_str = "<]*?>"
self.assertEqual(utils.decode_entities(test_str), "")
示例15: test_decode_entities_10
def test_decode_entities_10(self):
test_str = "<[lol^>"
self.assertEqual(utils.decode_entities(test_str), "")