#!/usr/local/bin/python # 2004/6/25 v 0.3.5.4 import urllib, string, sys, socket, re, calendar versionIs = "get_weather V0.3.5.4
" def wd(x): #print "hoge" if x[0] in weatherData.keys(): return weatherData[x[0]][x[1]] else: return "--" def EtoJ_weather(conditions): if (conditions[-1] == "."): conditions = conditions[:-1] weather = { 'Light Drizzle':'軽い霧雨', 'Light Rain':'小雨', 'Light Snow':'小雪', 'Light Snow Grains':'小雪粒', 'Light Ice Crystals':'軽い氷晶', 'Light Ice Pellets':'軽い凍雨', 'Light Hail':'軽いあられ', 'Light Mist':'軽いもや', 'Light Fog':'軽い霧', 'Light Smoke':'軽い煙', 'Light Volcanic Ash':'軽い火山灰', 'Light Widespread Dust':'軽い広範囲ほこり', 'Light Sand':'軽いサンド', 'Light Haze':'軽いもや', 'Light Spray':'軽いスプレー', 'Light Dust Whirls':'軽い塵旋風', 'Light Sandstorm':'軽い砂嵐', 'Light Low Drifting Snow':'軽い低い浮遊物雪', 'Light Low Drifting Widespread Dust':'軽い低い浮遊物の広範囲ほこり', 'Light Low Drifting Sand':'軽い低い漂流する砂', 'Light Blowing Snow':'雪を吹く光', 'Light Blowing Widespread Dust':'広範囲ほこりを吹く光', 'Light Blowing Sand':'砂を吹く光', 'Light Rain Showers':'小雨にわか雨', 'Light Snow Showers':'小雪にわか雨', 'Light Ice Pellet Showers':'軽い凍雨にわか雨', 'Light Hail Showers':'軽いあられにわか雨', 'Light Small Hail Showers':'軽い小さなあられにわか雨', 'Light Thunderstorm':'軽い雷雨', 'Light Thunderstorms and Rain':'軽い雷雨および雨', 'Light Thunderstorms and Snow':'軽い雷雨および雪', 'Light Thunderstorms and Ice Pellets':'軽い雷雨および凍雨', 'Light Thunderstorms with Hail':'あられを備えた軽い雷雨', 'Light Thunderstorms with Small Hail':'小さなあられを備えた軽い雷雨', 'Light Freezing Drizzle':'光結氷霧雨', 'Light Freezing Rain':'光結氷雨', 'Light Freezing Fog':'光結氷霧', 'Drizzle':'霧雨', 'Rain':'雨', 'Snow':'雪', 'Snow Grains':'雪粒', 'Ice Crystals':'氷晶', 'Ice Pellets':'凍雨', 'Hail':'あられ', 'Small Hail':'小さなあられ', 'Unknown Precipitation':'未知降雨', 'Mist':'もや', 'Fog':'霧', 'Smoke':'煙', 'Volcanic Ash':'火山灰', 'Widespread Dust':'広範囲ほこり', 'Sand':'サンド', 'Haze':'もや', 'Spray':'スプレー', 'Dust Whirls':'塵旋風', 'Squalls':'スコール', 'Funnel Cloud':'漏斗雲', 'Sandstorm':'砂嵐', 'Shallow Fog':'浅い霧', 'Partial Fog':'部分的な霧', 'Patches of Fog':'霧のパッチ', 'Low Drifting Snow':'ロウ浮遊物雪', 'Low Drifting Widespread Dust':'ロウの浮遊物の広範囲ほこり', 'Low Drifting Sand':'砂を漂流させるロウ', 'Blowing Snow':'雪を吹くこと', 'Blowing Widespread Dust':'広範囲ほこりを吹くこと', 'Blowing Sand':'サンドを吹くこと', 'Rain Showers':'にわか雨', 'Snow Showers':'雪にわか雨', 'Ice Pellet Showers':'凍雨にわか雨', 'Hail Showers':'あられにわか雨', 'Small Hail Showers':'小さなあられにわか雨', 'Thunderstorm':'雷雨', 'Thunderstorms and Rain':'雷雨と雨', 'Thunderstorms and Snow':'雷雨と雪', 'Thunderstorms and Ice Pellets':'雷雨と凍雨', 'Thunderstorms with Hail':'あられを備えた雷雨', 'Thunderstorms with Small Hail':'小さなあられを備えた雷雨', 'Freezing Drizzle':'結氷霧雨', 'Freezing Rain':'結氷雨', 'Freezing Fog':'結氷霧', 'Heavy Drizzle':'重い霧雨', 'Heavy Rain':'大雨', 'Heavy Snow':'大雪', 'Heavy Snow Grains':'大雪粒', 'Heavy Ice Crystals':'重い氷晶', 'Heavy Ice Pellets':'重い凍雨', 'Heavy Hail':'重いあられ', 'Heavy Small Hail':'重い小さなあられ', 'Heavy Mist':'激しいもや', 'Heavy Fog':'重い霧', 'Heavy Smoke':'激しい煙', 'Heavy Volcanic Ash':'重い火山灰', 'Heavy Widespread Dust':'重い広範囲ほこり', 'Heavy Sand':'重砂', 'Heavy Haze':'重いもや', 'Heavy Spray':'激しいスプレー', 'Heavy Dust Whirls':'重い塵旋風', 'Heavy Sandstorm':'重い砂嵐', 'Heavy Fog Patches':'重い霧パッチ', 'Heavy Low Drifting Snow':'激しい低い浮遊物雪', 'Heavy Low Drifting Widespread Dust':'重い低い浮遊物の広範囲ほこり', 'Heavy Low Drifting Sand':'重い低い漂流する砂', 'Heavy Blowing Snow':'激しい吹雪', 'Heavy Blowing Widespread Dust':'重い吹く広範囲ほこり', 'Heavy Blowing Sand':'重い吹く砂', 'Heavy Rain Showers':'大雨にわか雨', 'Heavy Snow Showers':'大雪にわか雨', 'Heavy Ice Pellet Showers':'激しい凍雨にわか雨', 'Heavy Hail Showers':'激しいあられにわか雨', 'Heavy Small Hail Showers':'激しい小さなあられにわか雨', 'Heavy Thunderstorm':'激しい雷雨', 'Heavy Thunderstorms and Rain':'激しい雷雨および雨', 'Heavy Thunderstorms and Snow':'激しい雷雨および雪', 'Heavy Thunderstorms with Ice Pellets':'凍雨を備えた激しい雷雨', 'Heavy Thunderstorms with Hail':'あられを備えた激しい雷雨', 'Heavy Thunderstorms with Small Hail':'小さなあられを備えた激しい雷雨', 'Heavy Freezing Drizzle':'重い結氷霧雨', 'Heavy Freezing Rain':'激しい結氷雨', 'Heavy Freezing Fog':'重い結氷霧', 'Overcast':'曇', 'Clear':'快晴', 'Partly Cloudy':'部分的に曇です', 'Mostly Cloudy':'ほとんど曇です', 'Scattered Clouds':'散在した雲', 'Chance of Rain':'雨の降りそうな天気', } if (weather.has_key(conditions)): return weather[conditions] else: return conditions #わすれるな、実行できなくなるぞ!! print 'Content-Type: text/html\n' # HTML ヘッダー 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") start_table = rawData.find("\s*<", "><", str) #tmpTable2 = re.sub(r"|", "", tmpTable1) #table = re.split("", tmpTable2)[1:-1] #次の式は ↑ です。 # が区切りになります。で、その中の余計な、タグ等々を消去する。 table = re.split("", reduce(lambda x, y:re.sub(y[0], y[1], x), [str, (r">\s*<", "><"), (r"|", "")]))[:-1] weekNameIs = ["Today", "Tonight", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday Night", "Monday Night", "Tuesday Night", "Wednesday Night", "Thursday Night", "Friday Night", "Saturday Night"] 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 key in dayJapanese.keys(): # print key matchStringIs = re.compile(r"|
|<.?b>|\n+|\t+| +") nextDay = [] for tableData in table: littleTable = filter(lambda x: not re.match(r"^[/\s]*$", x), matchStringIs.split(tableData)) #print littleTable if littleTable[1] in weekNameIs: nextDay.append([dayJapanese[littleTable[1]]]+littleTable[:1]+ littleTable[2:]) #for dayNameInfo in nextDay: # print "--------
" # print dayNameInfo[0], ":", dayNameInfo, "
" # #sys.exit() # 今日の天気 ## ディクショナリーを使う。キーで操作できるから。 start = rawData.find("Updated", nextPoint) last = rawData.find("\s*<", "><", str) #tmpTable2 = re.sub(r"|||
|\s+/|Observed|at| in ", "", tmpTable1) #tmpTable3 = re.sub(r"
|", "\n", tmpTable2) #table = re.split("", tmpTable3) table = re.split(r"", reduce (lambda x, y:re.sub(y[0], y[1], x), [ str, (r">\s*<", "><"), (r"|||
|\s+/|Observed| at | in ", ""), (r"
|", "\n"), (r"
", ""), (r"(%|°)", r" \1")])) #for key in table: # print "------
" # print key, "
" #sys.exit() #今日は何曜日? monthDecimal ={'January':1,'Febuary':2, 'March':3, 'April':4, 'May':5, 'June':6, 'July':7, 'August':8, 'September':9, 'Octobar':10, 'Nombember':11, 'December':12} decimal2week=['月', '火', '水', '木', '金', '土', '日'] dayList = filter(lambda x: not re.match(r"^\s*$", x), re.split(r"\s+", table[0])) #print dayList[7],"/", monthDecimal[dayList[5]],"/", dayList[6][:-1] todayNumber = calendar.weekday(int(dayList[7]), int(monthDecimal[dayList[5]]), int(dayList[6][:-1])) todayWeek = decimal2week[todayNumber] NowDay = "%s/%s/%s (%s) %s %s
" % (dayList[7], monthDecimal[dayList[5]], dayList[6][:-1], todayWeek, dayList[1], dayList[2]) #sys.exit() #print "------" matchStringIs = re.compile(r"|
|<.?b>|\n+|\t+| +") weatherData = {} for tableData in table[1:]: littleTable = filter(lambda x: not re.match(r"^[/\s]*$", x), matchStringIs.split(tableData)) #print "debug littleTable[0] ", littleTable[0], "littletable[1: ", littleTable[1:] # 最初のデーターをキーとして、残りのデーターは、リストになったデーター #print " debug 2 : ", len(littleTable), "
" if len(littleTable) > 1: weatherData[littleTable[0]] = littleTable[1:] #version 3.5.3 else: weatherData[littleTable[0]] = "--" #print "
weather information
" #for info in weatherData: # print info, ":", weatherData[info], "
" # #sys.exit() (nowWindchill, nowHumidity, nowDewPoint, nowWind, nowWindSpeed, nowPressure, nowConditions, nowVisibility, nowClouds, YesterdaysMax, YesterdaysMin, nowSunrise, nowSunset, nowMoonrise, nowMoonseti ) = ["--"] * 15 #print nowWindchill [nowTemperature, nowWindchill, nowDewPoint, nowConditions, YesterdaysMin, YesterdaysMax, nowClouds, nowHumidity, nowPressure, nowSunset, nowSunrise, nowMoonset, nowMoonrise ] = [wd(x) for x in ( ("Temperature", 1), ("Windchill", 1), ("Dew Point", 1), ("Conditions", 0), ("Yesterday's Minimum", 1), ("Yesterday's Maximum", 1), ("clouds", 0), ("Humidity", 0), ("Pressure", 1), ("Sunset", 0), ("Sunrise", 0), ("Moon Set", 0), ("Moon Rise", 0))] #print "Temperature:", nowTemperature #print "Windchill:", nowWindchill #print "DewPoint:", nowDewPoint #print "Conditions:", nowConditions #print "Yesterday's Minimum:", YesterdaysMin #print "Yesterday's Maximum", YesterdaysMax #print "clouds", nowClouds #print "Sunset:", nowSunset #print "Sunrise:", nowSunrise #print "Moon Set", nowMoonset #print "Moon Rise", nowMoonrise #sys.exit() 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 "Visibility" in weatherData.keys(): if weatherData["Visibility"][0].find("miles") > 0: nowVisibility = weatherData["Visibility"][1][:-11] + " km" #天気を日本語化 JnowConditions = EtoJ_weather(nowConditions) # #好きなように書いてください。 # print '' print "只今の金沢地方の天気は
" print "
" print NowDay #print "Place is ", placeIs, "
" print "
" print "気温   : ", nowTemperature, "
" print "体感温度 : ", nowWindchill, "
" print "湿度   : ", nowHumidity, "
" #print "DewPoint : ", nowDewPoint, "
" print "風向き  : ", nowWind, "
" print "風速   : ", nowWindSpeed, "
" print "気圧   : ", nowPressure, "
" print "天気   : ", JnowConditions, "
" print "視界   : ", nowVisibility, "
" print "
" #print "--------------" print "日出   : ", nowSunrise, "
" print "日没   : ", nowSunset, "
" print "月出   : ", nowMoonrise, "
" print "月没   : ", nowMoonset, "
" print "
" #print "--------------" # 下記フォーマットは日本の場合である # アメリカは異なる。が、情報は nextDay[][2] のリストになっている。 # この中の情報の順番、情報数が異なる。 # 明日を探す if todayNumber == 6: nextNumber = 0 else: nextNumber = todayNumber+1 tommorowDay = decimal2week[nextNumber] + "曜日" #print tommorowDay todayN = 0 for keyDay in nextDay: if keyDay[0] == tommorowDay: #print "あたり" #print keyDay #print keyDay[0] todayN = nextDay.index(keyDay) break forcastDay_1 = nextDay[todayN][0] forcastInfo_1 = nextDay[todayN][2] forcastTemp_1 = nextDay[todayN][5] forcastDay_2 = nextDay[todayN+1][0] forcastInfo_2 = nextDay[todayN+1][2] forcastTemp_2 = nextDay[todayN+1][5] #天気を日本語に JforcastInfo_1 = EtoJ_weather(forcastInfo_1) JforcastInfo_2 = EtoJ_weather(forcastInfo_2) print "明日の天気予報", "
" print forcastDay_1, "
" print JforcastInfo_1, "
" print "最高気温 : ", forcastTemp_1, "
" print "
" #print "--------------" print forcastDay_2, "
" print JforcastInfo_2, "
" print "最低気温 : ", forcastTemp_2, "
" print "
" #print "--------------" print "
" print '' print versionIs print 'Copyright c 2004 The Weather Underground,Inc.' print '' print "" print ""