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)
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)})
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
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)})
def execute(self, message): """ @type message: IRCMessage """ try: results = WebUtils.googleSearch(message.Parameters) firstResult = results['responseData']['results'][0] title = firstResult['titleNoFormatting'] content = firstResult['content'] content = re.sub(r'<.*?>', '', content) # strip html tags content = re.sub(r'\s+', ' ', content) # replace multiple spaces with single ones (includes newlines?) content = StringUtils.unescapeXHTML(content) url = firstResult['unescapedUrl'] replyText = u'{1}{0}{2}{0}{3}'.format(StringUtils.graySplitter, title, content, url) return IRCResponse(ResponseType.Say, replyText, message.ReplyTo) except Exception, x: print str(x) return IRCResponse(ResponseType.Say, x.args, message.ReplyTo)
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
def execute(self, message): """ @type message: IRCMessage """ try: results = WebUtils.googleSearch(message.Parameters) firstResult = results[u'items'][0] title = firstResult[u'title'] title = re.sub(r'\s+', ' ', title) content = firstResult[u'snippet'] content = re.sub( r'\s+', ' ', content ) # replace multiple spaces with single ones (includes newlines?) content = StringUtils.unescapeXHTML(content) url = firstResult[u'link'] replyText = u'{1}{0}{2}{0}{3}'.format(StringUtils.graySplitter, title, content, url) return IRCResponse(ResponseType.Say, replyText, message.ReplyTo) except Exception, x: print str(x) return IRCResponse(ResponseType.Say, x.args, message.ReplyTo)
def _scanTwitter(self): for user, lastTweetTimestamp in self.follows.iteritems(): timeline = self.twitter.statuses.user_timeline(screen_name=user) newTweets = [] for tweet in timeline: tweetTimestamp = datetime.datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y') if tweetTimestamp > lastTweetTimestamp: newTweets.append(tweet) else: if len(newTweets) > 0: self.follows[user] = datetime.datetime.strptime(newTweets[0]['created_at'], '%a %b %d %H:%M:%S +0000 %Y') else: self.follows[user] = tweetTimestamp break if len(newTweets) > 0: newTweets = newTweets[::-1] # reverse the list so oldest tweets are first for tweet in newTweets: # skip replies if tweet['in_reply_to_screen_name'] is not None: continue 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']) formatString = unicode(assembleFormattedText(A.normal['Tweet! ', A.bold['@{0}>'], ' {1}'])) newTweet = formatString.format(tweet['user']['screen_name'], tweetText) for channel, _ in self.bot.channels.iteritems(): self.bot.sendResponse(IRCResponse(ResponseType.Say, newTweet, channel))