本文整理汇总了Python中Utils.WebUtils类的典型用法代码示例。如果您正苦于以下问题:Python WebUtils类的具体用法?Python WebUtils怎么用?Python WebUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WebUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FollowTwitch
def FollowTwitch(self, channel, message):
# Heavily based on Didero's DideRobot code for the same
# https://github.com/Didero/DideRobot/blob/06629fc3c8bddf8f729ce2d27742ff999dfdd1f6/commands/urlTitleFinder.py#L37
# TODO: other stats?
chanData = {}
channelOnline = False
twitchHeaders = [('Accept', 'application/vnd.twitchtv.v2+json')]
webPage = WebUtils.fetchURL(u'https://api.twitch.tv/kraken/streams/{0}'.format(channel), twitchHeaders)
streamData = json.loads(webPage.body)
if 'stream' in streamData and streamData['stream'] is not None:
chanData = streamData['stream']['channel']
channelOnline = True
elif 'error' not in streamData:
webPage = WebUtils.fetchURL(u'https://api.twitch.tv/kraken/channels/{0}'.format(channel), twitchHeaders)
chanData = json.loads(webPage.body)
if len(chanData) > 0:
if channelOnline:
channelInfo = assembleFormattedText(A.fg.green['']) + '{0}'.format(chanData['display_name']) + assembleFormattedText(A.normal[''])
else:
channelInfo = assembleFormattedText(A.fg.red['']) + '{0}'.format(chanData['display_name']) + assembleFormattedText(A.normal[''])
channelInfo += u' "{0}"'.format(re.sub('[\r\n]+', self.graySplitter, chanData['status'].strip()))
if chanData['game'] is not None:
channelInfo += assembleFormattedText(A.normal[A.fg.gray[', playing '], '{0}'.format(chanData['game'])])
if chanData['mature']:
channelInfo += assembleFormattedText(A.normal[A.fg.lightRed[' [Mature]']])
if channelOnline:
channelInfo += assembleFormattedText(A.normal[A.fg.green[' (Live with {0:,d} viewers)'.format(streamData['stream']['viewers'])]])
else:
channelInfo += assembleFormattedText(A.normal[A.fg.red[' (Offline)']])
return IRCResponse(ResponseType.Say, channelInfo, message.ReplyTo)
示例2: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
wtfsimfd = "http://whatthefuckshouldimakefordinner.com/{}"
options = {'meat': 'index.php', 'veg': 'veg.php', 'drink': 'drinks.php'}
option = 'meat'
if len(message.ParameterList) > 0:
option = message.ParameterList[0]
if option in options:
webPage = WebUtils.fetchURL(wtfsimfd.format(options[option]))
soup = BeautifulSoup(webPage.body)
phrase = soup.find('dl').text
item = soup.find('a')
link = WebUtils.shortenGoogl(item['href'])
return IRCResponse(ResponseType.Say,
u"{}... {} {}".format(phrase, item.text, link),
message.ReplyTo)
else:
error = u"'{}' is not a recognized dinner type, please choose one of {}"\
.format(option, u'/'.join(options.keys()))
return IRCResponse(ResponseType.Say, error, message.ReplyTo)
示例3: FollowTwitter
def FollowTwitter(self, tweeter, tweetID, message):
webPage = WebUtils.fetchURL('https://twitter.com/{0}/status/{1}'.format(tweeter, tweetID))
soup = BeautifulSoup(webPage.body)
tweet = soup.find(class_='permalink-tweet')
user = tweet.find(class_='username').text
tweetText = tweet.find(class_='tweet-text')
tweetTimeText = tweet.find(class_='client-and-actions').text.strip()
try:
tweetTimeText = time.strftime('%Y/%m/%d %H:%M', time.strptime(tweetTimeText, '%I:%M %p - %d %b %Y'))
except ValueError:
pass
links = tweetText.find_all('a', {'data-expanded-url': True})
for link in links:
link.string = ' ' + link['data-expanded-url']
embeddedLinks = tweetText.find_all('a', {'data-pre-embedded': 'true'})
for link in embeddedLinks:
link.string = ' ' + link['href']
text = StringUtils.unescapeXHTML(tweetText.text)
text = re.sub('[\r\n]+', self.graySplitter, text)
formatString = unicode(assembleFormattedText(A.normal[A.fg.gray['[{0}]'], A.bold[' {1}:'], ' {2}']))
return IRCResponse(ResponseType.Say,
formatString.format(tweetTimeText, user, text),
message.ReplyTo,
{'urlfollowURL': 'https://twitter.com/{}/status/{}'.format(tweeter, tweetID)})
示例4: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
if len(message.ParameterList) != 1:
return IRCResponse(ResponseType.Say, self.help, message.ReplyTo)
subreddit = message.ParameterList[0].lower()
url = "https://api.imgur.com/3/gallery/r/{}/time/all/{}"
url = url.format(subreddit, random.randint(0, 100))
response = WebUtils.fetchURL(url, self.headers)
jsonResponse = json.loads(response.body)
images = jsonResponse['data']
if not images:
return IRCResponse(ResponseType.Say,
"The subreddit '{}' doesn't seem to have any images posted to it (or it doesn't exist!)"
.format(subreddit),
message.ReplyTo)
image = random.choice(images)
data = []
if image['title'] is not None:
data.append(image['title'])
if image['nsfw']:
data.append(u'\x034\x02NSFW!\x0F')
if image['animated']:
data.append(u'\x032\x02Animated!\x0F')
data.append(image['link'])
graySplitter = assembleFormattedText(A.normal[' ', A.fg.gray['|'], ' '])
return IRCResponse(ResponseType.Say, graySplitter.join(data), message.ReplyTo)
示例5: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
if len(message.ParameterList) > 0:
if self.api_key is None:
return IRCResponse(ResponseType.Say, "[Bing Maps API key not found]", message.ReplyTo)
url = "http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}".format(urllib.quote_plus(message.Parameters), self.api_key)
page = WebUtils.fetchURL(url)
result = json.loads(page.body)
if result['resourceSets'][0]['estimatedTotal'] == 0:
print result
return IRCResponse(ResponseType.Say,
"Couldn't find GPS coords for '{0}', sorry!".format(message.Parameters),
message.ReplyTo)
coords = result['resourceSets'][0]['resources'][0]['point']['coordinates']
return IRCResponse(ResponseType.Say,
"GPS coords for '{0}' are: {1},{2}".format(message.Parameters, coords[0], coords[1]),
message.ReplyTo)
else:
return IRCResponse(ResponseType.Say,
"You didn't give an address to look up",
message.ReplyTo)
示例6: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
if len(message.ParameterList) == 0:
return IRCResponse(ResponseType.Say,
"You didn't give a word! Usage: {0}".format(self.help),
message.ReplyTo)
word = message.Parameters
webPage = WebUtils.fetchURL('http://www.etymonline.com/index.php?allowed_in_frame=0&search={0}'.format(word))
root = BeautifulSoup(webPage.body)
etymTitle = root.find('dt')
etymDef = root.find('dd')
if not etymTitle or not etymDef:
return IRCResponse(ResponseType.Say,
"No etymology found for '{0}'".format(word),
message.ReplyTo)
response = "{0}: {1}".format(etymTitle.text.strip(), etymDef.text.strip())
return IRCResponse(ResponseType.Say,
response,
message.ReplyTo)
示例7: _stringifyTweet
def _stringifyTweet(self, tweet):
"""
turn a tweet object into a nice string for us to send to IRC
@type tweet: dict[str, T/str]
"""
retweet = None
# get the original tweet if this is a retweet
if 'retweeted_status' in tweet:
retweet = tweet
tweet = retweet['retweeted_status']
tweetText = StringUtils.unescapeXHTML(tweet['text'])
tweetText = re.sub('[\r\n]+', StringUtils.graySplitter, tweetText)
for url in tweet['entities']['urls']:
tweetText = tweetText.replace(url['url'], url['expanded_url'])
timestamp = parser.parse(tweet['created_at'])
timeString = timestamp.strftime('%A, %Y-%m-%d %H:%M:%S %Z')
delta = datetime.datetime.utcnow() - timestamp.replace(tzinfo=None)
deltaString = StringUtils.deltaTimeToString(delta)
tweetText = u'{} (tweeted {}, {} ago)'.format(tweetText, timeString, deltaString)
if retweet is None:
user = tweet['user']['screen_name']
else:
user = retweet['user']['screen_name']
tweetText = 'RT {}: {}'.format(tweet['user']['screen_name'], tweetText)
link = u'https://twitter.com/{}/status/{}'.format(tweet['user']['screen_name'], tweet['id_str'])
link = WebUtils.shortenGoogl(link)
formatString = unicode(assembleFormattedText(A.normal[A.bold['@{0}>'], ' {1} {2}']))
newTweet = formatString.format(user, tweetText, link)
return newTweet
示例8: FollowTwitter
def FollowTwitter(self, tweeter, tweetID, message):
webPage = WebUtils.fetchURL('https://twitter.com/{0}/status/{1}'.format(tweeter, tweetID))
soup = BeautifulSoup(webPage.body)
tweet = soup.find(class_='permalink-tweet')
user = tweet.find(class_='username').text
tweetText = tweet.find(class_='tweet-text')
links = tweetText.find_all('a', {'data-expanded-url': True})
for link in links:
link.string = link['data-expanded-url']
embeddedLinks = tweetText.find_all('a', {'data-pre-embedded': 'true'})
for link in embeddedLinks:
link.string = link['href']
text = StringUtils.unescapeXHTML(tweetText.text)
text = re.sub('[\r\n]+', self.graySplitter, text)
formatString = unicode(assembleFormattedText(A.normal[A.bold['{0}:'], ' {1}']))
return IRCResponse(ResponseType.Say, formatString.format(user, text), message.ReplyTo)
示例9: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
if len(message.ParameterList) == 0:
return IRCResponse(ResponseType.Say,
"You didn't give an expression to calculate! {0}".format(self.help),
message.ReplyTo)
query = urllib.quote(message.Parameters.encode('utf8'))
j = WebUtils.sendToServer('http://www.google.com/ig/calculator?hl=en&q=' + query)
j = re.sub(r"{\s*'?(\w)", r'{"\1', j)
j = re.sub(r",\s*'?(\w)", r',"\1', j)
j = re.sub(r"(\w)'?\s*:", r'\1":', j)
j = re.sub(r":\s*'(\w)'\s*([,}])", r':"\1"\2', j)
j = self.htmlParser.unescape(j)
j = j.replace('\xa0', ',')
j = j.replace('\\x3c', '<')
j = j.replace('\\x3e', '>')
j = j.replace('<sup>', '^(')
j = j.replace('</sup>', ')')
print j
result = json.loads(j)
if len(result['rhs']) > 0:
return IRCResponse(ResponseType.Say,
"{1}'s Result: {0}".format(result['rhs'], message.User.Name),
message.ReplyTo)
if len(result['error']) > 0:
return IRCResponse(ResponseType.Say,
'Calculation Error or Unsupported Operations',
message.ReplyTo)
示例10: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
baseURL = "http://greywool.com/desertbus/{}/gifs/random.php"
years = range(7, 9)
if len(message.ParameterList) > 0:
invalid = u"'{}' is not a valid year, valid years are {} to {}"\
.format(message.ParameterList[0], years[0], years[-1])
try:
year = int(message.ParameterList[0])
except ValueError:
return IRCResponse(ResponseType.Say, invalid, message.ReplyTo)
if year not in years:
return IRCResponse(ResponseType.Say, invalid, message.ReplyTo)
else:
year = random.choice(years)
url = baseURL.format(year)
webPage = WebUtils.fetchURL(url)
link = webPage.body
return IRCResponse(ResponseType.Say,
u"Random DB{} gif: {}".format(year, link),
message.ReplyTo)
示例11: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
if len(message.ParameterList) == 0:
return IRCResponse(ResponseType.Say, "You didn't give a URL to shorten!", message.ReplyTo)
url = WebUtils.shortenGoogl(message.Parameters)
return IRCResponse(ResponseType.Say, url, message.ReplyTo)
示例12: FollowKickstarter
def FollowKickstarter(self, ksID, message):
webPage = WebUtils.fetchURL('https://www.kickstarter.com/projects/{0}/'.format(ksID))
soup = BeautifulSoup(webPage.body)
data = []
title = soup.find(class_='title')
if title is not None:
creator = soup.find(id='name')
if creator is not None:
data.append(assembleFormattedText(A.normal['{0}', A.fg.gray[' by '], '{1}']).format(title.h2.text.strip(), creator.text.strip()))
else:
data.append(title.h2.text.strip())
stats = soup.find(id='stats')
backerCount = stats.find(id='backers_count')
if backerCount is not None:
data.append('Backers: {0:,}'.format(int(backerCount['data-backers-count'])))
pledged = stats.find(id='pledged')
if pledged is not None:
if float(pledged['data-percent-raised']) >= 1.0:
percentageString = A.fg.green['({3:,.0f}% funded)']
else:
percentageString = A.fg.red['({3:,.0f}% funded)']
pledgedString = assembleFormattedText(A.normal['Pledged: {0:,.0f}', A.fg.gray['/'], '{1:,.0f} {2} ', percentageString])
data.append(pledgedString.format(float(pledged['data-pledged']),
float(pledged['data-goal']),
pledged.data['data-currency'],
float(pledged['data-percent-raised']) * 100))
findState = soup.find(id='main_content')
if 'Project-state-canceled' in findState['class']:
data.append(assembleFormattedText(A.normal[A.fg.red['Cancelled']]))
elif 'Project-state-failed' in findState['class']:
data.append(assembleFormattedText(A.normal[A.fg.red['Failed']]))
elif 'Project-state-successful' in findState['class']:
data.append(assembleFormattedText(A.normal[A.fg.green['Successful']]))
elif 'Project-state-live' in findState['class']:
duration = stats.find(id='project_duration_data')
if duration is not None:
remaining = float(duration['data-hours-remaining'])
days = math.floor(remaining/24)
hours = remaining/24 - days
data.append('Duration: {0:.0f} days {1:.1f} hours to go'.format(days, hours))
return IRCResponse(ResponseType.Say, self.graySplitter.join(data), message.ReplyTo)
示例13: getSteamPrice
def getSteamPrice(cls, appType, appId, region):
webPage = WebUtils.fetchURL('http://store.steampowered.com/api/{0}details/?{0}ids={1}&cc={2}&l=english&v=1'.format(appType, appId, region))
priceField = {'app': 'price_overview', 'package': 'price'}[appType]
response = json.loads(webPage.body)
if 'data' not in response[appId]:
return
if region == 'AU':
response[appId]['data'][priceField]['currency'] = 'AUD'
return response[appId]['data'][priceField]
示例14: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
url = "http://greywool.com/desertbus/gifs/random.php"
webPage = WebUtils.fetchURL(url)
link = webPage.body
return IRCResponse(ResponseType.Say, u"Random DB gif: {0}".format(link), message.ReplyTo)
示例15: execute
def execute(self, message):
"""
@type message: IRCMessage
"""
responses = []
for feedName, feedDeets in DataStore.LRRChecker.iteritems():
if feedDeets['lastCheck'] > datetime.datetime.utcnow() - datetime.timedelta(minutes=10):
continue
DataStore.LRRChecker[feedName]['lastCheck'] = datetime.datetime.utcnow()
feedPage = WebUtils.fetchURL(feedDeets['url'])
if feedPage is None:
#TODO: log an error here that the feed likely no longer exists!
continue
root = ET.fromstring(feedPage.body)
item = root.find('channel/item')
if item is None:
#TODO: log an error here that the feed likely no longer exists!
continue
title = DataStore.LRRChecker[feedName]['lastTitle'] = item.find('title').text
link = DataStore.LRRChecker[feedName]['lastLink'] = WebUtils.shortenGoogl(item.find('link').text)
newestDate = dparser.parse(item.find('pubDate').text, fuzzy=True, ignoretz=True)
if newestDate > feedDeets['lastUpdate']:
DataStore.LRRChecker[feedName]['lastUpdate'] = newestDate
if feedDeets['suppress']:
DataStore.LRRChecker[feedName]['suppress'] = False
else:
response = 'New {0}! Title: {1} | {2}'.format(feedName, title, link)
responses.append(IRCResponse(ResponseType.Say, response, '#desertbus'))
return responses