#!/usr/local/bin/python
# weather undergroun から情報を取得し、その情報を表示する。
# 2004/3/7 v 0.3.1
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()
rawData = f.read()
if not rawData:
print "1: web のフォーマットが変更された可能性があります。"
print "プログラムを停止します。"
print "I can't read any more any longer."
sys.exit()
start = string.find(rawData, "Forecast as")
last = string.find(rawData,"/table",start)
nextPoint = last
str = rawData[start:last+6]
table = re.split("|
", str)
# 翌日の天気予報のデーターはリストとする。
# リストはシーケンシャル番号で操作できるため。翌日が何曜日か考えるのは面倒なので単純に
# 表示された順番で考える。
i = 0
nextDay = []
while i < len(table) :
#print "--------------------"
#
0:
start = string.find(table[i], "
", start)
img_src = table[i][start:last+1]
#print "img src : ", img_src
# 曜日の抽出
last2 = string.find(table[i], "
", last)
r = re.search("(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)(.| )*.( *\s*).(\s|.)*\s*", table[i][last2+1:len(table[i])])
if r :
tmp = table[i][last2+1:len(table[i])] [r.end(1):r.end(2)]
# 余計な文字を削除
# 改行、ピリオッドを区切りとしてリストに
ltemp =re.split("\n|\.", re.sub("\s*/\s*|\t*", "", tmp))
# 情報のあるシーケンス番号のみ新しいリストにコピー
j = 0
condition = []
while j < len(ltemp):
if not re.match("^ *$", ltemp[j]) :
#情報の前後ろの空白、: を削除してリストにコピー
condition.append(re.sub("^ +| +$|:", "", ltemp[j]))
j = j + 1
#condition = re.sub("\s*/\s*", "", tmp)
#print "コンディション : ", condition
#else:
#print "マッチする文字列がありません!!"
if re.match("(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)", dayName):
ltemp = [dayName, img_src, condition]
#print "ltemp : ", ltemp
nextDay.append(ltemp)
i = i + 1
#print nextDay
#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 = string.find(rawData, "Updated", nextPoint)
last = string.find(rawData, "|", str)
#print "2つめの table : ", table
#sys.exit()
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(re.sub("^ +| +$|
*|*tr>*", "", tempList[j]))
j = j + 1
#print "DEBUG mesureData : ", mesureData
if re.match("(?!Updated)[a-zA-Z]", tempList[0]):
weatherData [tempList[0]] = mesureData
elif re.match("Updated", tempList[0]):
weatherData ["Updated"] = [tempList[0][10:len(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()
#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 = string.find(weatherData["Humidity"][0], "%")
nowHumidity = weatherData["Humidity"][0][0:last] + " %"
if 'Dew Point' in weatherData.keys():
nowDewPoint = weatherData["Dew Point"][1]
if 'Wind' in weatherData.keys():
last = string.find(weatherData["Wind"][0], " ")
nowWind = weatherData["Wind"][0][0:last]
last = string.find(weatherData["Wind"][2], " ")
#print weatherData["Wind"][2][0:last]
#sys.exit()
Speed = string.atof(weatherData["Wind"][2][0:last]) * 1000/3600
nowWindSpeed = "%.1f m/s" % Speed
if 'Pressure' in weatherData.keys():
last = string.find(weatherData["Pressure"][1], " ")
nowPressure = weatherData["Pressure"][1][0:last] + " hPa"
if 'Conditions' in weatherData.keys():
nowConditions = weatherData["Conditions"][0]
if "Visibility" in weatherData.keys():
last = string.find(weatherData["Visibility"][1], " ")
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():
YesterdaysMin = 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 = string.find(weatherData["Updated"][0], "
")
dataTimeIs = weatherData["Updated"][0][0:last]
#
#好きなように書いてください。
#
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 "天気 : ", nowConditions, "
"
print "視界 : ", nowVisibility, "
"
print "
"
#print "--------------"
print "日出 : ", nowSunrise, "
"
print "日没 : ", nowSunset, "
"
print "月出 : ", nowMoonrise, "
"
print "月没 : ", nowMoonset, "
"
print "
"
#print "--------------"
# 下記フォーマットは日本の場合である
# アメリカは異なる。が、情報は nextDay[][2] のリストになっている。この中の情報の順番、情報数
# が異なるようである。
print "明日の天気予報", "
"
print nextDay[0][0], " 昼
"
print nextDay[0][2][0], "
"
print "最高気温 : ", nextDay[0][2][3], "
"
print "
"
#print "--------------"
print nextDay[1][0], " 夜
"
print nextDay[1][2][0], "
"
print "最低気温 : ", nextDay[1][2][3], "
"
print "
"
#print "--------------"
print ""
print ''
print '-- ver 0.3.1 --
'
print 'Copyright c 2004 The Weather Underground,Inc.'
print ''
print ""
print ""