Keypirinha icon indicating copy to clipboard operation
Keypirinha copied to clipboard

WebSuggest for Google Maps

Open gitolicious opened this issue 5 years ago • 2 comments

Unfortunately, the Place Autocomplete API for Google Maps requires an API key from Google Cloud Platform so some additional configuration is needed, but other than that it's rather easy to add.

API documentation: https://developers.google.com/places/web-service/autocomplete API key documentation: https://developers.google.com/places/web-service/get-api-key

websuggest.ini

[item/Maps]
provider = maps

[provider/Maps]
# API example:
#   https://maps.googleapis.com/maps/api/place/autocomplete/json?key=mysupersecretkey&language=de&location=50.9,6.94&radius=10000&input=foo
api_base = https://maps.googleapis.com/maps/api/place/autocomplete/json
api_method = get
api_args =
    key ${var:maps_key}
    language ${var:lang}
    location ${var:maps_location}
    radius ${var:maps_radius}
    input {terms}
api_headers =
    User-Agent Mozilla/5.0
api_parser = user.maps_parser
browse_base = https://www.google.com/maps/search/{terms}
browse_args = 
    hl ${var:lang}

[var]
lang = de
# https://developers.google.com/places/web-service/get-api-key
maps_key = mysupersecretkey
# latitude,longitude
maps_location = 52.51,13.40
# in meters
maps_radius = 10000

Keypirinha/portable/Profile/Packages/WebSuggest/websuggest_user_parsers.py

import json
import traceback

def maps_parser(plugin, provider, response):
    try:
        response = response.decode(encoding="utf-8", errors="strict")

        data = json.loads(response)

        if ("error_message" in data):
            raise Exception(data)

        results = []
        for result in data["predictions"]:
            results.append(result["description"])

        return results
    except:
        plugin.warn("Failed to parse response from provider {}.".format(
                    provider.label))
        traceback.print_exc()
        return []

Too complicated for the average Keypirinhist or worth adding it?

gitolicious avatar Apr 25 '20 22:04 gitolicious

Thanks for sharing :)

It does feel like an extra pinch of user-friendliness would help before it could be integrated in default config. Mainly because of the language, location and radius args triplet.

Feel free to make a PR to the Packages repository though

polyvertex avatar Nov 07 '20 16:11 polyvertex

Thanks for your feedback. If we could extract the language from some setting available in Keypirinha and come up with a reasonable default radius, the necessary settings would be reduced. The elephant in the room though is getting the API key which is way more complicated than setting these three values.

gitolicious avatar Nov 08 '20 08:11 gitolicious