#!/usr/local/bin/python # 2004/3/8 v 0.3.4 import urllib, string, sys, re, calendar #import urllib, string, sys, 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() rawData = f.read() if not rawData: print "1: web のフォーマットが変更された可能性があります。" print "プログラムを停止します。" print "I can't read any more any longer." sys.exit() start = rawData.find("Forecast as") last = rawData.find("/table",start) nextPoint = last str = rawData[start:last+6] # 余計な文字を削除 # 改行、ピリオッドを区切りとしてリストに tmpTable1 = re.sub(r"\s*(", tmpTable) #for tableData in table: # print "-----" # print tableData # #sys.exit():$ # 翌日の天気予報のデーターはリストとする。 # リストはシーケンシャル番号で操作できるため。翌日が何曜日か考えるのは面倒なので単純に # 表示された順番で考える。 nextDay = [] matchString_Day = re.compile(r"(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)(.)*(\s*\s*).(\s|.)*.(\s*)") matchString_onlyDay = re.compile("Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat") dayJapanese = {"Today":"今日","Tonight":"今晩", "Sunday":"日曜日", "Monday":"月曜日", "Tuesday":"火曜日", "Wednesday":"水曜日", "Thursday":"木曜日", "Friday":"金曜日", "Saturday":"土曜日","Sunday Night":"日曜日 夜", "Monday Night":"月曜日 夜", "Tuesday Night":"火曜日 夜", "Wednesday Night":"水曜日 夜", "Thursday Night":"木曜日 夜", "Friday Night":"金曜日 夜", "Saturday Night":"土曜日 夜"} for tableData in table: #print "--------------------" # 0: start = tableData.find("", start) img_src = tableData[start:last+1] #print "img src : ", img_src # 曜日の抽出 last2 = tableData.find("
", last) r = matchString_Day.search(tableData[last+1:last2]) if r : dayName = tableData[last+1:last2] [r.start(1):r.start(3)] #print "day : ", dayName else: #print "マッチする曜日文字列がありません!!" dayName = "" # 天気の情報の抽出 r = matchString_Info.search(tableData[last2+1:]) if r : tmp = tableData[last2+1:len(tableData)] [r.end(1):r.start(3)] ltemp =re.split(r"\n", tmp) #print "天気情報 : ", ltemp #else: #print "マッチする天気情報文字列がありません!!" if matchString_onlyDay.match(dayName): #print "こここ" #print "日本語の曜日は? ", dayJapanese[dayName] ltemp = [dayJapanese[dayName], img_src, ltemp] nextDay.append(ltemp) #for dayNameInfo in nextDay: # print "--------" # print dayNameInfo #sys.exit() #i = 0 #while i < len(nextDay): # # print nextDay[i][0], " :", # # j = 1 # while j < len(nextDay[i]): # print nextDay[i][j], " ", # j = j + 1 # print # i = i + 1 #sys.exit() # 今日の天気 # ディクショナリーを使う。キーで操作できるから。 start = rawData.find("Updated", nextPoint) last = rawData.find("(.|\s)", r"\1",str) table2 = re.sub(r"(.|\s)", r"\1", table) table3 =re.sub(r"(?!/tr)(/|:|-|)", "", table2) table4 = re.sub(r">\s*<", "><", table3) table5 = re.sub(r"\n\s*", r"\n", table4) table = re.split(r"\s*\s*", table5) #for tableData in table: # print "-----" # print tableData #sys.exit() #print "2つめの table : ", table #sys.exit() weatherData = {} for tableData in table: # 天気情報をリストに tempList = re.split("|\n", tableData) #print "temp List : ", tempList if re.match("(?!Updated)[a-zA-Z]", tempList[0]): #weatherData [tempList[0]] = mesureData weatherData [tempList[0]] = tempList[1:] elif re.match("Updated", tempList[0]): #weatherData ["Updated"] = [tempList[0], mesureData] weatherData ["Updated"] = tempList #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() #sys.exit() nowWindchill = "--" nowHumidity = "--" nowDewPoint = "--" nowWind = "--" nowWindSpeed = "--" nowPressure = "--" nowConditions = "--" nowVisibility = "--" nowClouds = "--" YesterdaysMax = "--" YesterdaysMin = "--" nowSunrise = "--" nowSunset = "--" nowMoonrise = "--" nowMoonset = "--" if 'Temperature' in weatherData.keys(): nowTemperature = weatherData["Temperature"][1] if 'Windchill' in weatherData.keys(): nowWindchill = weatherData["Windchill"][1] if 'Humidity' in weatherData.keys(): last = weatherData["Humidity"][0].find("%") nowHumidity = weatherData["Humidity"][0][0:last] + " %" if 'Dew Point' in weatherData.keys(): nowDewPoint = weatherData["Dew Point"][1] if 'Wind' in weatherData.keys(): last = weatherData["Wind"][0].find(" ") nowWind = weatherData["Wind"][0][0:last] last = weatherData["Wind"][2].find(" ") Speed = string.atof(weatherData["Wind"][2][0:last]) * 1000/3600 nowWindSpeed = "%.1f m/s" % Speed if 'Pressure' in weatherData.keys(): last = weatherData["Pressure"][1].find(" ") nowPressure = weatherData["Pressure"][1][0:last] + " hPa" if 'Conditions' in weatherData.keys(): nowConditions = weatherData["Conditions"][0] if "Visibility" in weatherData.keys(): last = weatherData["Visibility"][1].find(" ") nowVisibility = weatherData["Visibility"][1][0:last] + " km" if "Yesterday's Minimum" in weatherData.keys(): YesterdaysMin = weatherData["Yesterday's Minimum"][1] if "Yesterday's Maximum" in weatherData.keys(): YesterdaysMax = weatherData["Yesterday's Maximum"][1] if 'Clouds' in weatherData in weatherData.keys(): nowClouds = weatherData["Clouds"][0] if "Sunset" in weatherData.keys(): nowSunset = weatherData["Sunset"][0] if "Sunrise" in weatherData.keys(): nowSunrise = weatherData["Sunrise"][0] if "Moon Set" in weatherData.keys(): nowMoonset = weatherData["Moon Set"][0] if "Moon Rise" in weatherData.keys(): nowMoonrise = weatherData["Moon Rise"][0] last = weatherData["Updated"][0].find(r"' print "只今の金沢地方の天気は
" print "
" #print dataTimeIs, " (", todayWeek, ")
" print "%s/%s/%s (%s) %s:%s %s
" % (dayList[-1], monthDecimal[dayList[-3]], dayList[-2][:-1], todayWeek, dayList[-7][:-2], dayList[-7][-2:], dayList[-6]) #print "Place is ", placeIs, "
" print "
" print "気温   : ", nowTemperature, "
" print "体感温度 : ", nowWindchill, "
" print "湿度   : ", nowHumidity, "
" #print "DewPoint : ", nowDewPoint, "
" print "風向き  : ", nowWind, "
" print "風速   : ", nowWindSpeed, "
" print "気圧   : ", nowPressure, "
" print "天気   : ", nowConditions, "
" print "視界   : ", nowVisibility, "
" print "
" #print "--------------" print "日出   : %s:%s
" % (nowSunrise[:2], nowSunrise[2:-5]) print "日没   : %s:%s
" % (nowSunset[:2], nowSunset[2:-5]) print "月出   : %s:%s
" % (nowMoonrise[:2], nowMoonrise[2:-5]) print "月没   : %s:%s
" % (nowMoonset[:2], nowMoonset[2:-5]) print "
" #print "--------------" # 下記フォーマットは日本の場合である # アメリカは異なる。が、情報は nextDay[][2] のリストになっている。 # この中の情報の順番、情報数が異なる。 #print nextDay[0][0] #print nextDay[0][0].find(todayWeek) if nextDay[0][0].find(todayWeek) >= 0: forcastDay_1 = nextDay[2][0] forcastInfo_1 = nextDay[2][2][0] forcastTemp_1 = nextDay[2][2][3] forcastDay_2 = nextDay[3][0] forcastInfo_2 = nextDay[3][2][0] forcastTemp_2 = nextDay[3][2][3] else: forcastDay_1 = nextDay[0][0] forcastInfo_1 = nextDay[0][2][0] forcastTemp_1 = nextDay[0][2][3] forcastDay_2 = nextDay[1][0] forcastInfo_2 = nextDay[1][2][0] forcastTemp_2 = nextDay[1][2][3] print "明日の天気予報", "
" print forcastDay_1, "
" print forcastInfo_1, "
" print "最高気温 : ", forcastTemp_1, "
" print "
" #print "--------------" print forcastDay_2, "
" print forcastInfo_2, "
" print "最低気温 : ", forcastTemp_2, "
" print "
" #print "--------------" print "" print '' print '--ver 0.3.4--
' print 'Copyright c 2004 The Weather Underground,Inc.' print '
' print "" print ""