I'm currently reading O'Reilly's Creating Apps in Kivy and there's an example that I can't get to work correctly because at the time he wrote the book openWeatherMap didn't require the API key (APPID) but now it does. I'm a novice programmer and don't know how to change the code so it will work.
This is the main.py source code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
import json
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
def search_location(self):
search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"
search_url = search_template.format(self.search_input.text)
request = UrlRequest(search_url, self.found_location)
def found_location(self, request, data):
data = json.loads(data.decode()) if not isinstance(data, dict) else data
cities = ["{} ({})".format(d['name'], d['sys']['country'])
for d in data['list']]
self.search_results.item_strings = cities
print("\n".join(cities))
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()
and this is weather.kv source code:
AddLocationForm:
<AddLocationForm>:
orientation: "vertical"
search_input: search_box
search_results: search_results_list
BoxLayout:
height: "40dp"
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 50
Button:
text: "Search"
size_hint_x: 25
on_press: root.search_location()
Button:
text: "Current Location"
size_hint_x: 25
ListView:
id: search_results_list
item_strings: []
The code's simple. You put a city name in textbox and hit search and it confirms it by showing the name it received.
OK, so I don't know if I'm late or not but having bought this book recently, I too found myself stuck exactly in this problem. Upon Googling this issue, I happened to stumble upon your question as well as O'Reilly link for this book. This is what the author had to say about this problem:
"I've confirmed the issue; openweathermap has changed their query process and the urls in the book are now all broken. This is going to utterly ruin the reader experience for all new readers; we'll need to do an update and should maybe talk about a second edition."
Luckily a good Samaritan found out the solution to this problem. But in order to do so you must first create a free Open Weather account. After creating the account, you'll get an API key. It'll be in your profile.
So, now this code:
search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"
becomes:
search_template = "http://api.openweathermap.org/data/2.5/find?q={}&type=like&APPID=" + "YOUR_API_KEY"
This worked for me. I know I'm 3 months late and probably by now you'd have gotten your answer, but I thought this will be useful for those who run into similar problems and their Google result will bring them to this place.