本文整理汇总了Python中util.http.quote_plus函数的典型用法代码示例。如果您正苦于以下问题:Python quote_plus函数的具体用法?Python quote_plus怎么用?Python quote_plus使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quote_plus函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: weather
def weather(inp, bot=None, reply=None, nick=None, db=None):
'.weather <location> [dontsave] -- gets weather data for location'
request = 'http://api.worldweatheronline.com/free/v1/weather.ashx?key={0}&q={1}&num_of_days=1&format=json'
loc = inp
# init db
db.execute("create table if not exists weather(nick primary key, loc)")
dontsave = inp.endswith(' dontsave')
if dontsave:
loc = inp[:-9]
loc = inp.lower().strip()
# no address given
if not loc:
loc = db.execute("select loc from weather where nick=lower(?)",
(nick,)).fetchone()
if not loc:
return weather.__doc__
loc = loc[0]
try:
j = http.get_json(request.format(bot.config['api_keys']['wwo'], http.quote_plus(loc)))
except IOError, e:
print e
return 'um the api broke or something'
示例2: weather
def weather(inp, nick=None, reply=None, db=None, notice=None):
"weather | <location> [save] | <@ user> -- Gets weather data for <location>."
save = True
if '@' in inp:
save = False
nick = inp.split('@')[1].strip()
loc = database.get(db,'users','location','nick',nick)
if not loc: return "No location stored for {}.".format(nick.encode('ascii', 'ignore'))
else:
loc = database.get(db,'users','location','nick',nick)
if not inp:
if not loc:
notice(weather.__doc__)
return
else:
# if not loc: save = True
if " dontsave" in inp:
inp = inp.replace(' dontsave','')
save = False
loc = inp.replace(' ','_') #.split()[0]
location = http.quote_plus(loc)
# location = location.replace(',','').replace(' ','-')
# now, to get the actual weather
try:
data = get_weather('%s' % location)
except KeyError:
return "Could not get weather for that location."
if location and save: database.set(db,'users','location',location,'nick',nick)
# put all the stuff we want to use in a dictionary for easy formatting of the output
weather_data = {
"place": data['location']['city'],
"conditions": data['item']['condition']['text'],
"temp_f": data['item']['condition']['temp'],
"temp_c": data['item']['condition']['temp_c'],
"humidity": data['atmosphere']['humidity'],
"wind_kph": data['wind']['speed_kph'],
"wind_mph": data['wind']['speed'],
"wind_text": data['wind']['text'],
"forecast": data['item']['forecast'][0]['text'],
"high_f": data['item']['forecast'][0]['high'],
"high_c": data['item']['forecast'][0]['high_c'],
"low_f": data['item']['forecast'][0]['low'],
"low_c": data['item']['forecast'][0]['low_c'],
"_forecast": data['item']['forecast'][1]['text'],
"_high_f": data['item']['forecast'][1]['high'],
"_high_c": data['item']['forecast'][1]['high_c'],
"_low_f": data['item']['forecast'][1]['low'],
"_low_c": data['item']['forecast'][1]['low_c']
}
reply("\x02{place}\x02 - \x02Current:\x02 {conditions}, {temp_f}F/{temp_c}C, Humidity: {humidity}%, " \
"Wind: {wind_kph}KPH/{wind_mph}MPH {wind_text}, \x02Today:\x02 {forecast}, " \
"High: {high_f}F/{high_c}C, Low: {low_f}F/{low_c}C. " \
"\x02Tomorrow:\x02 {_forecast}, High: {_high_f}F" \
"/{_high_c}C, Low: {_low_f}F/{_low_c}C.".format(**weather_data))
示例3: rottentomatoes
def rottentomatoes(inp, bot=None):
'.rt <title> -- gets ratings for <title> from Rotten Tomatoes'
api_key = bot.config.get("api_keys", {}).get("rottentomatoes", None)
if not api_key:
return None
title = inp.strip()
results = http.get_json(movie_search_url % (http.quote_plus(title), api_key))
if results['total'] > 0:
movie = results['movies'][0]
title = movie['title']
id = movie['id']
critics_score = movie['ratings']['critics_score']
audience_score = movie['ratings']['audience_score']
url = movie['links']['alternate']
if critics_score != -1:
reviews = http.get_json(movie_reviews_url%(id, api_key))
review_count = reviews['total']
fresh = critics_score * review_count / 100
rotten = review_count - fresh
return response % (title, critics_score, fresh, rotten, audience_score, url)
示例4: define
def define(text):
"""define <word> -- Fetches definition of <word>.
:type text: str
"""
url = 'http://ninjawords.com/'
h = http.get_html(url + http.quote_plus(text))
definition = h.xpath('//dd[@class="article"] | '
'//div[@class="definition"] |'
'//div[@class="example"]')
if not definition:
return 'No results for ' + text + ' :('
result = format_output(h, definition, True)
if len(result) > 450:
result = format_output(h, definition, False)
if len(result) > 450:
result = result[:result.rfind(' ', 0, 450)]
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...'
return result
示例5: define
def define(inp):
""".define/.dict <word> - fetches definition of <word>."""
url = 'http://ninjawords.com/'
try:
h = http.get_html(url + http.quote_plus(inp))
except:
return "API error; please try again in a few minutes."
definition = h.xpath('//dd[@class="article"] | '
'//div[@class="definition"] |'
'//div[@class="example"]')
if not definition:
return 'No results for ' + inp
def format_output(show_examples):
result = '%s: ' % h.xpath('//dt[@class="title-word"]/a/text()')[0]
correction = h.xpath('//span[@class="correct-word"]/text()')
if correction:
result = 'definition for "%s": ' % correction[0]
sections = []
for section in definition:
if section.attrib['class'] == 'article':
sections += [[section.text_content() + ': ']]
elif section.attrib['class'] == 'example':
if show_examples:
sections[-1][-1] += ' ' + section.text_content()
else:
sections[-1] += [section.text_content()]
for article in sections:
result += article[0]
if len(article) > 2:
result += ' '.join('%d. %s' % (n + 1, section)
for n, section in enumerate(article[1:]))
else:
result += article[1] + ' '
synonyms = h.xpath('//dd[@class="synonyms"]')
if synonyms:
result += synonyms[0].text_content()
result = re.sub(r'\s+', ' ', result)
result = re.sub('\xb0', '', result)
return result
result = format_output(True)
if len(result) > 450:
result = format_output(False)
if len(result) > 450:
result = result[:result.rfind(' ', 0, 450)]
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...'
return result
示例6: lmgtfy
def lmgtfy(inp, bot=None):
"lmgtfy [phrase] - Posts a google link for the specified phrase"
link = "http://lmgtfy.com/?q=%s" % http.quote_plus(inp)
try:
return web.isgd(link)
except (web.ShortenError, http.HTTPError):
return link
示例7: lmgtfy
def lmgtfy(inp):
"""lmgtfy [phrase] - Posts a google link for the specified phrase"""
link = "http://lmgtfy.com/?q={}".format(http.quote_plus(inp))
try:
return web.isgd(link)
except (web.ShortenError, http.HTTPError):
return link
示例8: define
def define(inp):
"""define <word> -- Fetches definition of <word>."""
url = 'http://ninjawords.com/'
h = http.get_html(url + http.quote_plus(inp))
definition = h.xpath('//dd[@class="article"] | '
'//div[@class="definition"] |'
'//div[@class="example"]')
if not definition:
return 'No results for ' + inp + ' :('
def format_output(show_examples):
result = '{}: '.format(h.xpath('//dt[@class="title-word"]/a/text()')[0])
correction = h.xpath('//span[@class="correct-word"]/text()')
if correction:
result = 'Definition for "{}": '.format(correction[0])
sections = []
for section in definition:
if section.attrib['class'] == 'article':
sections += [[section.text_content() + ': ']]
elif section.attrib['class'] == 'example':
if show_examples:
sections[-1][-1] += ' ' + section.text_content()
else:
sections[-1] += [section.text_content()]
for article in sections:
result += article[0]
if len(article) > 2:
result += u' '.join(u'{}. {}'.format(n + 1, section)
for n, section in enumerate(article[1:]))
else:
result += article[1] + ' '
synonyms = h.xpath('//dd[@class="synonyms"]')
if synonyms:
result += synonyms[0].text_content()
result = re.sub(r'\s+', ' ', result)
result = re.sub('\xb0', '', result)
return result
result = format_output(True)
if len(result) > 450:
result = format_output(False)
if len(result) > 450:
result = result[:result.rfind(' ', 0, 450)]
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...'
return result
示例9: time_cmd
def time_cmd(inp, reply=None, bot=None):
if inp == 'lodon':
inp = 'london'
if inp == 'my life':
return 'no:no'
request = 'http://api.worldweatheronline.com/free/v1/tz.ashx?key={0}&q={1}&format=json'
j = http.get_json(request.format(bot.config['api_keys']['wwo'], http.quote_plus(inp)))
j = j['data']['time_zone'][0]
utc = j['utcOffset'].split('.')[0]
utc = '+' + utc if float(utc) >= 0 else utc
return '{0} (UTC {1})'.format(j['localtime'], utc)
示例10: xkcd_search
def xkcd_search(term):
search_term = http.quote_plus(term)
soup = http.get_soup("http://www.ohnorobot.com/index.pl?s={}&Search=Search&"
"comic=56&e=0&n=0&b=0&m=0&d=0&t=0".format(search_term))
result = soup.find('li')
if result:
url = result.find('div', {'class': 'tinylink'}).text
xkcd_id = url[:-1].split("/")[-1]
print(xkcd_id)
return xkcd_info(xkcd_id, url=True)
else:
return "No results found!"
示例11: define
def define(inp):
"define <word> -- Fetches definition of <word>."
url = "http://ninjawords.com/"
h = http.get_html(url + http.quote_plus(inp))
definition = h.xpath('//dd[@class="article"] | ' '//div[@class="definition"]')
if not definition:
return "No results for " + inp + " :("
def format_output(show_examples):
result = "%s: " % h.xpath('//dt[@class="title-word"]/a/text()')[0]
correction = h.xpath('//span[@class="correct-word"]/text()')
if correction:
result = 'Definition for "%s": ' % correction[0]
sections = []
for section in definition:
if section.attrib["class"] == "article":
sections += [[section.text_content() + ": "]]
elif section.attrib["class"] == "example":
if show_examples:
sections[-1][-1] += " " + section.text_content()
else:
sections[-1] += [section.text_content()]
for article in sections:
result += article[0]
if len(article) > 2:
result += " ".join("%d. %s" % (n + 1, section) for n, section in enumerate(article[1:]))
else:
result += article[1] + " "
synonyms = h.xpath('//dd[@class="synonyms"]')
if synonyms:
result += synonyms[0].text_content()
result = re.sub(r"\s+", " ", result)
result = re.sub("\xb0", "", result)
return result
result = format_output(True)
if len(result) > 450:
result = format_output(False)
if len(result) > 450:
result = result[: result.rfind(" ", 0, 450)]
result = re.sub(r"[^A-Za-z]+\.?$", "", result) + " ..."
return result
示例12: weather
def weather(inp, nick=None, reply=None, db=None, notice=None):
"weather | <location> [save] | <@ user> -- Gets weather data for <location>."
save = False
if '@' in inp:
save = False
nick = inp.split('@')[1].strip()
loc = database.get(db,'users','location','nick',nick)
if not loc: return "No location stored for {}.".format(nick.encode('ascii', 'ignore'))
else:
loc = database.get(db,'users','location','nick',nick)
if not inp:
if not loc:
notice(weather.__doc__)
return
else:
# if not loc: save = True
if " save" in inp:
inp = inp.replace(' save','')
save = True
loc = inp.replace(' ','_') #.split()[0]
location = http.quote_plus(loc)
# location = location.replace(',','').replace(' ','-')
# now, to get the actual weather
try:
q ={
'q': 'select title, units.temperature, item.forecast from weather.forecast where woeid in (select woeid from geo.places where text="'+ location+'") limit 1',
'format': 'json',
'env': 'store://datatables.org/alltableswithkeys'
}
result = query(q)
data = json.loads(result)
weather = data["query"]["results"]["channel"]
average_F = float((int(weather['item']['forecast']['high']) + int(weather['item']['forecast']['low']))/2)
average_C = round(float((average_F - 32) * (5.0/9.0)), 2)
except KeyError:
return "Could not get weather for that location."
if location and save: database.set(db,'users','location',location,'nick',nick)
# put all the stuff we want to use in a dictionary for easy formatting of the output
weather_data = {
'title': weather["title"].replace("Yahoo! Weather -", ""),
'current': weather['item']['forecast']['text'],
'temp_f': average_F,
'temp_c': average_C
}
reply("\x02{title}\x02 - \x02Current:\x02 {current}, {temp_f}F/{temp_c}C".format(**weather_data))
示例13: wolframalpha
def wolframalpha(inp, bot=None):
"wa <query> -- Computes <query> using Wolfram Alpha."
api_key = bot.config.get("api_keys", {}).get("wolframalpha", None)
if not api_key:
return "error: missing api key"
url = 'http://api.wolframalpha.com/v2/query?format=plaintext'
result = http.get_xml(url, input=inp, appid=api_key)
# get the URL for a user to view this query in a browser
query_url = "http://www.wolframalpha.com/input/?i=" + \
http.quote_plus(inp.encode('utf-8'))
try:
short_url = web.isgd(query_url)
except (web.ShortenError, http.HTTPError):
short_url = query_url
pod_texts = []
for pod in result.xpath("//pod[@primary='true']"):
title = pod.attrib['title']
if pod.attrib['id'] == 'Input':
continue
results = []
for subpod in pod.xpath('subpod/plaintext/text()'):
subpod = subpod.strip().replace('\\n', '; ')
subpod = re.sub(r'\s+', ' ', subpod)
if subpod:
results.append(subpod)
if results:
pod_texts.append(title + ': ' + ', '.join(results))
ret = ' - '.join(pod_texts)
if not pod_texts:
return 'No results.'
ret = re.sub(r'\\(.)', r'\1', ret)
def unicode_sub(match):
return unichr(int(match.group(1), 16))
ret = re.sub(r'\\:([0-9a-z]{4})', unicode_sub, ret)
ret = text.truncate_str(ret, 250)
if not ret:
return 'No results.'
return "%s - %s" % (ret, short_url)
示例14: get_status
def get_status(name):
""" takes a name and returns status """
try:
name_encoded = http.quote_plus(name)
response = http.get(NAME_URL.format(name_encoded))
except (http.URLError, http.HTTPError) as e:
raise McuError("Could not get name status: {}".format(e))
if "OK" in response:
return "free"
elif "TAKEN" in response:
return "taken"
elif "invalid characters" in response:
return "invalid"
示例15: get_wiki_article
def get_wiki_article(inp):
# using scraping instead of the wikidot api because it sucks
# it doesn't have a text search, though the site might just be using the api method to select tags anyway
results = http.get_html(search_url % http.quote_plus(inp))
try:
result = results.xpath(result_xpath)[0]
page_url = result.values()[0]
except IndexError:
return "No Results"
title = result.text_content()
return "%s -- %s" % (title, page_url)