def lookup_location_lat_lon(vars): print "mid", vars["mid"] import freebase result = freebase.mqlread( { "mid": vars["mid"], "type": "/location/location", "/location/location/geolocation": {"latitude": None, "longitude": None}, } ) if result: return result["/location/location/geolocation"] else: # the first thing didn't match, maybe see if it is containedby something that has a geolocation # this isn't necessarily the best thing to do, but it seems to work for now result = freebase.mqlread( { "mid": vars["mid"], "type": "/location/location", "/location/location/containedby": [ { "/location/location/geolocation": {"latitude": None, "longitude": None}, "/location/location/area": None, "sort": "/location/location/area", "limit": 1, } ], } ) if result: return result["/location/location/containedby"][0]["/location/location/geolocation"] else: raise Exception("location not found")
def lookup_location_lat_lon(vars) : print 'mid', vars['mid'] import freebase result = freebase.mqlread({ "mid" : vars['mid'], "type" : "/location/location", "/location/location/geolocation" : { "latitude" : None, "longitude" : None, }, }) if result : return result['/location/location/geolocation'] else : # the first thing didn't match, maybe see if it is containedby something that has a geolocation # this isn't necessarily the best thing to do, but it seems to work for now result = freebase.mqlread({ "mid" : vars['mid'], "type" : "/location/location", "/location/location/containedby" : [{ "/location/location/geolocation" : { "latitude" : None, "longitude" : None, }, "/location/location/area" : None, "sort" : "/location/location/area", "limit" : 1, }], }) if result : return result['/location/location/containedby'][0]['/location/location/geolocation'] else : raise Exception('location not found')
def get(self): VIEW = "?id=" BASE = "http://api.freebase.com/api/trans/image_thumb" OPTIONS = "?maxheight=300" id = cgi.escape(self.request.get('id')) if id != "": info_query = { "id" : id, "name" : None, } info = freebase.mqlread(info_query) user = users.get_current_user() search = Search() if user: search.author = user search.id = id search.name = info.name search.url = VIEW + id search.put() query = { "id": id, "/type/reflect/any_reverse": [{ "id" : None, "/common/topic/image": [{ "id": None, "optional": True, "limit": 5 }] }] } results = freebase.mqlread(query) images = dict() for thing in results["/type/reflect/any_reverse"]: imgroup = thing['/common/topic/image'] if len(imgroup) > 0: for image in imgroup: images[VIEW + thing["id"]] = BASE + image['id'] + OPTIONS #template_values = {"images": images} #path = os.path.join(os.path.dirname(__file__), 'images.html') #self.response.out.write(template.render(path, template_values)) final = "" for page, image in images.items(): final += "<a href='" + page + "'><img src='" + image + "' alt='' /></a>" if len(final) == 0: final = "Sorry, there are no images to show." self.response.out.write(final)
def test_mqlreaditer(self): filmq = [{"id": None, "initial_release_date>=": "2009", "name": None, "type": "/film/film"}] r0 = freebase.mqlreaditer(filmq) r1 = freebase.mqlreaditer( filmq[0] ) # The difference between [{}] and []. mqlreaditer should be able to handle both self.assertNotEqual(r0, None) self.assertEqual([a for a in r0], [b for b in r1]) # and let's test it for mqlread, just in case # actually, for mqlread, it must be [{}], because there are lots of elements m0 = freebase.mqlread(filmq) m1 = lambda: freebase.mqlread(filmq[0]) self.assertRaises(MetawebError, m1) self.assertNotEqual(m0, None)
def GetType(self, keyword): if not keyword: return [[], []] #search for the id data = freebase.search(keyword, limit=1) if not data: return [[], []] item_id = [d['id'] for d in data if 'id' in d][0] query = {"id": item_id, "type": [{}]} response = freebase.mqlread(query) types = response['type'] names = [] base_names = [] for k in types: names.append(k['name']) type_x = ''.join(k['id']).split('/') if len(type_x) > 1: base_names.append(type_x[1]) names_set = set(names) base_names_set = set(base_names) names = [] base_names = [] for y in names_set: names.append(y) for y in base_names_set: base_names.append(y) all_list = [] all_list.append(names) all_list.append(base_names) return all_list
def view_page(request, page_id): try: post = BlogPost.objects.get(pk = page_id) except BlogPost.DoesNotExist: raise Http404 baseT = ThreadedComment.objects.get(pk = post.baseThreadID) baseSet = ThreadedComment.objects.filter(tree_path__contains = ('%010i' % int(baseT.comment_ptr_id))) newSet = ThreadedComment.objects.filter(tree_path__contains = ('%010i' % int(baseT.comment_ptr_id))) newSet = newSet.order_by('-submit_date') latestTC = newSet[0] post.postNumComments = baseSet.count() -1 post.save() postSet = BlogPost.objects.filter(pk = page_id) tagSet = list() try: tups = TagUserPost.objects.filter(post = post) for tup in tups: tag = Tag.objects.get(pk = tup.tag.mid) tagSet.append(tag) imageID = "NULL" if len(tagSet)>0: headTag = tagSet[0] query=[{"mid":headTag.mid,"/common/topic/image" : [{"id" : None,"optional": True}]}] result = freebase.mqlread(query,extended=True) if(len(result[0]["/common/topic/image"]))>0: imageID = result[0]["/common/topic/image"][0]["id"] except TagUserPost.DoesNotExist: pass return render_to_response( "view_page.html", {"post":post, "postSet": postSet ,'base' : baseSet,'user' : request.user, 'tags' : tagSet,'latestTC' : latestTC.comment_ptr_id,'imageID': imageID},context_instance = RequestContext(request))
def get(self): # Get username username = self.request.get("username") # Get top 50 albums from last.fm lastFMurl = ( "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&" + urllib.urlencode({"user": username}) + "&api_key=b25b959554ed76058ac220b7b2e0a026" ) lastFMresponse = urlfetch.fetch(lastFMurl) lastFMdom = xml.dom.minidom.parseString(lastFMresponse.content) # If username not found, return error message if len(lastFMdom.getElementsByTagName("artist")) == 0: self.response.out.write( simplejson.dumps({"failure": "No Last.FM user by the username:"******"artist"): names.append(artist.getElementsByTagName("name")[0].childNodes[0].data.encode("ascii", "ignore")) # Fidn artist by name in metabase mqlQuery = [ { "type": "/music/artist", "/common/topic/image": [{"id": None, "limit": 1}], "name": None, "name|=": names, "origin": [{"name": None, "geolocation": [{"latitude": None, "longitude": None}]}], } ] mqlResult = freebase.mqlread(mqlQuery) self.response.out.write(simplejson.dumps(mqlResult))
def freebase_id_from_parl_id(): import freebase import time for info in PoliticianInfo.sr_objects.filter(schema='parl_id').order_by('value'): if PoliticianInfo.objects.filter(politician=info.politician, schema='freebase_id').exists(): continue query = { 'type': '/base/cdnpolitics/member_of_parliament', 'id': [], 'key': { 'namespace': '/source/ca/gov/house', 'value': info.value } } result = freebase.mqlread(query) print "result: %s" % result #time.sleep(1) if not result: try: print "Nothing for %s (%s)" % (info.value, info.politician) except: pass else: freebase_id = result['id'][0] PoliticianInfo(politician=info.politician, schema='freebase_id', value=freebase_id).save() print "Saved: %s" % freebase_id
def queryCityTown(taxonomy, tourist_locations): logger.debug('queryCityTown') logger.debug(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) query = [{ "type": "/location/citytown", "name": None, "id":None, "limit": 10, "/common/topic/alias": [], "/common/topic/image": [{ "id": None, "optional": True, "/common/image/size": { "/measurement_unit/rect_size/x" : None, "/measurement_unit/rect_size/y" : None } }] }] if fwiki.settings.DEBUG == True: print 'in test mode' r = freebase.mqlread(query) else: print 'in production mode' count = 0 while count < 3 : try: r = freebase.mqlreaditer(query) break except: count += 1 for i in r: buildTBranch(i,taxonomy,tourist_locations)
def getBoardMemberships(id=None, name=None): """Get al boardmemberships for a person.""" qGetBoardMemberships = { "/business/board_member/organization_board_memberships": [{ "/organization/organization_board_membership/organization": { "id": None, "name": None } }], "id": None, "name": None, "type": "/business/board_member" } qGetBoardMemberships['id'] = id qGetBoardMemberships['name'] = name result = fb.mqlread(qGetBoardMemberships) companies = {} for m in result['/business/board_member/organization_board_memberships']: cid = m['/organization/organization_board_membership/organization'][ 'id'] clbl = m['/organization/organization_board_membership/organization'][ 'name'] companies[cid] = clbl rootnode = {result['id']: result['name']} return (rootnode, companies)
def query_parser(item_id): # summary, photo and properties query[0]['source']['id'] = item_id response = freebase.mqlread(query) summary = '' image = '' name = '' more_url = '' # key:string value:dedup list properties = {} t_format_summary = None t_format_wikiurl = Format_WikiURL(item_id) t_format_wikiurl.start() for r in response: # summary if r['master_property'][0]['id'] == '/common/topic/article': t_format_summary = Format_Summary(r['target'][0]['id']) t_format_summary.start() # image elif r['master_property'][0]['id'] == '/common/topic/image': image = format_image(r['target'][0]['id']) # properties else: if r['target_value'] and r['target_value'][0]: if r['master_property'][0]['name'] == 'Name': if r['target'][0]['name'] != 'English': continue properties = format_properties(properties, r['master_property'][0]['name'], r['target_value'][0]['value']) elif r['target'] and r['target'][0]: properties = format_properties(properties, r['master_property'][0]['name'], r['target'][0]['name']) else: properties = format_properties(properties, r['master_property'][0]['name'], '') if t_format_summary: t_format_summary.join() t_format_wikiurl.join() if t_format_summary.result: summary = t_format_summary.result if t_format_wikiurl.result: more_url = t_format_wikiurl.result if 'Name' in properties: name = properties.pop('Name')[0] if 'Permission' in properties: properties.pop('Permission') if 'Type' in properties: properties.pop('Type') if 'Hero image ID' in properties: properties.pop('Hero image ID') else: summary = '' image = '' name = '' more_url = '' properties = {} return summary, image, name, more_url, properties
def GetType(self, keyword): if not keyword: return [[],[]] #search for the id data = freebase.search(keyword, limit=1) if not data: return [[],[]] item_id = [d['id'] for d in data if 'id' in d][0] query = {"id":item_id, "type":[{}]} response = freebase.mqlread(query) types = response['type'] names = [] base_names = [] for k in types: names.append(k['name']) type_x = ''.join(k['id']).split('/'); if len(type_x)>1: base_names.append(type_x[1]) names_set = set(names) base_names_set = set(base_names) names = [] base_names = [] for y in names_set: names.append(y) for y in base_names_set: base_names.append(y) all_list = [] all_list.append(names) all_list.append(base_names) return all_list
def test_mqlreaditer(self): filmq = [{'id': None, 'initial_release_date>=': '2009', 'name': None, 'type': '/film/film' }] r0 = freebase.mqlreaditer(filmq) r1 = freebase.mqlreaditer(filmq[0]) # The difference between [{}] and []. mqlreaditer should be able to handle both self.assertNotEqual(r0, None) self.assertEqual([a for a in r0], [b for b in r1]) # and let's test it for mqlread, just in case # actually, for mqlread, it must be [{}], because there are lots of elements m0 = freebase.mqlread(filmq) m1 = lambda : freebase.mqlread(filmq[0]) self.assertRaises(MetawebError, m1) self.assertNotEqual(m0, None)
def lookup_albums_by_musician(vars): import freebase result = freebase.mqlread( {"mid": vars["artist_mid"], "type": "/music/artist", "album": [{"name": None, "mid": None}]} ) # print result return result["album"]
def lookup_birthplace(vars): import freebase result = freebase.mqlread( {"mid": vars["person_mid"], "type": "/people/person", "place_of_birth": {"mid": None, "name": None}} ) # print result return result["place_of_birth"]
def test_unsafe(self): kurt = "/en/kurt_vonnegut" self.assertRaises(MetawebError, lambda: freebase.unsafe(kurt)) r = freebase.mqlread({"id": kurt, "/common/topic/article": [{"id": None, "optional": True, "limit": 1}]}) unsafe = freebase.unsafe(r["/common/topic/article"][0].id) self.assertNotEqual(len(unsafe), 0)
def run(self): query = [{'id':'%s' % self.freebase_id, 'wiki_en:key':{'/type/key/namespace':'/wikipedia/en_id', 'value':None, 'optional':True}}] response = freebase.mqlread(query) if response[0] and 'wiki_en:key' in response[0]: wiki_id = str(response[0]['wiki_en:key']['value']) self.result = more_tag_template % wiki_id else: self.result = ''
def test_unsafe(self): kurt = "/en/kurt_vonnegut" self.assertRaises(MetawebError, lambda: freebase.unsafe(kurt)) r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) unsafe = freebase.unsafe(r["/common/topic/article"][0].id) self.assertNotEqual(len(unsafe), 0)
def test_ridiculously_long_write(self): q = [{ "id":None, "id|=":["/guid/9202a8c04000641f80000000000" + str(a) for a in range(10000,10320)] }] self.assert_(len(str(q)), 1024) self.assertNotEqual(len(freebase.mqlread(q)), 0)
def test_trans_image_thumb(self): kurt = "/en/kurt_vonnegut" r = freebase.mqlread({"id":kurt, "/common/topic/image":[{"id":None, "optional":True, "limit":1}]}) imageid = r["/common/topic/image"][0].id rawimage = freebase.raw(imageid) thumbedimage = freebase.image_thumb(imageid, maxheight=99) self.assertNotEqual(rawimage, thumbedimage)
def queryDivisions(taxonomy, tourist_locations): logger.debug('queryDivisions') logger.debug(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) query =[{ "type": "/location/administrative_division", "name": None, "/common/topic/image" : [ { "id": None, "optional" : True, "/common/image/size": { "/measurement_unit/rect_size/x" : None, "/measurement_unit/rect_size/y" : None } } ], "id": None, "/common/topic/alias": [], "/location/location/containedby": [{ "type": "/location/country", "name": None, "id": None, "/common/topic/alias": [], "/location/location/containedby": [{ "type": "/location/continent", "optional": True, "name": None, "id": None, "/common/topic/alias": [] }] }] }] count = 0 if fwiki.settings.DEBUG == True: print 'in test mode' iter = freebase.mqlread(query) else: print 'in production mode' while count < 3 : try: iter = freebase.mqlreaditer(query) break except: count += 1 for i in iter: contained_by = [] for c in i[C_BY]: for cc in c[C_BY]: buildTBranch(cc, taxonomy, tourist_locations) contained_by.append(cc[NAME].lower()) buildTBranch(c, taxonomy, tourist_locations, contained_by ) contained_by.append(c[NAME].lower()) buildTBranch(i,taxonomy,tourist_locations, contained_by)
def test_trans_image_thumb(self): kurt = "/en/kurt_vonnegut" r = freebase.mqlread({"id": kurt, "/common/topic/image": [{"id": None, "optional": True, "limit": 1}]}) imageid = r["/common/topic/image"][0].id rawimage = freebase.raw(imageid) thumbedimage = freebase.image_thumb(imageid, maxheight=99) self.assertNotEqual(rawimage, thumbedimage)
def wikipedia_from_freebase(): import freebase for info in PoliticianInfo.sr_objects.filter(schema="freebase_id"): query = {"id": info.value, "key": [{"namespace": "/wikipedia/en_id", "value": None}]} result = freebase.mqlread(query) if result: # freebase.api.mqlkey.unquotekey wiki_id = result["key"][0]["value"] info.politician.set_info("wikipedia_id", wiki_id)
def execute(self, query): import freebase fbResult = freebase.mqlread(query.freebaseQuery) result = {} for key in query.selectedKeys: result[key] = fbResult[key] return result
def execute(self, query): import freebase fbResult = freebase.mqlread(query.freebaseQuery) result = {} for key in query.selectedKeys: result[key] = fbResult[key] return result
def test_trans_raw(self): kurt = "/en/kurt_vonnegut" self.assertRaises(MetawebError, lambda: freebase.raw(kurt)) r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) raw = freebase.raw(r["/common/topic/article"][0].id) self.assertNotEqual(len(raw), 0) # trans should also work trans = freebase.trans(r["/common/topic/article"][0].id) self.assertEqual(trans, raw)
def lookup_members_in_group(vars): # TODO: /music/musical_group/member doesn't have a name import freebase result = freebase.mqlread( { "mid": vars["mid"], "type": "/music/musical_group", "member": [{"/music/group_membership/member": {"mid": None, "name": None}}], } ) return [member[u"/music/group_membership/member"] for member in result["member"]]
def test_trans_raw(self): kurt = "/en/kurt_vonnegut" self.assertRaises(MetawebError, lambda: freebase.raw(kurt)) r = freebase.mqlread({"id": kurt, "/common/topic/article": [{"id": None, "optional": True, "limit": 1}]}) raw = freebase.raw(r["/common/topic/article"][0].id) self.assertNotEqual(len(raw), 0) # trans should also work trans = freebase.trans(r["/common/topic/article"][0].id) self.assertEqual(trans, raw)
def lookup_albums_by_musician(vars) : import freebase result = freebase.mqlread({ "mid" : vars['artist_mid'], "type" : "/music/artist", "album" : [{ "name" : None, "mid" : None, }] }) #print result return result['album']
def lookup_birthplace(vars) : import freebase result = freebase.mqlread({ "mid" : vars['person_mid'], "type" : "/people/person", "place_of_birth" : { "mid" : None, "name" : None, }, }) #print result return result['place_of_birth']
def run(self): query = [ { "id": "%s" % self.freebase_id, "wiki_en:key": {"/type/key/namespace": "/wikipedia/en_id", "value": None, "optional": True}, } ] response = freebase.mqlread(query) if response[0] and "wiki_en:key" in response[0]: wiki_id = str(response[0]["wiki_en:key"]["value"]) self.result = more_tag_template % wiki_id else: self.result = ""
def sparql(request): token = list() if request.method == "POST": # fSession = HTTPMetawebSession("api.freebase.com") r = list() for tokenItem in request.POST.getlist('token'): query=[{"id": tokenItem, "mid":None, "/common/topic/notable_for": [{}],"name":[] }] result = freebase.mqlread(query,extended=True) return render_to_response("sparql.html",{"list":r}) return render_to_response("sparql.html",{})
def test_freebase_dot_read(self): query = {'type':'/music/artist','guid':[{}],'name':'Sting', 'album':[{}]} result = freebase.mqlread(query) self.assertNotEqual(None, result) self.assert_(result.has_key('guid')) self.assert_(result.has_key('type')) self.assert_(result.has_key('name')) self.assert_(result.has_key('album')) self.assertEqual(type([]), type(result['album'])) self.assert_(len(result['album']) > 0) self.assertEqual( 'Sting', result['name']) self.assertEqual('#9202a8c04000641f8000000000092a01', result['guid'][0]['value'])
def test_freebase_dot_read(self): query = {"type": "/music/artist", "guid": [{}], "name": "Sting", "album": [{}]} result = freebase.mqlread(query) self.assertNotEqual(None, result) self.assert_(result.has_key("guid")) self.assert_(result.has_key("type")) self.assert_(result.has_key("name")) self.assert_(result.has_key("album")) self.assertEqual(type([]), type(result["album"])) self.assert_(len(result["album"]) > 0) self.assertEqual("Sting", result["name"]) self.assertEqual("#9202a8c04000641f8000000000092a01", result["guid"][0]["value"])
def lookup_members_in_group(vars) : # TODO: /music/musical_group/member doesn't have a name import freebase result = freebase.mqlread({ "mid" : vars['mid'], "type" : '/music/musical_group', "member" : [{ "/music/group_membership/member" : { "mid" : None, "name" : None, } }] }) return [member[u'/music/group_membership/member'] for member in result['member']]
def parse_fb(self): createurs = Artiste.objects.all() for createur in createurs: try: print "Scanne l'artiste "+str(createur.name_id).encode('<utf-8>') requete = { "type": "/people/person", "name": createur.name.encode('<utf8>'), "date_of_birth" : None, "place_of_birth" : None, '/common/topic/description' : None, '/common/topic/official_website' : [{}], '/common/topic/topic_equivalent_webpage' : [{}], "profession" : [] } essai = freebase.mqlreaditer(requete, asof=None) reponses = 0 if essai is not None: for i in essai: reponses = reponses+1 print "Freebase trouve "+str(reponses).encode('<utf-8>')+" réponses pour ce créateur" if reponses==1: print "debut de collecte des informations" i = freebase.mqlread(requete, asof=None) if i["date_of_birth"] is not None and createur.date_naissance is None: birth = i['date_of_birth'] birth = birth.split('-') if len(birth)==1: createur.date_naissance = datetime.datetime(int(birth[0]), 1, 1, 0, 0) else: createur.date_naissance = datetime.datetime(int(birth[0]), int(birth[1]), int(birth[2]), 0, 0) if i["place_of_birth"] is not None and createur.lieu_naissance is None: createur.lieu_naissance = i['place_of_birth'] #chopper le site perso if len(i['/common/topic/official_website'])>0: url1 = i['/common/topic/official_website'][0]['value'] test = LienWeb.objects.filter(URL = url1) if len(test) == 0: siteweb = LienWeb(URL = url1) siteweb.name = "site personnel de "+str(createur.name) siteweb.save() createur.productions_web.add(siteweb) createur.save() print "fini de chercher dans freebase pour "+str(createur.name_id).encode('<utf-8>') except: print "probleme avec l'artiste "+str(createur.name_id).encode('<utf-8>')
def wikipedia_from_freebase(): import freebase for info in PoliticianInfo.sr_objects.filter(schema='freebase_id'): query = { 'id': info.value, 'key': [{ 'namespace': '/wikipedia/en_id', 'value': None }] } result = freebase.mqlread(query) if result: # freebase.api.mqlkey.unquotekey wiki_id = result['key'][0]['value'] info.politician.set_info('wikipedia_id', wiki_id)
def get_types(self): '''docs''' if not self.keyword: return None # search for the id data = freebase.search(self.keyword, limit=1) if data: item_id = [d['id'] for d in data if 'id' in d][0] query = {"id": item_id, "type": [{}], 'key': [], 'name': None} response = freebase.mqlread(query) return self.entity_builder(response) else: return None
def get_types(self): '''docs''' if not self.keyword: return None # search for the id data = freebase.search(self.keyword, limit=1) if data: item_id = [d['id'] for d in data if 'id' in d][0] query = {"id":item_id, "type":[{}], 'key':[], 'name':None} response = freebase.mqlread(query) return self.entity_builder(response) else: return None
def get(self): # Get username username = self.request.get("username") # Get top 50 albums from last.fm lastFMurl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&' + urllib.urlencode( {'user': username}) + '&api_key=b25b959554ed76058ac220b7b2e0a026' lastFMresponse = urlfetch.fetch(lastFMurl) lastFMdom = xml.dom.minidom.parseString(lastFMresponse.content) # If username not found, return error message if len(lastFMdom.getElementsByTagName('artist')) == 0: self.response.out.write( simplejson.dumps({ 'failure': 'No Last.FM user by the username:'******'artist'): names.append( artist.getElementsByTagName('name') [0].childNodes[0].data.encode('ascii', 'ignore')) # Fidn artist by name in metabase mqlQuery = [{ 'type': '/music/artist', "/common/topic/image": [{ "id": None, "limit": 1 }], 'name': None, 'name|=': names, "origin": [{ "name": None, "geolocation": [{ "latitude": None, "longitude": None }] }] }] mqlResult = freebase.mqlread(mqlQuery) self.response.out.write(simplejson.dumps(mqlResult))
def retrieve_freebase_desc(name, stype): if not freebase: return "" print "Retrieving the description for %s" % name fb_type = settings.FREEBASE_TYPE_MAPPINGS.get(stype, None) value, data = None, "" try: value = freebase.mqlread({"name": name, "type": fb_type or [], FREEBASE_DESC_KEY: [{"id": None}]}) except: try: values = freebase.mqlreaditer({"name": name, "type": fb_type or [], FREEBASE_DESC_KEY: [{"id": None}]}) value = values.next() except Exception, e: # Only print error as freebase is only optional if settings.ST_DEBUG: print "Error using `freebase`: %s" % e
def filter(self, prop, ids, rhs, reverse=False): id_or_equals = set() if reverse: prop = "!%s" % prop for nodenum, fbid in ids: id_or_equals.add(fbid) query = {"id" : null, "id|=": list(id_or_equals)} query[prop] = rhs result = freebase.mqlread([query]) out_set = set() out_data = {} for r in result: r_id = r["id"] for nodenum, fbid in ids: if fbid == r_id: out_set.add(nodenum) out_data[nodenum] = r[prop] return (out_set, out_data)
def retrieve_freebase_name(name, stype): if not freebase: return name search_key = fix_name_for_freebase(name) fb_type = settings.FREEBASE_TYPE_MAPPINGS.get(stype, None) value = None try: # Try to get the exact match value = freebase.mqlread({"name": None, "type": fb_type or [], "key": {"value": search_key}}) except: try: # Try to get a results has a generator and return its top result values = freebase.mqlreaditer({"name": None, "type": fb_type or [], "key": {"value": search_key}}) value = values.next() except Exception, e: # Only print error as freebase is only optional if settings.ST_DEBUG: print "Error using `freebase`: %s" % e
def ensure_freebase_ids_are_guids(): for species in Species.objects.exclude(freebase_id__startswith='/guid/'): guid = freebase.mqlread({ 'id': species.freebase_id, 'guid': None })['guid'] new_freebase_id = '/guid/%s' % guid[1:] # Does that guid already exist? If so, a merge is needed if Species.objects.filter(freebase_id = new_freebase_id): dupe = Species.objects.get(freebase_id = new_freebase_id) print "Duplicate: %s:%s and %s:%s - you must fix that first" % ( species.pk, species.slug, dupe.pk, dupe.slug ) continue species.freebase_id = new_freebase_id species.save() print " %s freebase id is now %s" % (species, new_freebase_id)
def getBGGIDFromFB(game_id): """Returns the Board Game Geek Game ID and Freebase MID from Freebase. """ # This query will return None if there is no BGG key/id. query = { "id": str(game_id), "mid": None, "key" : { "namespace" : "/user/pak21/boardgamegeek/boardgame", "value" : None, "optional": True } } result = freebase.mqlread(query, extended=True) if result.key is not None: # The FB game entry has a BGG id game_ids = {"bgg_id":result.key.value, "mid":result.mid} logging.info(TRACE+'getBGGIDFromFB():: bgg_id = ' +game_ids["bgg_id"]) return game_ids else: game_ids = {"bgg_id":None, "mid":result.mid} logging.info(TRACE+'getBGGIDFromFB():: bgg_id = None') return game_ids
def getFreebaseData(source,value): if source == "twitter": result = freebase.mqlread({"key": [{ "namespace": "/authority/twitter", "value": value}], "name": None, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/gender": None, "ns0:key": [{}]}) elif source == 'wikipedia': result = freebase.mqlread({"key": [{ "namespace": "/wikipedia/en", "value": value}], "name": None, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/gender": None, "ns0:key": [{}]}) if result == None: result = freebase.mqlread({"key": [{ "namespace": "/wikipedia/en_id", "value": value}], "name": None, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/gender": None, "ns0:key": [{}]}) return result elif source == 'imdb': if 'http' in value: value = value.split('/name/')[1].split('/')[0] return freebase.mqlread({"key": [{ "namespace": "/authority/imdb/name", "value": value}], "name": None, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/gender": None, "ns0:key": [{}]}) #make sure it was quotekey elif source == 'baseballalmanac': result = freebase.mqlread({"key": [{ "namespace": "/source/baseball_almanac/players", "value": value}], "name": None, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/profession": [{}], "/people/person/gender": None, "ns0:key": [{}] }) ###add checks to see if empty???? elif source == 'name': return freebase.mqlread({"name": value, "/people/person/place_of_birth": None, "/people/person/nationality": [{}], "/people/person/date_of_birth": None, "/people/person/profession": [{}], "/people/person/gender": None, "ns0:key": [{}] }) else: return None
def getBoardMembers(id=None, name=None): """Return all the board members for a company.""" # MQL query, in the form of a Python datastructure qGetBoardMembers = { "/organization/organization/board_members": [{ "/organization/organization_board_membership/member": { "name": None, "id": None } }], "id": None, "name": None } # Assign the id and/or the name/label in the query qGetBoardMembers['id'] = id qGetBoardMembers['name'] = name result = fb.mqlread(qGetBoardMembers) # construct a tuple with two dictionaries (rootnode and related nodes) # a node has a { id : label } form persons = {} for p in result['/organization/organization/board_members']: pid = p['/organization/organization_board_membership/member']['id'] plabel = p['/organization/organization_board_membership/member'][ 'name'] persons[pid] = plabel rootnode = {result['id']: result['name']} return (rootnode, persons)
def freebase_id_from_parl_id(): import freebase import time for info in PoliticianInfo.sr_objects.filter(schema="parl_id").order_by("value"): if PoliticianInfo.objects.filter(politician=info.politician, schema="freebase_id").exists(): continue query = { "type": "/base/cdnpolitics/member_of_parliament", "id": [], "key": {"namespace": "/source/ca/gov/house", "value": info.value}, } result = freebase.mqlread(query) print "result: %s" % result # time.sleep(1) if not result: try: print "Nothing for %s (%s)" % (info.value, info.politician) except: pass else: freebase_id = result["id"][0] PoliticianInfo(politician=info.politician, schema="freebase_id", value=freebase_id).save() print "Saved: %s" % freebase_id
def main(): ''' docs ''' # read in n3 file g.parse(INPUT, format='n3') fb_namespace = rdflib.namespace.Namespace('http://rdf.freebase.com/ns/') g.bind('freebase', 'http://rdf.freebase.com/ns/') # uncategorized terms f = open('uncategorized', 'w') for c in g.subject_objects(predicate=rdflib.term.URIRef( u'http://www.w3.org/2002/07/owl#equivalentClass')): if 'http://dbpedia.org/ontology/' in c[1]: freebase_type_added = False for cc in g.objects( subject=c[0], predicate=rdflib.term.URIRef( u'http://www.w3.org/2002/07/owl#equivalentClass')): if 'http://rdf.freebase.com/ns/' in cc: freebase_type_added = True break if not freebase_type_added: #get the dbpedia type name dbpedia_type = c[1].replace('http://dbpedia.org/ontology/', '') searchet_type = c[0].replace( 'http://searchet.baidu.com/ontology#', '') freebase_guid = rclient.get(dbpedia_type) if freebase_guid: # find the freebase type name QUERY['guid'] = freebase_guid fb_feedback = freebase.mqlread(QUERY) # corresponding to id freebase_type = '' # corresponding to name freebase_type_name = '' if 'id' in fb_feedback: freebase_type = '.'.join( fb_feedback['id'].split('/')[1:]) freebase_type_name = fb_feedback['name'] # add freebase type to the ontology if freebase_type: if dbpedia_type != freebase_type_name or searchet_type != freebase_type_name: print '%s // %s' % (dbpedia_type, freebase_type) choice = raw_input('confirmed y/n?: ').lower() if choice == 'y' or choice == 'yes': g.add(( c[0], rdflib.term.URIRef( u'http://www.w3.org/2002/07/owl#equivalentClass' ), fb_namespace[freebase_type])) g.commit() else: freebase_found = freebase_limited_search( dbpedia_type) candidate_type_names = [ c['name'] for c in freebase_found ] if candidate_type_names: choice = raw_input( '[%s] enter nunber or \'no\': ' % ', '.join([ '%s(%i)' % (t, i) for i, t in enumerate(candidate_type_names) ])).lower() # show all the types if choice != 'n' and choice != 'no': if 'type' in freebase_found[int( choice)]: for t in freebase_found[int( choice)]['type']: print ': %s' % t['id'] else: f.write('%s\n' % dbpedia_type) print else: f.write('%s\n' % dbpedia_type) else: g.add(( c[0], rdflib.term.URIRef( u'http://www.w3.org/2002/07/owl#equivalentClass' ), fb_namespace[freebase_type])) g.commit() else: # cannot find a linked freebase type freebase_found = freebase_limited_search(dbpedia_type) candidate_type_names = [c['name'] for c in freebase_found] if candidate_type_names: print dbpedia_type choice = raw_input( '[%s] enter nunber or \'no\': ' % ', '.join([ '%s(%i)' % (t, i) for i, t in enumerate(candidate_type_names) ])).lower() # show all the types if choice != 'n' and choice != 'no': if 'type' in freebase_found[int(choice)]: for t in freebase_found[int(choice)]['type']: print ': %s' % t['id'] else: f.write('%s\n' % dbpedia_type) print else: f.write('%s\n' % dbpedia_type) f.close() g.serialize(destination=OUTPUT, format='n3', encoding='utf-8')
def run(self): data = freebase.mqlread(self.query) # only one id is necessary if data and 'id' in data: self.result.append(data['id'][0]['value'])
query = [{ "name": "Karl Popper", "guid": None, "type": "/influence/influence_node", "influenced": [{ "guid": None, "name": None, "type": "/influence/influence_node", "influenced_by": [{ "type": "/people/person", "guid": None, "name": None, "date_of_birth": None, "nationality": [], "religion": [], "place_of_birth":[{ "name": None, "/location/location/geolocation": { "latitude" : None, "longitude" : None } }] }] }] }] result = freebase.mqlread(query) pprint(result,indent=1)
def get_category(self, kwd): category = "" #FILM query1 = { "type": "/film/film", "name~=": "*" + kwd + "*", "return": "count" } result1 = freebase.mqlread(query1) #FILM ACTOR query2 = { "type": "/film/actor", "name~=": "*" + kwd + "$", "return": "count" } result2 = freebase.mqlread(query2) #DIRECTOR query3 = { "type": "/film/director", "name~=": "*" + kwd + "$", "return": "count" } result3 = freebase.mqlread(query3) #CHARACTER query4 = { "type": "/film/character", "name~=": "*" + kwd + "$", "return": "count" } result4 = freebase.mqlread(query4) #SERIES query5 = { "type": "/film/series", "name~=": "*" + kwd + "$", "return": "count" } result5 = freebase.mqlread(query5) #PRODUCER query6 = { "type": "/film/producer", "name~=": "*" + kwd + "$", "return": "count" } result6 = freebase.mqlread(query6) #WRITER query7 = { "type": "/film/writer", "name~=": "*" + kwd + "$", "return": "count" } result7 = freebase.mqlread(query7) film_count = result1 + result2 + result3 + result4 + result5 + result6 + result7 #import pdb;pdb.set_trace(); ######################################################################################### #BOOK query8 = { "type": "/book/book", "name~=": "*" + kwd + "*", "return": "count" } #result8 = freebase.mqlread(query8) query9 = { "type": "/book/short_story", "name~=": "*" + kwd + "*", "return": "count" } #result9 = freebase.mqlread(query9) query10 = { "type": "/book/book_character", "name~=": "*" + kwd + "*", "return": "count" } #result10 = freebase.mqlread(query10) #books_count = result8 +result9 + result10 #######################PEOPLE######################################### query11 = { "type": "/people/person", "name~=": "*" + kwd + "*", "return": "count" } #result11 = freebase.mqlread(query11) #people_count = result11 ################################PLACES########################33 query12 = { "type": "/location/country", "name~=": "*" + kwd + "*", "return": "count" } #result12 = freebase.mqlread(query12) query13 = { "type": "/location/location", "name~=": "*" + kwd + "*", "return": "count" } #result13 = freebase.mqlread(query13) query14 = { "type": "/location/region", "name~=": "*" + kwd + "*", "return": "count" } #result14 = freebase.mqlread(query14) query15 = { "type": "/location/us_state", "name~=": "*" + kwd + "*", "return": "count" } #result15 = freebase.mqlread(query15) query16 = { "type": "/location/in_city", "name~=": "*" + kwd + "*", "return": "count" } # result16 = freebase.mqlread(query16) query17 = { "type": "/location/in_state", "name~=": "*" + kwd + "*", "return": "count" } # result17 = freebase.mqlread(query17) # location_count = result12 + result13 + result14 + result15 + result16 +result17 if film_count >= 1: category = "film" #if books_count >= 1 : # category.append("books") #if people_count >= 1: # category.append("people") #if location_count >= 1 : # category.append("location") print category return category
db = connection.dataset_db cursor1 = None query_film = [{ "type": "/film/film", #"film/actor":[], #"director":[], "/common/topic/article": { "id": None, "optional": True, "limit": 1 }, "name": [], "limit": lim }] results_film = freebase.mqlread(query_film) query_book = [{ "type": "/book/book", "/common/topic/article": { "id": None, "optional": True, "limit": 1 }, "name": [], "limit": lim }] results_book = freebase.mqlread(query_book) query_people = [{ "type": "/people/person",
def get(self): query = { "id" : cgi.escape(self.request.get('id')), "name" : None} r = freebase.mqlread(query) self.response.out.write(r.name)