#!/usr/local/bin/python import urllib, string, sys, socket, re print 'Content-Type: text/html\n' print "" print "" print "" print "" print "お天気だよーん" print "" print "" #最後の query=ローマ字表記の地名、詳細は www.wunderground.com url = "http://mobile.wunderground.com/cgi-bin/findweather/getForecast?query=kanazawa" #url = "http://mobile.wunderground.com/auto/mobile/CA/Huntington_Beach.html" #python 2.3 からは、urllib についても timeout がサポートしているが 2.2 ではNG #socket.setdefaulttimeout(5) try: f = urllib.urlopen(url) except IOError, (errno, strerror): print "IOError %s:%s" % (errno, strerror) print url print "上記 url を読み込めません。終了します" sys.exit() except: print "エラーが発生しました。終了します。" sys.exit() while 1: data = f.readline() #print data if not data: print "1: web のフォーマットが変更された可能性があります。" print "プログラムを停止します。" print "I can't read any more any longer." sys.exit() if string.find(data, "Forecast as") > 0: break #print data #print "--------" #searchCount = 0 #while 1: # data = f.readline() # # if not data: # print "1: web のフォーマットが変更された可能性があります。" # print "プログラムを停止します。" # print "I can't read any more any longer." # sys.exit() # # #if re.match("< *tr *>", data): # # searchCount = searchCount + 1 # # #if searchCount == 2: # # print data # if re.match("(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)", data): # print "BREAK" # break #print data #sys.exit() str = data while 1: data = f.readline() if not data: print "web のフォーマットが変更された可能性があります。" print "プログラムを停止します。" print "I can't read any more any longer." sys.exit() str = str + data if string.find(data, "/table") > 0: break table = re.split("|", str) #print table #sys.exit() i = 0 nextDay = [] reg = re.compile("(.| |\n)*)") while i < len(table) : #print "t0:",table[i] table_2 =re.sub("((.|\n)*)||\t", "", table[i]) #print "t2:", table_2 table_3 = re.sub("", " ", table_2) #print "t3:", table_3 table_4 = reg.sub("\g<3>\g<4>", table_3) #print "t4:", table_4 tempList = re.split("|\n", table_4) j = 0 tempList2 = [] while j < len(tempList): if not re.match("^(\n| )*$", tempList[j]) and string.find(tempList[j], "/") < 0 : temp = re.sub(" +", "", tempList[j]) #tempList2.append(tempList[j]) tempList2.append(temp) j = j + 1 #print i, " DEBUG tempList : ", tempList #print i, " DEBUG tempList2 : ", tempList2 if len(tempList2) >= 2: # print i, " DEBUG tempList2 : ", tempList2 # print len(tempList2) if re.match("(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)", tempList2[0]): nextDay.append(tempList2) # j = 1 # mesureData = [] # while j < len(tempList): # # # if not re.match("^ *$| */ *", tempList[j]): # #print tempList[j] # mesureData.append(tempList[j]) # # j = j + 1 # # #print "DEBUG mesureData : ", mesureData # weatherData [tempList[0]] = mesureData i = i + 1 #print nextDay #sys.exit() #今日の天気 while 1: data = f.readline() if not data: print "web のフォーマットが変更された可能性があります。" print "プログラムを停止します。" print "I can't read any more any longer." sys.exit() start = string.find(data, "Updated: ") if (start > 0): break str = data while 1: data = f.readline() if not data: print "web のフォーマットが変更された可能性があります。" print "プログラムを停止します。" print "I can't read any more any longer." sys.exit() str = str + data if string.find(data, "/table") > 0: break #print str table = re.split("|", str) #print table i = 0 weatherData = {} while i < len(table) : table_2 =re.sub("|", "", table[i]) table_3 = re.sub("", " ", table_2) tempList = re.split("|\n", table_3) #print i, " : DEBUG templist : ", tempList j = 1 mesureData = [] while j < len(tempList): if not re.match("^ *$| */ *", tempList[j]): #print tempList[j] mesureData.append(tempList[j]) j = j + 1 #print "DEBUG mesureData : ", mesureData weatherData [tempList[0]] = mesureData i = i + 1 #print weatherData print "-----" i = 1 while i < len(nextDay): print nextDay[i] i = i + 1 print "-----" for title in weatherData.keys(): print title, " : ", weatherData[title] sys.exit() print weatherData.keys() nowWindchill = "--" nowHumidity = "--" nowDewPoint = "--" nowWind = "--" nowWindSpeed = "--" nowPressure = "--" nowCondition = "--" nowVisibility = "--" #この間にはきっと # Cloud # Yesterday ??? #という情報もある場合がある。 nowSunrise = "--" nowSunset = "--" nowMoonrise = "--" nowMoonset = "--" if 'Temperature' in weatherData.keys(): nowTemperature = re.sub("^ *", "", weatherData["Temperature"][1]) #print nowTemperature sys.exit() #data = get_data_2(f, "WindChill", "°C") #nowWindchill = data[0][data[1]+3:data[2]] + " °C" #data = get_data_2(f, "Humidity", "%") #last = string.find(data[0], "%") #nowHumidity = data[0][data[1]+3:last] + " %" #data = get_data_2(f, "Dew Point", "°C") #nowDewPoint = data[0][data[1]+3:data[2]] + " °C" #data = get_data_2(f, "Wind", "at") #nowWind = data[0][data[1]+3:data[2]] #data = get_data_2(f,"mph", "km/h" ) #Speed = string.atof(data[0][data[1]+3:data[2]]) * 1000 / 3600 #nowWindSpeed = "%.1f m/s" % Speed #data = get_data_2(f, "Pressure", "hPa") #nowPressure = data[0][data[1]+3:data[2]] + " hPa" #data = get_data_2(f, "Conditions", "/td") #nowCondition = data[0][data[1]+3:data[2]] #data = get_data_2(f, "Visibility", "kilometers") #nowVisibility = data[0][data[1]+3:data[2]] + " km" #この間にはきっと # Cloud # Yesterday ??? #という情報もある場合がある。 #data = get_data_2(f, "Sunrise", "") #nowSunrise= data[0][data[1]+3:data[2]] #data = f.readline() #start = string.find(data, "") #last = string.find(data, "") #nowSunset= data[start+3:last] #data = f.readline() #start = string.find(data, "") #last = string.find(data, "") #nowMoonrise= data[start+3:last] #data = f.readline() #start = string.find(data, "") #last = string.find(data, "") #nowMoonset= data[start+3:last] #break #except: # print "エラーが発生しました。終了します。" # sys.exit() # #好きなように書いてください。 # print '' print "只今の金沢地方の天気は
" print "
" print dataTimeIs, "
" #print "Place is ", placeIs, "
" print "
" print "気温   : ", nowTemperature, "
" #print "体感温度 : ", nowWindchill, "
" print "湿度   : ", nowHumidity, "
" #print "DewPoint : ", nowDewPoint, "
" print "風向き  : ", nowWind, "
" print "風速   : ", nowWindSpeed, "
" print "気圧   : ", nowPressure, "
" print "天気   : ", nowCondition, "
" print "視界   : ", nowVisibility, "
" print "
" #print "--------------" print "日出   : ", nowSunrise, "
" print "日没   : ", nowSunset, "
" print "月出   : ", nowMoonrise, "
" print "月没   : ", nowMoonset, "
" print "
" #print "--------------" print "明日の天気予報", "
" #print nextay[0], "
" print "昼", "
" print nextWeather[0], "
" print "最高気温 : ", nextTemp[0], "
" print "
" #print "--------------" print "夜", "
" #print nextDay[1], "
" print nextWeather[1], "
" print "最低気温 : ", nextTemp[1], "
" print "
" #print "--------------" print "
" print '' print 'Copyright c 2004 The Weather Underground,Inc.' print '' print "" print ""