def get_tweets(url, sender_uid=False): try: data = web.text(url).replace('\r', '').replace('\n', ' ') data = re.compile(r'<table class="tweet.*?>.*?</table>').findall(data) except: return tweets = [] for tweet in data: try: tmp = {} tmp['url'] = list(r_tweeturl.findall(tweet)[0]) tmp['url'] = 'https://twitter.com/%s/status/%s' % (tmp['url'][0], tmp['url'][1]) tmp['full'] = web.escape(r_fullname.findall(tweet)[0].strip()) tmp['user'] = r_username.findall(tweet)[0].strip() tmp['time'] = web.striptags(r_time.findall(tweet)[0]) tweet_data = r_tweet.findall(tweet)[0].strip() tweet_data = re.sub(r_url, '\g<url>', tweet_data) tmp['text'] = web.escape(web.striptags(tweet_data)) uids = r_uid.findall(' ' + tmp['text']) for uid in uids: tmp['text'] = tmp['text'].replace(uid, '{purple}{b}@{b}%s{c}' % uid.strip('@')).lstrip() # Check if it's a retweet if sender_uid: if sender_uid.lower().strip('@') != tmp['user'].lower().strip('@'): tmp['text'] = tmp['text'] + ' ({purple}{b}@{b}%s{c})' % tmp['user'] tmp['user'] = sender_uid.strip('@') + ' {blue}{b}retweeted{c}{b}' tweets.append(tmp) except: continue if tweets: return tweets else: return False
def search(code, input): """Queries DuckDuckGo for the specified input.""" try: data = web.get(uri, params={'q': input.group(2)}) tmp = data.text.replace('\r', '').replace('\n', '').strip() target = r'(?im)<div class="results_links .*?(?!.*web\-result\-sponsored)">.*?<a .*? href="(.*?)">.*?</a>.*?' \ '<div class="snippet">(.*?)</div>.*?<div class="url">(.*?)</div>' found = list(re.findall(target, tmp)) if len(found) > url_count: found = found[:url_count] results = [] if len(found) < 2: return code.say('{b}No results found{b}') count = 0 for item in found: i = list(item) result = {} result['url'] = web.escape(web.striptags(i[0])) result['short'] = web.escape(web.striptags(i[2]).capitalize().split('/')[0]) result['title'] = web.escape(web.striptags(i[1])) if len(result['title']) > title_length: result['title'] = result['title'][:title_length] + '{b}...{b}' results.append('{b}%s{b} - {%s}%s{c} - %s' % (result['short'], url_colors[count], result['title'], result['url'])) count += 1 return code.say(' | '.join(results)) except Exception as e: output.error('Error in search.py: %s' % str(e)) return code.say('{b}Unable to search for %s{b}' % input.group(2))
def fml(code, input): """fml - Retrieve random FML's, via FMyLife.com's dev API.""" # Random/No input if not input.group(2): try: r = fml_random() code.say('#{blue}%s{c} %s +{b}%s{b}/-{b}%s{b} - http://fmylife.com/%s' % ( str(r['fml-id']), web.escape(r['fml']).replace('FML', '{red}FML{c}'), r['+'], r['-'], str(r['fml-id']) )) except: return code.say('{red}Failed to retrieve random FML.') elif input.group(2).startswith('#') and input.group(2).lstrip('#').isdigit(): try: r = fml_id_search(input.group(2).lstrip('#')) code.say('#{blue}%s{c} %s +{b}%s{b}/-{b}%s{b} - http://fmylife.com/%s' % ( str(r['fml-id']), web.escape(r['fml']).replace('FML', '{red}FML{c}'), r['+'], r['-'], str(r['fml-id']) )) except: return code.say('Failed to retrieve FML via ID.') # Input/Assume search query, with (possible) number at end indicating FML # index else: msg = input.group(2).lower().strip() parts = msg.split() if parts[-1].replace('-', '').isdigit(): if int(parts[-1]) <= 0: id = 1 else: id = int(parts[-1].replace('-', '')) del parts[-1] query = '+'.join(parts) else: id = 1 query = msg.replace(' ', '+') try: r = fml_search(query, id) code.say( '(%s/%s) #{blue}%s{c} %s +{b}%s{b}/-{b}%s{b} - http://fmylife.com/%s' % ( r['id'], r['max'], str(r['fml-id']), web.escape(r['fml']).replace('FML', '{red}FML{c}'), r['+'], r['-'], str(r['fml-id']) )) except: return code.say('Failed to search for FML.')
def user_lookup(code, id, showerror=True): try: data = web.text( 'http://steamdb.info/calculator/?player={id}¤cy=us'.format( id=id), timeout=10) if 'This profile is private, unable to retrieve owned games.' in data: if showerror: code.say( '{b}Unabled to retrieve info, that account is {red}private{c}!' ) return realname = re.search(r'<title>(?P<name>.*?) \xb7 .*?</title>', data).group('name') status = re.search( r'<td class="span2">Status</td>.*?<td>(?P<status>.*?)</td>', data).group('status') # Basic user information details = data.split('[list]')[1].split('[/list]')[0] details = re.sub(r'\<\/.*?\>', '', details) details = re.sub(r'\<.*?\>', ' {b}- ', details) details = re.sub(r'\[.*?\]', '', details) details = details.replace(': ', ': {b}') form = 'profiles' if str(id).isdigit() else 'id' url = 'http://steamcommunity.com/{}/'.format(form) + id return code.say( '{b}%s{b} - {green}%s{c} - %s - %s' % (web.escape(realname), web.striptags(status), details, url)) except: if showerror: code.say('{b}Unable to find user information on %s!' % id) return
def fml_fmt(data, id=0): raw_items = [item for item in re.compile(r'<item .*?>.*?</item>').findall(data)] if not raw_items: return False items, count = [], 0 for f in raw_items: count += 1 try: items.append({ 'fml': web.escape(web.striptags(re.compile(r'<text>(.*?)</text>').findall(f)[0])).replace('FML', '{red}FML{c}'), 'id': int(re.compile(r'<item .*id="([0-9]+)".*>').findall(f)[0]), 'uid': count, 'max': len(raw_items), 'agree': int(web.striptags(re.compile(r'<agree>(.*?)</agree>').findall(f)[0])), 'deserved': int(web.striptags(re.compile(r'<deserved>(.*?)</deserved>').findall(f)[0])) }) except: items.append(False) if id < 1: id = 1 if id > len(raw_items): id = len(raw_items) id = id - 1 return items[id]
def chuck(code, input): """Get random Chuck Norris facts. I bet he's better than you.""" try: data = web.json('http://api.icndb.com/jokes/random') except: return code.say('Chuck seems to be in the way. I\'m not f*****g with him.') code.say('#{blue}%s{c} - %s' % (data['value']['id'], web.escape(data['value']['joke'])))
def lastfm(code, input): """ lfm <username> -- Pull last played song for the user """ user = input.group(2).split()[0].strip().lower() data = getdata(user) data = data.text.encode('ascii', 'ignore') if not data: return code.say('Username {} does not exist in the last.fm database.'.format(user)) song = web.striptags(re.compile(r'<title>.*?</title>').findall(data)[1]) code.reply('{purple}' + web.escape(song).replace(' ', ' -- ', 1) + '{c} {red}(via Last.Fm)')
def chuck(code, input): """Get random Chuck Norris facts. I bet he's better than you.""" try: if input.group(2) and input.group(2).isdigit(): data = web.json('http://api.icndb.com/jokes/' + input.group(2)) else: data = web.json('http://api.icndb.com/jokes/random') except: return code.say( 'Chuck seems to be in the way. I\'m not f*****g with him.') code.say('#{blue}%s{c} - %s' % (data['value']['id'], web.escape(data['value']['joke'])))
def dinner(code, input): """fd -- WHAT DO YOU WANT FOR F*****G DINNER?""" err = '{red}EAT LEFT OVER PIZZA FOR ALL I CARE.' try: data = web.text(uri) results = re_mark.findall(data) if not results: return code.say(err) url, food = results[0][0], web.escape(results[0][1]) code.say('WHY DON\'T YOU EAT SOME F*****G {b}%s{b}. HERE IS THE RECIPE: %s' % ( food.upper(), url)) except: return code.say(err)
def get_tweets(url, sender_uid=False): try: data = web.text(url).replace('\r', '').replace('\n', ' ') data = re.compile(r'<table class="tweet.*?>.*?</table>').findall(data) except: return tweets = [] for tweet in data: try: tmp = {} tmp['url'] = list(r_tweeturl.findall(tweet)[0]) tmp['url'] = 'https://twitter.com/%s/status/%s' % (tmp['url'][0], tmp['url'][1]) tmp['full'] = web.escape(r_fullname.findall(tweet)[0].strip()) tmp['user'] = r_username.findall(tweet)[0].strip() tmp['time'] = web.striptags(r_time.findall(tweet)[0]) tweet_data = r_tweet.findall(tweet)[0].strip() tweet_data = re.sub(r_url, '\g<url>', tweet_data) tmp['text'] = web.escape(web.striptags(tweet_data)) uids = r_uid.findall(' ' + tmp['text']) for uid in uids: tmp['text'] = tmp['text'].replace( uid, '{purple}{b}@{b}%s{c}' % uid.strip('@')).lstrip() # Check if it's a retweet if sender_uid: if sender_uid.lower().strip('@') != tmp['user'].lower().strip( '@'): tmp['text'] = tmp[ 'text'] + ' ({purple}{b}@{b}%s{c})' % tmp['user'] tmp['user'] = sender_uid.strip( '@') + ' {blue}{b}retweeted{c}{b}' tweets.append(tmp) except: continue if tweets: return tweets else: return False
def fucking_weather(code, input): """fw (ZIP|City, State) -- provide a ZIP code or a city state pair to hear about the f*****g weather""" if not input.group(2): return code.say('{red}{b}INVALID F*****G INPUT. PLEASE ENTER A F*****G ZIP CODE, OR A F*****G CITY-STATE PAIR.') try: args = { "where": web.quote(input.group(2)) } data = web.text('http://thefuckingweather.com/', params=args) temp = re.compile( r'<p class="large"><span class="temperature" tempf=".*?">.*?</p>').findall(data)[0] temp = web.striptags(temp).replace(' ', '').replace('"', '') remark = re.compile(r'<p class="remark">.*?</p>').findall(data)[0] remark = re.sub(r'\<.*?\>', '', remark).strip() flavor = re.compile(r'<p class="flavor">.*?</p>').findall(data)[0] flavor = re.sub(r'\<.*?\>', '', flavor).strip() return code.say('%s {b}%s{b}. %s' % (web.escape(temp), remark, flavor)) except: return code.say('{red}{b}I CAN\'T FIND THAT SHIT.')
def wa(code, input): """Wolfram Alpha search - It's slow. """ query = input.group(2) uri = 'http://tumbolia.appspot.com/wa/' try: answer = web.text(uri + web.quote(query), timeout=14) except: return code.say('It seems WolframAlpha took too long to respond!') if answer and 'json stringified precioussss' not in answer: answer = answer.strip('\n').split(';') for i in range(len(answer)): answer[i] = answer[i].replace('|', '').strip() answer = '{purple}{b}WolframAlpha: {c}{b}' + ' - '.join(answer).replace('\\', '').replace('->', ': ') while ' ' in answer: answer = answer.replace(' ', ' ') return code.say(web.escape(answer)) else: return code.reply('{red}Sorry, no result.')
def fml_id_search(query_id): """fml - Retrieve the FML in accordance with the assigned ID, via FMyLife.com's dev API.""" try: args = { "language": language, "key": key } r = web.text('http://api.fmylife.com/view/{}/nocomment'.format(str(query_id)), params=args) except: return fml = re.compile(r'<text>.*?</text>').findall(r) fmlid = re.compile(r'<item id=".*?">').findall(r) agree = re.compile(r'<agree>.*?</agree>').findall(r) deserved = re.compile(r'<deserved>.*?</deserved>').findall(r) return { 'fml': web.escape(web.striptags(fml[0])), 'fml-id': fmlid[0].replace('<item id="', '', 1).replace('">', '', 1), '+': web.striptags(agree[0]), '-': web.striptags(deserved[0]) }
def fml_random(): """fml - Retrieve random FML's, via FMyLife.com's dev API.""" try: args = { "language": language, "key": key } r = web.text('http://api.fmylife.com/view/random/1', params=args) except: return fml = re.compile(r'<text>.*?</text>').findall(r) fmlid = re.compile(r'<item id=".*?">').findall(r) agree = re.compile(r'<agree>.*?</agree>').findall(r) deserved = re.compile(r'<deserved>.*?</deserved>').findall(r) return { 'fml': web.escape(web.striptags(fml[0])), 'fml-id': fmlid[0].replace('<item id="', '', 1).replace('">', '', 1).strip(), '+': web.striptags(agree[0]), '-': web.striptags(deserved[0]) }
def get_url_data(url): if len(url) < url_min_length: return False # URL is really short. Don't need shortening. try: # Pull the headers and content, before we actually pull the data. Test HTTP status codes, # as well as force redirection (not enabled by default for HEAD requests) test = web.head(url, allow_redirects=True, verify=False) if test.headers['content-type'].split('/', 1)[0].lower() not in safe_mime: return False if test.status_code not in safe_status_codes: return False uri = web.get(url, verify=False) if not uri.text: return False title = re.compile('<title.*?>(.*?)</title>', re.IGNORECASE | re.DOTALL).search(uri.text).group(1) title = web.escape(title) title = title.replace('\n', '').replace('\r', '') # Remove spaces... while ' ' in title: title = title.replace(' ', ' ') if compare(title, url.split('//', 1)[1], advanced=True) > 100: # Return if it's very similar to eachother (they're using pretty urls, which show # title words in the url) return False # Shorten LONG urls if len(title) > 200: title = title[:200] + '[...]' if len(title) < title_min_length: # Title output too short return False return title except: return False
def user_lookup(code, id, showerror=True): try: data = web.text('http://steamdb.info/calculator/?player={id}¤cy=us'.format(id=id), timeout=10) if 'This profile is private, unable to retrieve owned games.' in data: if showerror: code.say('{b}Unabled to retrieve info, that account is {red}private{c}!') return realname = re.search(r'<title>(?P<name>.*?) \xb7 .*?</title>', data).group('name') status = re.search( r'<td class="span2">Status</td>.*?<td>(?P<status>.*?)</td>', data).group('status') # Basic user information details = data.split('[list]')[1].split('[/list]')[0] details = re.sub(r'\<\/.*?\>', '', details) details = re.sub(r'\<.*?\>', ' {b}- ', details) details = re.sub(r'\[.*?\]', '', details) details = details.replace(': ', ': {b}') form = 'profiles' if str(id).isdigit() else 'id' url = 'http://steamcommunity.com/{}/'.format(form) + id return code.say('{b}%s{b} - {green}%s{c} - %s - %s' % (web.escape(realname), web.striptags(status), details, url)) except: if showerror: code.say('{b}Unable to find user information on %s!' % id) return
def define(code, input): try: data = web.json(uri.format(word=web.quote(input.group(2))))[0] except: return code.reply('{red}Failed to get definition!') # Go through filters to remove extra stuff that's not needed. word = data['html'] word = web.striptags(word) word = web.escape(word) word = word.replace('\\n', '').replace('\n', '') while ' ' in word: word = word.replace(' ', ' ') word = word.encode('ascii', 'ignore') if len(word) > 380: word = word[:375] + '{c}{b}[...]' # loop through and replace all possible type names for name in highlight: name = ' {} '.format(name) if data['query'].lower().strip() == name.lower(): continue tmp = re.findall(name, word, flags=re.IGNORECASE) for item in tmp: word = word.replace(item, " [{blue}{b}%s{b}{c}] " % item.strip()) if 'is not in the dictionary.' in word: return code.say('Definition for {b}%s{b} not found' % input.group(2)) name = data['query'][0].upper() + data['query'][1::] # Everything below here is for colors only word = '{b}{purple}%s{c}{b}: %s' % (name, word[len(data['query']) + 1::]) word = word.replace('(', '{purple}{b}(').replace(')', '){b}{c}') code.say(word)
def search(code, input): """Queries DuckDuckGo for the specified input.""" try: data = web.get(uri, params={'q': input.group(2)}) tmp = data.text.replace('\r', '').replace('\n', '').strip() target = r'(?im)(<div class="result results_links.*?">.*?<a .*?class="result__a" href="([^"]+)">(.*?)</a>.*?</div>)' found = [ x for x in list(re.findall(target, tmp)) if len(x) > 0 and "badge--ad" not in x[0] ] if len(found) > url_count: found = found[:url_count] results = [] if len(found) < 1: return code.say('{b}No results found{b}') count = 0 for item in found: i = list(item) result = {} result['url'] = i[1] result['title'] = web.escape(web.striptags(i[2])) if len(result['title']) > title_length: result['title'] = result['title'][:title_length] + '{b}...{b}' results.append('{%s}%s{c} - %s' % (url_colors[count], result['title'], result['url'])) count += 1 return code.say(' | '.join(results)) except Exception as e: output.error('Error in search.py: %s' % str(e)) return code.say('{b}Unable to search for %s{b}' % input.group(2))