def get_weather(location, config, units, weather_codes): """Gets the current weather for the specified location.""" # Get the current weather and organise it. result = pywapi.get_weather_from_yahoo(location, config["units"]) if units["airp"] == "mbar": result["atmosphere"]["pressure"] = str(float(result["atmosphere"]["pressure"]) * 33.86389) data = [] # Note: the conversion from int back to str for the temperature is necessary due # to encoding issues. data1 = [ ["Condition", weather_codes[result["condition"]["code"]]], ["Temperature", "%d %s" % (int(result["condition"]["temp"]), units["temp"])], ["Wind speed", "%s %s" % (result["wind"]["speed"], units["wind"])], ["Wind direction", degrees.degree_to_direction(int(result["wind"]["direction"]))], ["Wind chill", "%d %s" % (int(result["wind"]["chill"]), units["temp"])], ["Humidity", "%s%%" % result["atmosphere"]["humidity"]], ["Air pressure", "%s %s" % (result["atmosphere"]["pressure"], units["airp"])], ["Air pressure change", ["Steady", "Rising", "Falling"][int(result["atmosphere"]["rising"])]], ["Visibility", "%s %s" % (result["atmosphere"]["visibility"], result["units"]["distance"])], ["Sunrise", result["astronomy"]["sunrise"]], ["Sunset", result["astronomy"]["sunset"]] ] data2 = [ ["City", result["location"]["city"]], ["Region", result["location"]["region"]], ["Country", result["location"]["country"]], ["Latitude", result["geo"]["lat"]], ["Longitude", result["geo"]["long"]] ] data3 = [ ] for j in range(0, len(result["forecasts"])): i = result["forecasts"][j] data3.append(["Date", i["date"]]) data3.append(["Day", days[i["day"]]]) data3.append(["Condition", weather_codes[i["code"]]]) data3.append(["Low", "%d %s" % (int(i["low"]), units["temp"])]) data3.append(["High", "%d %s" % (int(i["high"]), units["temp"])]) if j != len(result["forecasts"]) - 1: data3.append(["", ""]) data.append(data1) data.append(data2) data.append(data3) # Get the prefill data. prefill_data = [ float(result["condition"]["temp"]), float(result["wind"]["chill"]), float(result["wind"]["speed"]), degrees.degree_to_direction(int(result["wind"]["direction"])), float(result["atmosphere"]["humidity"]), float(result["atmosphere"]["pressure"]), ["Steady", "Rising", "Falling"][int(result["atmosphere"]["rising"])], float(result["atmosphere"]["visibility"]) ] return result["location"]["city"], data, prefill_data
def prefill(self, user_location, units, profile): """Pre-fill the fields.""" # Get the data. data = pywapi.get_weather_from_yahoo(user_location, units = ("metric" if units["prec"] == "cm" else "imperial")) if "error" in data: show_error_dialog(self, "Add New Data- %s" % profile, "Error:\n\n%s" % data["error"]) return False # Set the temperature field. self.temp_sbtn.set_value(float(data["condition"]["temp"])) # Set the wind chill field. self.chil_sbtn.set_value(float(data["wind"]["chill"])) # Set the wind fields. try: self.wind_com.set_active(["None", "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"].index(degrees.degree_to_direction(float(data["wind"]["direction"])))) except: self.wind_com.set_active(0) try: self.wind_sbtn.set_value(float(data["wind"]["speed"])) except: self.wind_sbtn.set_value(0.0) # Set the humidity field. self.humi_sbtn.set_value(float(data["atmosphere"]["humidity"])) # Set the air pressure fields. self.airp_sbtn.set_value(float(data["atmosphere"]["pressure"])) if units["airp"] == "mbar": new_pressure = float(data["atmosphere"]["pressure"]) * 33.86389 self.airp_sbtn.set_value(new_pressure) self.airp_com.set_active(int(data["atmosphere"]["rising"])) # Set the visibility field. if data["atmosphere"]["visibility"].lstrip().rstrip() == "": self.visi_sbtn.set_value(0.0) else: self.visi_sbtn.set_value(float(data["atmosphere"]["visibility"])) # Return the location. return "%s, %s" % (data["location"]["city"], data["location"]["country"])