def on_incoming(self, msg): if not msg.type == msg.CHANNEL: return if self.c.is_ignored(msg.nick): return try: urls = re.findall(r'https?://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]', msg.body) for url in urls: if url.startswith('(') and url.endswith(')'): url = url[1:-1] filename = re.search(r'/([^/]+)/?$', url).groups(1)[0] #if '.' not in filename: # url += '' req_headers = {} if self.lang: req_headers['Accept-Language'] = self.lang head = requests.head(url) if 'text/html' in head.headers['content-type']: req = requests.get(url,headers=req_headers) req = req.text r = re.search(r'(?s)<title>.*</title>', req) if not r: return title = re.sub(r'</?title>', '', fixentities(r.group(0)).strip().replace('\n', '')) message = 'Title: '+title else: content_type = head.headers['content-type'] try: size = self.sizeof_fmt(int(head.headers['content-length'])) except TypeError: size = "Unknown size" message = '%s: %s (%s)' % (filename, content_type, size) self.c.privmsg(msg.channel, message) except Exception as e: print e
def trigger_yt(self, msg): "Usage: yt <searchterm>. Prints title and link to first youtube result." if len(msg.args) == 0: self.c.notice(msg.nick, "Please specify a search term") return url = "https://gdata.youtube.com/feeds/api/videos" params = {'q':' '.join(msg.args), 'max-results':'1', 'v':'2', 'alt':'json'} req = requests.get(url, params=params) entry = req.json['feed']['entry'] if len(entry) < 0: self.c.privmsg(msg.channel, '%s: No entries were found.'%' '.join(msg.args)) return entry = entry[0] link = entry['link'][0] message = u"\002You\0030,4Tube\003 ::\002 %s \002::\002 %s \002::\002 %s" % ( entry['title']['$t'], entry['media$group']['media$description']['$t'], "http://youtu.be/"+entry['id']['$t'].split(':')[-1],) self.c.privmsg(msg.channel, message)
def trigger_g(self, msg): "Usage: g <search term>. Prints title & short description of first google result." if len(msg.args) == 0: self.c.notice(msg.nick, "Please specify a search term") return url = 'http://ajax.googleapis.com/ajax/services/search/web' params = {'q': ' '.join(msg.args), 'v': 1.0} resp_json = requests.get(url, params=params).json results = resp_json["responseData"]["results"] if len(results) == 0: self.c.privmsg(msg.channel, '{}: No results.'.format(' '.join(msg.args))) return top_res = results[0] url = googl.get_short(top_res['url'], self.c.config) message = u"\002\0032G\0034o\0038o\0032g\0033l\0034e\003 ::\002 %s \002::\002 %s \002::\002 %s" % ( fixentities(top_res['titleNoFormatting']), fixentities(unicode(re.sub("</?b>", "\002", top_res['content']))), url) self.c.privmsg(msg.channel, message)