csparpa / pyowm

A Python wrapper around the OpenWeatherMap web API
https://pyowm.readthedocs.io
MIT License
789 stars 171 forks source link

Error: city name must start with a letter #291

Closed Patrick13vrn closed 4 years ago

Patrick13vrn commented 5 years ago

Hi! Could you help me please.

When I try to retrieve a list of relevant cities using cyrillic symbols, I receive something like this:

reg = owm.city_id_registry()
city = 'Москва' # Moscow in Russian
city_list = reg.ids_for(city)
print(city_list)

ValueError: Error: city name must start with a letter

However, there are no any troubles with latin.

geofbaum commented 5 years ago

The error points to Line 215 of the cityidregistry.py file and that specific section is the part of the code that's encoding the city name to be able to access the correct folder through the API. It only searches against latin letters a-z so it will automatically throw the error if the the name has anything other then that in it.

Oddly enough the following works as a direct api call. http://api.openweathermap.org/data/2.5/weather?q=Москва&apiid=your_api_key But that's because your browser is smart enough to encode the symbols correctly. Also if you do a direct latinization, which I believe for Moscow is actually Moskva you'll get the correct return with pyowm.

To fix it there would need to be a way to handle other characters automatically so that they are encoded properly.

csparpa commented 4 years ago

Hi @Patrick13vrn, sorry for the really late answer :-/

The direct call to the web API surely works because of the browser smart that @geofbaum mentioned, but in this case - behind the scenes - OWM is actually querying a real database, not a file. PyOWM instead needs to query for city names on a file, which is taken from OWM (here) and embedded into the library.

Now, it would technically be possible to put non-latin city names into that file, but I need these names to be somehow provided by OWM... I've checked out their website and unfortunately their City ID list only contains latin character names. Moreover, no updates on the list were issued since 2017:

image

Wrapping up: you have two options: