View Single Post
Old 04-17-2014, 11:37 AM   #38
Hellmann
Junior Member
Hellmann began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Apr 2014
Device: Kindle
Hey,

im using this code from you and changed it to my location and language.

Code:
# coding: utf-8
#!/usr/bin/python2

# Kindle Weather Display
# Matthew Petroff (http://www.mpetroff.net/)
# September 2012
# Reventlov Giskard (http://volcanis.me/)

import datetime
import codecs
import urllib2
import json

#######################
#unit = "fahrenheit"
unit = "celsius"
#unit_ = u'°F'
unit_ = u'°C'
# Api key from wunderground, get one here: http://www.wunderground.com/weather/api/
api_key = "68486b5679b7b81d"
# Search for the abbreviation here: http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=
#city="CA/San_Francisco/"
city = "Hamburg"
# LOCALISATION
#days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
days_of_week = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']
#tomorrow = 'Tomorrow'
tomorrow = 'Morgen'
#today = 'Today'
today = "Heute"
#hhigh = "High"
hhigh = "Maximum"
#llow = "Low"
llow = "Minimum"
#######################

highs = [None]*4
lows = [None]*4
icons = [None]*4

weather_json = urllib2.urlopen('http://api.wunderground.com/api/'+api_key+'/forecast/lang:DL/q/'+city+'.json')
json_string = weather_json.read()
parsed_json = json.loads(json_string)

highs[0] = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high'][unit]
lows[0] = parsed_json['forecast']['simpleforecast']['forecastday'][0]['low'][unit]
icons[0] = parsed_json['forecast']['simpleforecast']['forecastday'][0]['icon']

highs[1] = parsed_json['forecast']['simpleforecast']['forecastday'][1]['high'][unit]
lows[1] = parsed_json['forecast']['simpleforecast']['forecastday'][1]['low'][unit]
icons[1] = parsed_json['forecast']['simpleforecast']['forecastday'][1]['icon']

highs[2] = parsed_json['forecast']['simpleforecast']['forecastday'][2]['high'][unit]
lows[2] = parsed_json['forecast']['simpleforecast']['forecastday'][2]['low'][unit]
icons[2] = parsed_json['forecast']['simpleforecast']['forecastday'][2]['icon']

highs[3] = parsed_json['forecast']['simpleforecast']['forecastday'][3]['high'][unit]
lows[3] = parsed_json['forecast']['simpleforecast']['forecastday'][3]['low'][unit]
icons[3] = parsed_json['forecast']['simpleforecast']['forecastday'][3]['icon']

weather_json.close()

# Parse dates
day_one = datetime.datetime.today()

#
# Preprocess SVG
#

# Open SVG to process
output = codecs.open('weather-script-preprocess.svg', 'r', encoding='utf-8').read()

# Insert icons and temperatures
output = output.replace('ICON_ONE',icons[0]).replace('ICON_TWO',icons[1]).replace('ICON_THREE',icons[2]).replace('ICON_FOUR',icons[3])
output = output.replace('HIGH_ONE',str(highs[0])).replace('HIGH_TWO',str(highs[1])).replace('HIGH_THREE',str(highs[2])).replace('HIGH_FOUR',str(highs[3]))
output = output.replace('LOW_ONE',str(lows[0])).replace('LOW_TWO',str(lows[1])).replace('LOW_THREE',str(lows[2])).replace('LOW_FOUR',str(lows[3]))

# Localisation/Insert days of week
one_day = datetime.timedelta(days=1)
output = output.replace('DAY_THREE',days_of_week[(day_one + 2*one_day).weekday()]).replace('DAY_FOUR',days_of_week[(day_one + 3*one_day).weekday()])

# Insert the temperatures unit
output = output.replace('UNIT',unit_)

# Localisation
output = output.replace('TOMORROW',tomorrow)
output = output.replace('TODAY',today)
output = output.replace('HIGH',hhigh)
output = output.replace('LOW',llow)

output = output.replace('DATE',day_one.strftime("%H:%M"))

# Write output
codecs.open('weather-script-output.svg', 'w', encoding='utf-8').write(output)
but im getting an error in line 45

Code:
Traceback (most recent call last):
  File "weather-script.py", line 45, in <module>
    highs[0] = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high'][unit]
KeyError: 'forecast'
i cant figure out whats going wrong. the request seems to work normal, because in my wunderground analytics tool i can see a request.

thanks a lot and greetings

hellmann
Hellmann is offline   Reply With Quote