|
| 1 | +# geomelon |
| 2 | + |
| 3 | +Python client for the [Geomelon API](https://geomelon.dev) — cities, countries, regions, and languages with multilingual support (50+ languages). |
| 4 | + |
| 5 | +- **Zero dependencies** — standard library only (`urllib`) |
| 6 | +- **Python 3.8+** — tested on 3.8 through 3.14 |
| 7 | +- **Fully typed** — dataclass models, `py.typed` marker, works with mypy/pyright |
| 8 | +- Sync API, one sub-client per resource, mirroring the [TypeScript client](https://github.com/930m310n/typescript) |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install geomelon |
| 14 | +``` |
| 15 | + |
| 16 | +## Try it — no API key needed |
| 17 | + |
| 18 | +City autocomplete via the free oneshot endpoint, zero signup: |
| 19 | + |
| 20 | +```python |
| 21 | +from geomelon import GeomelonClient |
| 22 | + |
| 23 | +client = GeomelonClient() # no key |
| 24 | +for city in client.oneshot.search("es", "es", "barc"): |
| 25 | + print(city.name, city.population, city.emoji) |
| 26 | +# Barcelona 1620343 🇪🇸 |
| 27 | +``` |
| 28 | + |
| 29 | +`search(iso, lang, prefix)` takes an ISO 3166-1 alpha-2 country code, a BCP 47 language code, and the city-name prefix as the user typed it — the server normalizes case, punctuation, and diacritics (`"Sant Cugat"`, `"barç"`, and `"barc"` all work). Everything else (full-text search, filters, coordinates, countries, regions, translations) requires an API key. |
| 30 | + |
| 31 | +## Quickstart |
| 32 | + |
| 33 | +Get an API key from [RapidAPI](https://rapidapi.com/hom3chuk/api/geomelon). |
| 34 | + |
| 35 | +```python |
| 36 | +from geomelon import GeomelonClient |
| 37 | + |
| 38 | +client = GeomelonClient(api_key="YOUR_RAPIDAPI_KEY") |
| 39 | + |
| 40 | +# Prefix search with multilingual names |
| 41 | +cities = client.cities.search(name="barc", country_code="ES", preferred_languages="es,en") |
| 42 | +for city in cities: |
| 43 | + print(city.name, city.population, city.country_emoji) |
| 44 | +# Barcelona 1620343 🇪🇸 |
| 45 | + |
| 46 | +# Single city by UUID |
| 47 | +barcelona = client.cities.get("964512d1-f150-4876-87ec-0ba47aef694a") |
| 48 | +print(barcelona.time_zone) # Europe/Madrid |
| 49 | +print(barcelona.translations[0]) # CityTranslation(language='es', name='Barcelona') |
| 50 | + |
| 51 | +# Nearest cities to coordinates |
| 52 | +nearby = client.cities.by_coordinates_closest(41.3828, 2.1769, preferred_languages="es") |
| 53 | + |
| 54 | +# Countries, regions, languages |
| 55 | +spain = client.countries.get("a1e06cc1-817c-429f-84f4-6ab51dac9bfa") |
| 56 | +print(spain.iso_code, len(spain.regions)) |
| 57 | + |
| 58 | +# Distance between two cities (km) |
| 59 | +dist = client.cities.distance(city1="<uuid-1>", city2="<uuid-2>") |
| 60 | +print(dist.distance_km) |
| 61 | + |
| 62 | +# Oneshot: pre-built static prefix search (fastest path) |
| 63 | +results = client.oneshot.search("es", "es", "ba") |
| 64 | +``` |
| 65 | + |
| 66 | +With an API key, oneshot requests route through the RapidAPI gateway and count toward your plan. Pass `free_oneshot=True` to send them to the free keyless host instead: |
| 67 | + |
| 68 | +```python |
| 69 | +client = GeomelonClient(api_key="YOUR_RAPIDAPI_KEY", free_oneshot=True) |
| 70 | +``` |
| 71 | + |
| 72 | +## API surface |
| 73 | + |
| 74 | +| Sub-client | Methods | |
| 75 | +|---|---| |
| 76 | +| `client.cities` | `search`, `get`, `translations`, `settlement_types`, `distance`, `by_coordinates_closest`, `by_coordinates_largest` | |
| 77 | +| `client.countries` | `list`, `get`, `translations`, `regions` | |
| 78 | +| `client.regions` | `list`, `get`, `translations` | |
| 79 | +| `client.languages` | `list`, `get` | |
| 80 | +| `client.oneshot` | `search` | |
| 81 | + |
| 82 | +All method parameters are keyword arguments in `snake_case`; they map 1:1 to the API's camelCase query parameters (`country_code` → `countryCode`). `None` parameters are omitted from the request. Response models are dataclasses with `snake_case` fields; unknown response fields are ignored, so API additions never break older client versions. |
| 83 | + |
| 84 | +## Error handling |
| 85 | + |
| 86 | +All failures raise `GeomelonError`: |
| 87 | + |
| 88 | +```python |
| 89 | +from geomelon import GeomelonClient, GeomelonError |
| 90 | + |
| 91 | +client = GeomelonClient(api_key="YOUR_RAPIDAPI_KEY") |
| 92 | +try: |
| 93 | + client.cities.search(name="barc") |
| 94 | +except GeomelonError as err: |
| 95 | + if err.is_rate_limited: # HTTP 429 |
| 96 | + ... |
| 97 | + print(err.status, err.body, err.url) |
| 98 | +``` |
| 99 | + |
| 100 | +`status` is `None` for network-level failures (DNS, timeout, connection refused). |
| 101 | + |
| 102 | +## Configuration |
| 103 | + |
| 104 | +```python |
| 105 | +GeomelonClient( |
| 106 | + api_key="...", # optional; omit for free-oneshot-only mode |
| 107 | + host="geomelon.p.rapidapi.com", # override for staging/custom gateways |
| 108 | + timeout=30.0, # seconds, per request |
| 109 | + free_oneshot=False, # route oneshot to the keyless host even with a key |
| 110 | +) |
| 111 | +``` |
| 112 | + |
| 113 | +Calling a keyed endpoint on a keyless client raises `GeomelonError` immediately (no network request) with a message pointing to the RapidAPI signup. |
| 114 | + |
| 115 | +## Development |
| 116 | + |
| 117 | +```bash |
| 118 | +python -m unittest discover -s tests -v # run tests (no dev dependencies needed) |
| 119 | +python -m build # build sdist + wheel (pip install build) |
| 120 | +``` |
| 121 | + |
| 122 | +Publishing runs through GitHub Actions with [PyPI trusted publishing](https://docs.pypi.org/trusted-publishers/) on GitHub release — see `.github/workflows/publish.yml`. |
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +MIT |
0 commit comments