本文整理汇总了Python中willie.web.decode函数的典型用法代码示例。如果您正苦于以下问题:Python decode函数的具体用法?Python decode怎么用?Python decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: find_title
def find_title(url):
"""Return the title for the given URL."""
try:
content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)
except UnicodeDecodeError:
return # Fail silently when data can't be decoded
# Some cleanup that I don't really grok, but was in the original, so
# we'll keep it (with the compiled regexes made global) for now.
content = title_tag_data.sub(r'<\1title>', content)
content = quoted_title.sub('', content)
start = content.find('<title>')
end = content.find('</title>')
if start == -1 or end == -1:
return
title = web.decode(content[start + 7:end])
title = title.strip()[:200]
title = ' '.join(title.split()) # cleanly remove multiple spaces
# More cryptic regex substitutions. This one looks to be myano's invention.
title = re_dcc.sub('', title)
return title or None
开发者ID:DemonBob,项目名称:willie,代码行数:25,代码来源:url.py
示例2: tr2
def tr2(bot, trigger):
"""Translates a phrase, with an optional language hint."""
command = trigger.group(2).encode('utf-8')
def langcode(p):
return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()
args = ['auto', 'en']
for i in xrange(2):
if not ' ' in command:
break
prefix, cmd = command.split(' ', 1)
if langcode(prefix):
args[i] = prefix[1:]
command = cmd
phrase = command
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')
src, dest = args
if src != dest:
msg, src = translate(phrase, src, dest)
if isinstance(msg, str):
msg = msg.decode('utf-8')
if msg:
msg = web.decode(msg) # msg.replace(''', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
else:
msg = 'The %s to %s translation failed, sorry!' % (src, dest)
bot.reply(msg)
else:
bot.reply('Language guessing failed, so try suggesting one!')
开发者ID:dbolser,项目名称:willie,代码行数:35,代码来源:translate.py
示例3: tr
def tr(bot, trigger):
"""Translates a phrase, with an optional language hint."""
input, output, phrase = trigger.groups()
phrase = phrase.encode('utf-8')
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')
input = input or 'auto'
input = input.encode('utf-8')
output = (output or 'en').encode('utf-8')
if input != output:
msg, input = translate(phrase, input, output)
if isinstance(msg, str):
msg = msg.decode('utf-8')
if msg:
msg = web.decode(msg) # msg.replace(''', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
else:
msg = 'The %s to %s translation failed, sorry!' % (input, output)
bot.reply(msg)
else:
bot.reply('Language guessing failed, so try suggesting one!')
开发者ID:dbolser,项目名称:willie,代码行数:26,代码来源:translate.py
示例4: text
def text(html):
'''html to text dumb converter
cargo-culted from etymology.py'''
html = r_tag.sub('', html)
html = r_whitespace.sub(' ', html)
return web.decode(html.strip())
开发者ID:AndrewSDFoster,项目名称:systemstuff,代码行数:7,代码来源:bookie.py
示例5: duck_search
def duck_search(query):
query = query.replace('!', '')
uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
bytes = web.get(uri)
m = r_duck.search(bytes)
if m:
return web.decode(m.group(1))
开发者ID:Haus1,项目名称:willie,代码行数:7,代码来源:search.py
示例6: find_title
def find_title(url=None, content=None):
"""Return the title for the given URL.
Copy of find_title that allows for avoiding duplicate requests."""
if (not content and not url) or (content and url):
raise ValueError('url *or* content needs to be provided to find_title')
if url:
try:
content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)
except UnicodeDecodeError:
return # Fail silently when data can't be decoded
assert content
# Some cleanup that I don't really grok, but was in the original, so
# we'll keep it (with the compiled regexes made global) for now.
content = title_tag_data.sub(r'<\1title>', content)
content = quoted_title.sub('', content)
start = content.find('<title>')
end = content.find('</title>')
if start == -1 or end == -1:
return
title = web.decode(content[start + 7:end])
title = title.strip()[:200]
title = ' '.join(title.split()) # cleanly remove multiple spaces
# More cryptic regex substitutions. This one looks to be myano's invention.
title = re_dcc.sub('', title)
return title or None
开发者ID:AndrewSDFoster,项目名称:systemstuff,代码行数:31,代码来源:bookie.py
示例7: tr2
def tr2(bot, trigger):
"""Traduce un texto instantaneamente usando el traductor de Google"""
if not trigger.group(2):
return bot.reply('Para usar el traductor ingresa "%tr [idioma origen] [idioma destino] [texto a traducir]"')
command = trigger.group(2)
args = command.split(' ')
phrase = ' '.join(args[2:])
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('La frase debe ser menor a 350 caracteres')
src = args[0]
dest = args[1]
if src != dest:
msg, src = translate(phrase, src, dest)
if sys.version_info.major < 3 and isinstance(msg, str):
msg = msg.decode('utf-8')
if msg:
msg = web.decode(msg) # msg.replace(''', "'")
msg = '%s' % msg
else:
msg = 'La traducción de %s a %s ha fallado, lo siento!' % (src, dest)
bot.reply(msg)
else:
bot.reply('La detección de idioma ha fallado, intenta sugerir uno!')
开发者ID:InteliBOT,项目名称:pyDreamBot,代码行数:27,代码来源:translate.py
示例8: duck_search
def duck_search(query):
query = query.replace('!', '')
uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
bytes = web.get(uri)
if 'web-result"' in bytes: #filter out the adds on top of the page
bytes = bytes.split('web-result"')[1]
m = r_duck.search(bytes)
if m:
return web.decode(m.group(1))
开发者ID:DemonBob,项目名称:willie,代码行数:9,代码来源:search.py
示例9: fucking_dinner
def fucking_dinner(bot, trigger):
txt = trigger.group(2)
url = 'http://www.whatthefuckshouldimakefordinner.com'
if txt == '-v':
url = 'http://whatthefuckshouldimakefordinner.com/veg.php'
page = web.decode(web.get(url))
re_mark = re.compile('<dt><a href="(.*?)" target="_blank">(.*?)</a></dt>')
results = re_mark.findall(page)
if results:
bot.say("WHY DON'T YOU EAT SOME FUCKING: " + results[0][1] +
" HERE IS THE RECIPE: " + results[0][0])
else:
bot.say("I DON'T FUCKING KNOW, EAT PIZZA.")
开发者ID:ezoSresyeK,项目名称:willie-old,代码行数:13,代码来源:dinner.py
示例10: fucking_weather
def fucking_weather(bot, trigger):
text = trigger.group(2)
if not text:
bot.reply("INVALID FUCKING PLACE. PLEASE ENTER A FUCKING ZIP CODE, OR A FUCKING CITY-STATE PAIR.")
return
text = web.quote(text)
try:
page = web.decode(web.get("http://thefuckingweather.com/?where=%s" % (text)))
except:
bot.reply("I COULDN'T ACCESS THE FUCKING SITE.")
return
re_mark = re.compile('<p class="remark">(.*?)</p>')
re_temp = re.compile('<span class="temperature" tempf="\S+">(\S+)</span>')
re_condition = re.compile('<p class="large specialCondition">(.*?)</p>')
re_flavor = re.compile('<p class="flavor">(.*?)</p>')
re_location = re.compile('<span id="locationDisplaySpan" class="small">(.*?)</span>')
temps = re_temp.findall(page)
remarks = re_mark.findall(page)
conditions = re_condition.findall(page)
flavor = re_flavor.findall(page)
new_location = re_location.findall(page)
response = str()
if new_location and new_location[0]:
response += new_location[0] + ': '
if temps:
tempf = float(temps[0])
tempc = (tempf - 32.0) * (5 / 9.0)
response += u'%.1f°F?! %.1f°C?! ' % (tempf, tempc)
if remarks:
response += remarks[0]
else:
response += "THE FUCKING SITE DOESN'T CONTAIN ANY FUCKING INFORMATION ABOUT THE FUCKING WEATHER FOR THE PROVIDED FUCKING LOCATION."
if conditions:
response += ' ' + conditions[0]
if flavor:
response += ' -- ' + flavor[0].replace(' ', ' ')
bot.reply(response)
开发者ID:ezoSresyeK,项目名称:willie-old,代码行数:46,代码来源:fuckingweather.py
示例11: reddit
def reddit(bot, trigger):
"""Display posts from reddit"""
args = trigger.group(2)
if not args:
bot.reply('no, vieja: .reddit subreddit [cantidad]')
return
args = args.split(' ')
subreddit = args[0]
if len(args) == 1:
limit = 1
else:
try:
limit = int(args[1])
except ValueError:
limit = 1
if limit > 4:
limit = 4
r = praw.Reddit(user_agent='Trolo Bot (praw)')
try:
subr = r.get_subreddit(subreddit, fetch=True)
except InvalidSubreddit:
bot.reply('rofl, ese subreddit no existe, locura!')
return
results = subr.get_hot(limit=limit)
out = 'r/%(subreddit)s: %(title)s [%(ups)s/%(downs)s] %(url)s [%(short_link)s]'
for r in results:
output = out % {
'subreddit': r.subreddit,
'title': decode(r.title),
'ups': r.ups,
'downs': r.downs,
'url': r.url,
'short_link': r.short_link
}
bot.say(output)
开发者ID:lardissone,项目名称:willie-modules,代码行数:45,代码来源:reddit.py
示例12: calculate
def calculate(trigger):
q = trigger.encode('utf-8')
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
uri = 'http://www.google.com/ig/calculator?q='
bytes = web.get(uri + web.quote(q))
parts = bytes.split('",')
answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
if answer:
answer = answer.decode('unicode-escape')
answer = ''.join(chr(ord(c)) for c in answer)
answer = answer.decode('utf-8')
answer = answer.replace(u'\xc2\xa0', ',')
answer = answer.replace('<sup>', '^(')
answer = answer.replace('</sup>', ')')
answer = web.decode(answer)
return answer
else: return 'Sorry, no result.'
开发者ID:Shokodemon,项目名称:willie,代码行数:18,代码来源:calc.py
示例13: find_title
def find_title(url):
"""Return the title for the given URL."""
content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes)
if content == None:
#If it cant find any encoding, it could be an image or other file. Show filetype and size.
type = str(headers.get('content-type'))
size = str(headers.get('content-length'))
if size == "None":
size = "Unknown"
elif len(size) > 9:
size = "~"+size[0:-9] +" GB"
elif len(size) > 6:
size = "~"+size[0:-6] +" MB"
elif len(size) > 3:
size = "~"+size[0:-3] +" KB"
title = "Filetype: "+type+". Filesize: "+size+"."
return title
# Some cleanup that I don't really grok, but was in the original, so
# we'll keep it (with the compiled regexes made global) for now.
content = title_tag_data.sub(r'<\1title>', content)
content = quoted_title.sub('', content)
start = content.find('<title>')
end = content.find('</title>')
if start == -1 or end == -1:
return
title = web.decode(content[start + 7:end])
title = title.strip()[:200]
title = ' '.join(title.split()) # cleanly remove multiple spaces
# More cryptic regex substitutions. This one looks to be myano's invention.
title = re_dcc.sub('', title)
return title or None
开发者ID:walliski,项目名称:willie,代码行数:42,代码来源:url.py
示例14: tr2
def tr2(bot, trigger):
"""Translates a phrase, with an optional language hint."""
command = trigger.group(2)
if not command:
return bot.reply("You did not give me anything to translate")
def langcode(p):
return p.startswith(":") and (2 < len(p) < 10) and p[1:].isalpha()
args = ["auto", "en"]
for i in range(2):
if " " not in command:
break
prefix, cmd = command.split(" ", 1)
if langcode(prefix):
args[i] = prefix[1:]
command = cmd
phrase = command
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply("Phrase must be under 350 characters.")
src, dest = args
if src != dest:
msg, src = translate(phrase, src, dest)
if sys.version_info.major < 3 and isinstance(msg, str):
msg = msg.decode("utf-8")
if msg:
msg = web.decode(msg) # msg.replace(''', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
else:
msg = "The %s to %s translation failed, sorry!" % (src, dest)
bot.reply(msg)
else:
bot.reply("Language guessing failed, so try suggesting one!")
开发者ID:DoubleHolo,项目名称:willie,代码行数:38,代码来源:translate.py
示例15: tr
def tr(bot, trigger):
"""Translates a phrase, with an optional language hint."""
in_lang, out_lang, phrase = trigger.groups()
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')
in_lang = in_lang or 'auto'
out_lang = out_lang or 'en'
if in_lang != out_lang:
msg, in_lang = translate(phrase, in_lang, out_lang)
if sys.version_info.major < 3 and isinstance(msg, str):
msg = msg.decode('utf-8')
if msg:
msg = web.decode(msg) # msg.replace(''', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, in_lang, out_lang)
else:
msg = 'The %s to %s translation failed, sorry!' % (in_lang, out_lang)
bot.reply(msg)
else:
bot.reply('Language guessing failed, so try suggesting one!')
开发者ID:josephdunn,项目名称:willie,代码行数:23,代码来源:translate.py
示例16: find_title
def find_title(url):
"""Return the title for the given URL."""
content, headers = web.get(url, return_headers=True, limit_bytes=max_bytes,
dont_decode=True)
content_type = headers.get('Content-Type') or ''
encoding_match = re.match('.*?charset *= *(\S+)', content_type)
# If they gave us something else instead, try that
if encoding_match:
try:
content = content.decode(encoding_match.group(1))
except:
encoding_match = None
# They didn't tell us what they gave us, so go with UTF-8 or fail silently.
if not encoding_match:
try:
content = content.decode('utf-8')
except:
return
# Some cleanup that I don't really grok, but was in the original, so
# we'll keep it (with the compiled regexes made global) for now.
content = title_tag_data.sub(r'<\1title>', content)
content = quoted_title.sub('', content)
start = content.find('<title>')
end = content.find('</title>')
if start == -1 or end == -1:
return
title = web.decode(content[start + 7:end])
title = title.strip()[:200]
title = ' '.join(title.split()) # cleanly remove multiple spaces
# More cryptic regex substitutions. This one looks to be myano's invention.
title = re_dcc.sub('', title)
return title or None
开发者ID:ChaoticWeg,项目名称:kountdown,代码行数:37,代码来源:hurl.py
注:本文中的willie.web.decode函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论