Beispiel #1
0
def queryBatch(queries, taxonomy_ds):
    count = 0  #failure counter
    while count < 3 :
        try:
            attractions = freebase.mqlreadmulti(queries)
            break
        except:
            count += 1
    
    for top in attractions :
        if len(top) > 0 :
            for a in top:
                buildTBranch(a, taxonomy_ds, [] , [a[C_BY]], False)
def bulk_lookup(ids, place=None):
    if not ids:
        return []
    
    cached_details = dict([
        (key.replace('freebase_species:', ''), value)
        for key, value in cache.get_many(
            'freebase_species:%s' % id for id in ids
        ).items()
    ])
    ids_to_retrieve = [id for id in ids if id not in cached_details]
    details = cached_details.values()
    
    if FAKE_MODE:
        freebase_details = FAKE_FREEBASE_BULK
    else:
        if ids_to_retrieve:
            freebase_details = freebase.mqlreadmulti(
                [_species_query(i) for i in ids_to_retrieve]
            )
        else:
            freebase_details = []
    
    for o in freebase_details:
        cache.set('freebase_species:%s' % o['id'], o)
    
    details.extend(freebase_details)
    
    detail_ids = [o['id'] for o in details]
    
    ids_to_sort_by = [id for id in ids if id in detail_ids]
    
    # Ensure details is ordered the same was is ids
    details.sort(key = lambda o: ids_to_sort_by.index(o['id']))
    
    return _annotate_database_objects(
        details,
        place = place
    )
Beispiel #3
0
    def extract_types(self):
        url = "http://api.freebase.com/api/service/mqlread?"
        queries = []
        self.values["results"] = {}
        self.values["results"]["places"] = []
        self.values["results"]["people"] = []
        self.values["results"]["organizations"] = []
        self.values["results"]["events"] = []
        self.values["results"]["subjects"] = []

        for id in self.freebase_ids:
            queries.append(
                {
                    "id": str(id),
                    "guid": None,
                    "type": [],
                    "name": None,
                    "/location/location/geolocation": {"latitude": None, "longitude": None, "optional": True},
                    "/time/event/start_date": None,
                }
            )

        if len(queries) > 0:
            results = freebase.mqlreadmulti(queries)

            for value in self.values["terms"]:
                if "guid" in value:
                    guid = value["guid"]

                    for result in results:
                        if result["id"] == guid:
                            value["type"] = "/subject"
                            value["guid"] = result["guid"][:1]

                            if result["/location/location/geolocation"] is not None:
                                value["geolocation"] = result["/location/location/geolocation"]

                            for type in result["type"]:
                                if type == "/people/person":
                                    value["type"] = type
                                    self.values["results"]["people"].append(value)
                                    break

                                elif (
                                    type == "/location/citytown"
                                    or type == "/location/country"
                                    or type == "/location/administrative_division"
                                    or type == "/location/continent"
                                    or type == "/location/location"
                                ):
                                    value["type"] = type
                                    self.values["results"]["places"].append(value)
                                    break

                                elif (
                                    type == "/organization/organization"
                                    or type == "/organization/club"
                                    or type == "/business/company"
                                ):
                                    value["type"] = type
                                    self.values["results"]["organizations"].append(value)
                                    break

                                elif type == "/time/event" and result["/time/event/start_date"]:
                                    value["type"] = type
                                    self.values["results"]["events"].append(value)
                                    break

                            if value["type"] == "/subject":
                                self.values["results"]["subjects"].append(value)

                            break

        del self.values["terms"]