def _query_freebase(self, work_type, thing): props = FREEBASE_TYPES[work_type] url = "https://api.freebase.com/api/service/search?query=%s&type=%s" % (web.urlquote(thing),props['type']) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) if len(response['result']) == 0: return None else: fbid = response['result'][0]['id'] query = { 'escape': False, 'query': { "id": fbid, "type": props['type'], "name": None, "limit": 1 } } query['query'].update(props['subquery']) url = "https://api.freebase.com/api/service/mqlread?query=%s" % web.urlquote(simplejson.dumps(query)) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) result = response['result'] if result is None: return None else: return({ 'props': props, 'url': "http://www.freebase.com" + result['id'], 'title': result['name'], 'characters': props['extractor'](result) })
def _query_freebase(self, work_type, thing): props = FREEBASE_TYPES[work_type] url = "https://api.freebase.com/api/service/search?query=%s&type=%s" % ( web.urlquote(thing), props['type']) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) if len(response['result']) == 0: return None else: fbid = response['result'][0]['id'] query = { 'escape': False, 'query': { "id": fbid, "type": props['type'], "name": None, "limit": 1 } } query['query'].update(props['subquery']) url = "https://api.freebase.com/api/service/mqlread?query=%s" % web.urlquote( simplejson.dumps(query)) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) result = response['result'] if result is None: return None else: return ({ 'props': props, 'url': "http://www.freebase.com" + result['id'], 'title': result['name'], 'characters': props['extractor'](result) })
def _query_freebase(self, work_type, thing): key = conf.get(conf.supybot.plugins.Cast.FreebaseApiKey) props = FREEBASE_TYPES[work_type] url = "https://www.googleapis.com/freebase/v1/search?query=%s&type=%s&key=%s" % ( web.urlquote(thing), props['type'], key) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) if len(response['result']) == 0: return None else: fbid = response['result'][0]['id'] query = { "id": fbid, "type": props['type'], "name": None, "limit": 1 } query.update(props['subquery']) url = "https://www.googleapis.com/freebase/v1/mqlread?query=%s&key=%s" % ( web.urlquote(simplejson.dumps(query)), key) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) result = response['result'] if result is None: return None else: return ({ 'props': props, 'url': "http://www.freebase.com" + result['id'], 'title': result['name'], 'characters': props['extractor'](result) })
def _query_freebase(self, work_type, thing): key = conf.get(conf.supybot.plugins.Cast.FreebaseApiKey) props = FREEBASE_TYPES[work_type] url = "https://www.googleapis.com/freebase/v1/search?query=%s&type=%s&key=%s" % (web.urlquote(thing),props['type'],key) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) if len(response['result']) == 0: return None else: fbid = response['result'][0]['id'] query = { "id": fbid, "type": props['type'], "name": None, "limit": 1 } query.update(props['subquery']) url = "https://www.googleapis.com/freebase/v1/mqlread?query=%s&key=%s" % (web.urlquote(simplejson.dumps(query)),key) response = simplejson.loads(web.getUrl(url, headers=HEADERS)) result = response['result'] if result is None: return None else: return({ 'props': props, 'url': "http://www.freebase.com" + result['id'], 'title': result['name'], 'characters': props['extractor'](result) })
def urbandict(self, irc, msg, args, opts, words): """<phrase> Returns the definition and usage of <phrase> from UrbanDictionary.com. """ use_definition = None for (opt, arg) in opts: if opt == 'def': use_definition = int(arg) terms = ' '.join(words) url = 'http://www.urbandictionary.com/define.php?term=%s' \ % web.urlquote(terms) html = web.getUrl(url) doc = fromstring(html) if len(doc.xpath('//div[@id="not_defined_yet"]')): irc.error('No definition found.', Raise=True) definitions = [] for div in doc.xpath('//div[@class="definition"]'): text = div.text_content() if div.getnext().tag == 'div' \ and div.getnext().attrib.get('class', None) == 'example': text += ' [example] ' + div.getnext().text_content( ) + ' [/example] ' text = re.sub(r'[\\\r\\\n]+', ' ', text) definitions.append(text) if use_definition != None: definitions = [definitions[use_definition - 1]] reply_msg = '%s: %s' % (terms, '; '.join(definitions)) irc.reply(reply_msg.encode('utf8'))
def urbandict(self, irc, msg, args, opts, words): """<phrase> Returns the definition and usage of <phrase> from UrbanDictionary.com. """ use_definition = None for (opt,arg) in opts: if opt == 'def': use_definition = int(arg) terms = ' '.join(words) url = 'http://www.urbandictionary.com/define.php?term=%s' \ % web.urlquote(terms) html = web.getUrl(url) doc = fromstring(html) if len(doc.xpath('//div[@id="not_defined_yet"]')): irc.error('No definition found.', Raise=True) definitions = [] for div in doc.xpath('//div[@class="definition"]'): text = div.text_content() if div.getnext().tag == 'div' \ and div.getnext().attrib.get('class', None) == 'example': text += ' [example] ' + div.getnext().text_content() + ' [/example] ' text = re.sub(r'[\\\r\\\n]+', ' ', text) definitions.append(text) if use_definition != None: definitions = [definitions[use_definition-1]] reply_msg = '%s: %s' % (terms, '; '.join(definitions)) irc.reply(reply_msg.encode('utf8'))
def gender(self, irc, msg, args, name): """<name> Returns gender data on name usage from Freebase: http://genderednames.freebaseapps.com/ """ url = API_URL % web.urlquote(name) json = web.getUrl(url, headers=HEADERS) response = simplejson.loads(json) if not response['total']: irc.reply("The name '%s' was not found on Freebase" % response['name']) return female_percentage = percentage(response['female'], response['total']) male_percentage = percentage(response['male'], response['total']) irc.reply("'%s': %s%% female; %s%% male" % (response['name'], female_percentage, male_percentage), prefixNick=True)
def gender(self, irc, msg, args, name): """<name> Returns gender data on name usage from Freebase: http://genderednames.freebaseapps.com/ """ url = API_URL % web.urlquote(name) json = web.getUrl(url, headers=HEADERS) response = simplejson.loads(json) if not response["total"]: irc.reply("The name '%s' was not found on Freebase" % response["name"]) return female_percentage = percentage(response["female"], response["total"]) male_percentage = percentage(response["male"], response["total"]) irc.reply( "'%s': %s%% female; %s%% male" % (response["name"], female_percentage, male_percentage), prefixNick=True )