#!/usr/local/bin/python
# 2004/3/12 v 0.3.5.2
import urllib, string, sys, socket, re, calendar
def wd(x):
if x[0] in weatherData.keys():
return weatherData[x[0]][x[1]]
else:
return "--"
#わすれるな、実行できなくなるぞ!!
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]
#print table
table = re.split("",
reduce(lambda x, y:re.sub(y[0], y[1], x),
[str, (r">\s*<", "><"), (r"| | ", "")]))[:-1]
#print table
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")]))
#print "------"
#print table
#今日は何曜日?
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 littleTable
# 最初のデーターをキーとして、残りのデーターは、リストになったデーター
weatherData[littleTable[0]] =littleTable[1:]
#print "weather information "
#for info in weatherData:
# print info, ":", weatherData[info]
(nowWindchill, nowHumidity, nowDewPoint, nowWind, nowWindSpeed,
nowPressure, nowConditions, nowVisibility, nowClouds,
YesterdaysMax, YesterdaysMin, nowSunrise, nowSunset, nowMoonrise, nowMoonseti
) = ["--"] * 15
#print nowWindchill
[nowTemperature, nowWindchill, nowDewPoint, nowConditions,
YesterdaysMin, YesterdaysMin, 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", YesterdaysMin
#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"
#
#好きなように書いてください。
#
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 "天気 : ", nowConditions, "
"
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]
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 "get_weather V0.3.5.2
"
print 'Copyright c 2004 The Weather Underground,Inc.'
print ''
print ""
print ""