#!/usr/local/bin/python
# 2004/3/8 v 0.3.2
import urllib, string, sys, socket, re
print 'Content-Type: text/html\n'
print ""
print ""
print "
"
print ""
print "/title>"
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]
table = re.split("|
", str)
# 翌日の天気予報のデーターはリストとする。
# リストはシーケンシャル番号で操作できるため。翌日が何曜日か考えるのは面倒なので単純に
# 表示された順番で考える。
nextDay = []
matchString_Day = re.compile("(Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat)(.)*.( *\s*).(\s|.)*\s*")
matchString_onlyDay = re.compile("Tod|Ton|Sun|Mon|Tue|Wed|Thu|Fri|Sat")
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:len(tableData)])
if r :
tmp = tableData[last2+1:len(tableData)] [r.end(1):r.end(2)]
# 余計な文字を削除
# 改行、ピリオッドを区切りとしてリストに
ltemp =re.split("\n|\.", re.sub("\s*/\s*|\t*", "", tmp))
# 情報のあるシーケンス番号のみ新しいリストにコピー
#j = 0
condition = []
for ltempData in ltemp:
if not re.match("^ *$", ltempData) :
#情報の前後ろの空白、: を削除してリストにコピー
condition.append(re.sub("^ +| +$|:", "", ltempData))
#condition = re.sub("\s*/\s*", "", tmp)
#print "コンディション : ", condition
#else:
#print "マッチする文字列がありません!!"
if matchString_onlyDay.match(dayName):
ltemp = [dayName, img_src, condition]
#print "ltemp : ", ltemp
nextDay.append(ltemp)
#print nextDay
#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("|", str)
#print "2つめの table : ", table
#sys.exit()
weatherData = {}
for tableData in table:
table_2 =re.sub("|", "", tableData)
table_3 = re.sub("", " ", table_2)
# 天気情報をリストに
tempList = re.split(" | |\n", table_3)
mesureData = []
for tempListData in tempList[1:]:
if not re.match("^ *$| */ *", tempListData):
#print tempList[j]
mesureData.append(re.sub("^ +| +$|
*|*tr>*", "", tempListData))
#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], mesureData]
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():
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 = weatherData["Updated"][0].find("
")
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 '--v0.3.2--
'
print 'Copyright c 2004 The Weather Underground,Inc.'
print ''
print ""
print ""