本文整理汇总了Python中sopel.web.get函数的典型用法代码示例。如果您正苦于以下问题:Python get函数的具体用法?Python get怎么用?Python get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: woeid_search
def woeid_search(query):
"""
Find the first Where On Earth ID for the given query. Result is the etree
node for the result, so that location data can still be retrieved. Returns
None if there is no result, or the woeid field is empty.
"""
query = 'q=select woeid from geo.places where text="%s"' % query
body = web.get('http://query.yahooapis.com/v1/public/yql?' + query,
dont_decode=True)
parsed = xmltodict.parse(body).get('query')
results = parsed.get('results')
if not results:
return None
elif type(results) is collections.OrderedDict:
place = results.get('place')
elif type(results) is list:
place = results[0].get('place')
else:
return None
if not place:
return None
elif type(place) is collections.OrderedDict:
return place
elif type(place) is list:
return place[0]
else:
return None
示例2: 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
示例3: ytsearch
def ytsearch(bot, trigger):
"""
.youtube <query> - Search YouTube
"""
if not trigger.group(2):
return
uri = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&q=' + trigger.group(2)
raw = web.get('{0}&key={1}'.format(uri, bot.config.google.public_key))
vid = json.loads(raw)['items'][0]['id']['videoId']
uri = 'https://www.googleapis.com/youtube/v3/videos?id=' + vid + '&part=contentDetails,snippet,statistics'
video_info = ytget(bot, trigger, uri)
if video_info is None:
return
title = video_info['snippet']['title']
uploader = video_info['snippet']['channelTitle']
duration = video_info['contentDetails']['duration']
views = video_info['statistics']['viewCount']
likes = video_info['statistics']['likeCount']
dislikes = video_info['statistics']['dislikeCount']
message = '[YT Search] {0} | https://youtu.be/{1} | Duration: {2} | Views: {3} | Uploader: {4} | {5} | {6}'.format(
bold(title), video_info['id'], duration, views, uploader, color(likes, colors.GREEN), color(dislikes, colors.RED))
bot.say(message)
示例4: uptime
def uptime(bot, trigger):
"""
Report the stream uptime.
"""
try:
query_url = 'https://api.twitch.tv/kraken/streams/{0}?api_version=3&client_id={1}'
answer = web.get(query_url.format(trigger.sender[1:],
bot.config.LRB.api_key))
except:
return bot.reply("Couldn't contact the Twitch API servers. :( #BlameTwitch")
try:
data = json.loads(answer)
except:
return bot.reply("The Twitch API returned an invalid object. :( #BlameTwitch")
if data['stream'] != None:
startTime = data['stream']['created_at']
else:
return bot.reply("Stream offline. :(")
f = '%Y-%m-%dT%H:%M:%SZ'
tStart = datetime.datetime.strptime(startTime, f)
now = datetime.datetime.utcnow()
uptime = (now - tStart).seconds
h, r = divmod(uptime, 3600)
m, s = divmod(r, 60)
if h > 0:
return bot.reply('Stream has been online for %s:%s:%s' % (h,m,s))
else:
return bot.reply('Stream has been online for %s:%s' % (m,s))
示例5: weather
def weather(bot, trigger):
""".weather location - Show the weather at the given location."""
location = trigger.group(2)
woeid = ""
if not location:
woeid = bot.db.get_nick_value(trigger.nick, "woeid")
if not woeid:
return bot.msg(
trigger.sender,
"I don't know where you live. "
+ "Give me a location, like .weather London, or tell me where you live by saying .setlocation London, for example.",
)
else:
location = location.strip()
woeid = bot.db.get_nick_value(location, "woeid")
if woeid is None:
first_result = woeid_search(location)
if first_result is not None:
woeid = first_result.get("woeid")
if not woeid:
return bot.reply("I don't know where that is.")
query = "q=select * from weather.forecast where woeid=\"%s\" and u='c'" % woeid
body = web.get("http://query.yahooapis.com/v1/public/yql?" + query, dont_decode=True)
parsed = xmltodict.parse(body).get("query")
results = parsed.get("results")
location = results.get("channel").get("title")
cover = get_cover(results)
temp = get_temp(results)
humidity = get_humidity(results)
wind = get_wind(results)
bot.say("%s: %s, %s, %s, %s" % (location, cover, temp, humidity, wind))
示例6: wikt
def wikt(word):
bytes = web.get(uri % web.quote(word))
bytes = r_ul.sub('', bytes)
mode = None
etymology = None
definitions = {}
for line in bytes.splitlines():
if 'id="Etymology"' in line:
mode = 'etymology'
elif 'id="Noun"' in line:
mode = 'noun'
elif 'id="Verb"' in line:
mode = 'verb'
elif 'id="Adjective"' in line:
mode = 'adjective'
elif 'id="Adverb"' in line:
mode = 'adverb'
elif 'id="Interjection"' in line:
mode = 'interjection'
elif 'id="Particle"' in line:
mode = 'particle'
elif 'id="Preposition"' in line:
mode = 'preposition'
elif 'id="' in line:
mode = None
elif (mode == 'etmyology') and ('<p>' in line):
etymology = text(line)
elif (mode is not None) and ('<li>' in line):
definitions.setdefault(mode, []).append(text(line))
if '<hr' in line:
break
return etymology, definitions
示例7: movie
def movie(bot, trigger):
"""
Returns some information about a movie, like Title, Year, Rating, Genre and IMDB Link.
"""
if not trigger.group(2):
return
word = trigger.group(2).rstrip()
uri = "http://www.omdbapi.com/?t=" + word
u = web.get(uri, 30)
data = json.loads(u) # data is a Dict containing all the information we need
if data["Response"] == "False":
if "Error" in data:
message = "[MOVIE] %s" % data["Error"]
else:
LOGGER.warning("Got an error from the OMDb api, search phrase was %s; data was %s", word, str(data))
message = "[MOVIE] Got an error from OMDbapi"
else:
message = (
"[MOVIE] Title: "
+ data["Title"]
+ " | Year: "
+ data["Year"]
+ " | Rating: "
+ data["imdbRating"]
+ " | Genre: "
+ data["Genre"]
+ " | IMDB Link: http://imdb.com/title/"
+ data["imdbID"]
)
bot.say(message)
示例8: vimeo_by_url
def vimeo_by_url(bot, trigger, found_match=None):
match = found_match or trigger
videoID = match.group(2)
apiURL = "https://vimeo.com/api/v2/video/" + videoID + ".json"
try:
resp = json.loads(web.get(apiURL))
except:
return
output = u"[Vimeo] "
output += u"Title: %s" % (str(resp[0]['title']))
if 'user_name' in resp[0]:
output += u" | Uploader: %s" % (str(resp[0]['user_name']))
if 'upload_date' in resp[0]:
output += u" | Uploaded: %s" % (str(resp[0]['upload_date']))
if 'duration' in resp[0]:
output += u" | Duration: %s" % (str(resp[0]['duration']))
if 'stats_number_of_plays' in resp[0]:
output += u" | Views : %s" % (str(resp[0]['stats_number_of_plays']))
if 'stats_number_of_comments' in resp[0]:
output += u" | Comments: %s" % (str(resp[0]['stats_number_of_comments']))
if 'stats_number_of_likes' in resp[0]:
output += u" | Likes: %s" % (str(resp[0]['stats_number_of_likes']))
bot.say(output)
示例9: short_cancelled
def short_cancelled(bot, trigger):
"""Display short list of cancelled courses at MUN"""
page, headers = web.get(uri, return_headers=True)
if headers['_http_status'] != 200:
bot.say('Couldn\'t find cancellation information.')
return
parsed = html.fromstring(page)
middle = parsed.get_element_by_id('middle')
contents = list(middle)
reply = ''
for element in contents:
if element.tag=='p' and element.text_content() == '________________________________________':
break
elif element.tag=='h2':
printed = True
text = element.text_content()
day = parser.parse(text)
if day.date() == datetime.today().date():
reply += '| MUN\'s Cancellations for ' + bold(text) + ' (TODAY): '
else:
reply += '| MUN\'s Cancellations for ' + bold(text) + ': '
elif element.tag=='p':
text = element.text_content()
course = list(element)[0].text_content()
reply += course + ', '
bot.say(reply[2:-2])
bot.say('Use \'.canceldetail\' for more detailed information')
示例10: bing_search
def bing_search(query, lang="en-GB"):
base = "http://www.bing.com/search?mkt=%s&q=" % lang
bytes = web.get(base + query)
m = r_bing.search(bytes)
# print m
if m:
return m.group(1)
示例11: movie
def movie(bot, trigger):
"""
Returns some information about a movie, like Title, Year, Rating, Genre and IMDB Link.
"""
if not trigger.group(2):
return
word = trigger.group(2).rstrip()
uri = "http://www.imdbapi.com/?t=" + word
u = web.get(uri, 30)
data = json.loads(u) # data is a Dict containing all the information we need
if data['Response'] == 'False':
if 'Error' in data:
message = '[MOVIE] %s' % data['Error']
else:
LOGGER.warning(
'Got an error from the imdb api, search phrase was %s; data was %s',
word, str(data))
message = '[MOVIE] Got an error from imdbapi'
else:
message = '[MOVIE] Title: ' + data['Title'] + \
' | Year: ' + data['Year'] + \
' | Rating: ' + data['imdbRating'] + \
' | Genre: ' + data['Genre'] + \
' | IMDB Link: http://imdb.com/title/' + data['imdbID']
bot.say(message)
示例12: cancelled
def cancelled(bot, trigger):
"""Show current cancelled classes at MUN"""
page, headers = web.get(uri, return_headers=True)
if headers['_http_status'] != 200:
bot.say('Couldn\'t find cancellation information.')
return
parsed = html.fromstring(page)
middle = parsed.get_element_by_id('middle')
contents = list(middle)
reply = []
if trigger.nick != trigger.sender:
bot.reply('I\'m messaging you with a detailed cancellation list!')
for element in contents:
if element.tag=='p' and element.text_content() == '________________________________________':
break
elif element.tag=='h2':
printed = True
text = element.text_content()
day = parser.parse(text)
if day.date() == datetime.today().date():
reply.append('MUN\'s Cancellations for ' + bold(text) + ' (TODAY):')
else:
reply.append('MUN\'s Cancellations for ' + bold(text) + ': ')
elif element.tag=='p':
text = element.text_content()
course = list(element)[0].text_content()
reply.append(bold(course) + text[len(course):])
for a in reply:
bot.msg(trigger.nick, a)
示例13: weather
def weather(bot, trigger):
""".weather location - Show the weather at the given location."""
location = trigger.group(2)
woeid = ''
if not location:
woeid = bot.db.get_nick_value(trigger.nick, 'woeid')
if not woeid:
return bot.msg(trigger.sender, "I don't know where you live. " +
'Give me a location, like .weather London, or tell me where you live by saying .setlocation London, for example.')
else:
location = location.strip()
woeid = bot.db.get_nick_value(location, 'woeid')
if woeid is None:
first_result = woeid_search(location)
if first_result is not None:
woeid = first_result.get('woeid')
if not woeid:
return bot.reply("I don't know where that is.")
query = web.urlencode({'w': woeid, 'u': 'c'})
raw = web.get('http://weather.yahooapis.com/forecastrss?' + query,
dont_decode=True)
parsed = xmltodict.parse(raw).get('rss')
location = parsed.get('channel').get('title')
cover = get_cover(parsed)
temp = get_temp(parsed)
humidity = get_humidity(parsed)
wind = get_wind(parsed)
bot.say(u'%s: %s, %s, %s, %s' % (location, cover, temp, humidity, wind))
示例14: 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
示例15: etymology
def etymology(word):
# @@ <nsh> sbp, would it be possible to have a flag for .ety to get 2nd/etc
# entries? - http://swhack.com/logs/2006-07-19#T15-05-29
if len(word) > 25:
raise ValueError("Word too long: %s[...]" % word[:10])
word = {'axe': 'ax/axe'}.get(word, word)
bytes = web.get(etyuri % word)
definitions = r_definition.findall(bytes)
if not definitions:
return None
defn = text(definitions[0])
m = r_sentence.match(defn)
if not m:
return None
sentence = m.group(0)
maxlength = 275
if len(sentence) > maxlength:
sentence = sentence[:maxlength]
words = sentence[:-5].split(' ')
words.pop()
sentence = ' '.join(words) + ' [...]'
sentence = '"' + sentence.replace('"', "'") + '"'
return sentence + ' - ' + (etyuri % word)