Exemplo n.º 1
0
def get_place(geoname_id, language="en"):
    assert geoname_id
    params = [
        ("geonameId", geoname_id),
        ("lang", language),
        ("style", app.config.get("GEONAMES_SEARCH_STYLE", "full")),
    ]
    json_data = geonames_request("getJSON", params)
    return format_geoname_item(json_data)
    def get(self, req, lookup):
        assert req.args.get('name'), {'name': 1}
        params = [
            ('name_startsWith', req.args.get('name')),
            ('lang', req.args.get('lang')),
            ('featureClass', 'A'),
            ('featureClass', 'P'),
        ]

        json_data = geonames_request('search', params)
        data = [format_geoname_item(item) for item in json_data.get('geonames', [])]
        return ListCursor(data)
Exemplo n.º 3
0
def set_dateline_for_translation(item):
    """Set dateline fields required while translation using geoname API
    """
    located = item.get("dateline", {}).get("located")
    if located and not located.get("place"):
        try:
            # required params for geoname API
            params = [
                ("name", located.get("city", "")),
                ("lang", item.get("language", "en")),
                ("style", app.config.get("GEONAMES_SEARCH_STYLE", "full")),
            ]
            for feature_class in app.config.get("GEONAMES_FEATURE_CLASSES",
                                                ["P"]):
                params.append(("featureClass", feature_class.upper()))

            # get geo data from geoname_request
            json_data = geonames_request("search", params)

            formatted_geoname_item = None
            for item_ in json_data.get("geonames", []):
                if (float(item_["lat"]) == located["location"]["lat"]
                        and float(item_["lng"]) == located["location"]["lon"]):
                    formatted_geoname_item = format_geoname_item(item_)
                    break

            if formatted_geoname_item:
                item["dateline"]["located"].update({
                    "state_code":
                    formatted_geoname_item["state_code"],
                    "tz":
                    formatted_geoname_item["tz"],
                    "country_code":
                    formatted_geoname_item["country_code"],
                    "state":
                    formatted_geoname_item["state"],
                    "country":
                    formatted_geoname_item["country"],
                    "code":
                    formatted_geoname_item["code"],
                    "scheme":
                    formatted_geoname_item["scheme"],
                    "location":
                    formatted_geoname_item["location"]
                })
                # set place key required while translation
                item["dateline"]["located"]["place"] = formatted_geoname_item

        except Exception as e:
            logger.exception(
                "Unable to translate dateline for {} item: {}".format(
                    item["guid"], str(e)))
            pass
Exemplo n.º 4
0
    def get(self, req, lookup):
        assert req.args.get('name'), {'name': 1}
        params = [
            ('name_startsWith', req.args.get('name')),
            ('lang', req.args.get('lang')),
        ]

        for feature_class in app.config['GEONAMES_FEATURE_CLASSES']:
            params.append(('featureClass', feature_class.upper()))

        json_data = geonames_request('search', params)
        data = [
            format_geoname_item(item)
            for item in json_data.get('geonames', [])
        ]
        return ListCursor(data)
    def get(self, req, lookup):
        assert req.args.get("name"), {"name": 1}
        params = [
            ("name_startsWith", req.args.get("name")),
            ("lang", req.args.get("lang", "en").split("-")[0]),
            ("style", req.args.get("style",
                                   app.config["GEONAMES_SEARCH_STYLE"])),
        ]

        if req.args.get("featureClass"):
            params.append(("featureClass", req.args.get("featureClass")))
        else:
            for feature_class in app.config["GEONAMES_FEATURE_CLASSES"]:
                params.append(("featureClass", feature_class.upper()))

        json_data = geonames_request("search", params)
        data = [
            format_geoname_item(item)
            for item in json_data.get("geonames", [])
        ]
        return ListCursor(data)