@@ -23,8 +23,10 @@ def __init__(self):
23
23
self .day = self .now_time .strftime ('%a' )
24
24
reload (sys )
25
25
sys .setdefaultencoding ('utf-8' )
26
- # 130010 is tokyo. See http://weather.livedoor.com/forecast/rss/primary_area.xml ## NOQA
27
- self .citycode = '130010'
26
+ api_key_file = rospy .get_param (
27
+ '~api_key_file' , '/var/lib/robot/openweathermap_api_key.txt' )
28
+ with open (api_key_file , 'r' ) as f :
29
+ self .appid = f .read ().split ('\n ' )[0 ]
28
30
29
31
def speak (self , client , speech_text , lang = None ):
30
32
client .wait_for_server (timeout = rospy .Duration (1.0 ))
@@ -60,10 +62,12 @@ def speak_jp(self):
60
62
speech_text += '掃除の時間です。'
61
63
62
64
# weather forecast
63
- if self .now_hour == 0 or self .now_hour == 19 :
64
- url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city={}' .format (self .citycode ) # NOQA
65
- resp = json .loads (urllib2 .urlopen (url ))
66
- speech_text += '今日の天気は' + resp ['forecasts' ][0 ]['telop' ] + 'です。'
65
+ if self .now_hour in [0 , 12 , 19 ]:
66
+ try :
67
+ forecast_text = self ._get_weather_forecast (lang = 'ja' )
68
+ speech_text += forecast_text
69
+ except Exception :
70
+ pass
67
71
self .speak (self .client_jp , speech_text , lang = 'jp' )
68
72
69
73
def speak_en (self ):
@@ -73,6 +77,16 @@ def speak_en(self):
73
77
speech_text += " Let's go home."
74
78
if self .now_hour == 12 :
75
79
speech_text += " Let's go to lunch."
80
+ if self .now_hour == 19 :
81
+ speech_text += "Let's go to dinner"
82
+
83
+ # weather forecast
84
+ if self .now_hour in [0 , 12 , 19 ]:
85
+ try :
86
+ forecast_text = self ._get_weather_forecast (lang = 'en' )
87
+ speech_text += forecast_text
88
+ except Exception :
89
+ pass
76
90
self .speak (self .client_en , speech_text )
77
91
78
92
def _get_text (self , hour ):
@@ -85,9 +99,29 @@ def _get_text(self, hour):
85
99
text = str (hour % 12 ) + ' PM'
86
100
else :
87
101
text = str (hour % 12 ) + ' AM'
88
- text = "It's " + text + ""
102
+ text = "It's " + text + ". "
89
103
return text
90
104
105
+ def _get_weather_forecast (lang = 'en' ):
106
+ url = 'http://api.openweathermap.org/data/2.5/weather?q=tokyo&appid={}&lang={}' .format (self .appid , lang ) # NOQA
107
+ resp = json .loads (urllib2 .urlopen (url ))
108
+ weather = resp ['weather' ]['main' ]
109
+ temp = float (resp ['main' ]['temp' ]) - 273.15
110
+ humidity = resp ['main' ]['humidity' ]
111
+ wind_speed = resp ['wind' ]['speed' ]
112
+ forecast_text = ""
113
+ if lang == 'ja' :
114
+ forecast_text = "現在、天気は" + weather + "、"
115
+ forecast_text += "気温は" + temp + "度、"
116
+ forecast_text += "湿度は" + humidity + "%です。"
117
+ forecast_text += "風速は" + wind_speed + "メートル秒です。"
118
+ else :
119
+ forecast_text = "The weather is " + weather + " now."
120
+ forecast_text += "The temperature is" + temp + "celsius,"
121
+ forecast_text += " and the humidity is " + humidity + "%."
122
+ forecast_text += "The wind speed is " + wind_speed + " meter per second." # NOQA
123
+ return forecast_text
124
+
91
125
92
126
if __name__ == '__main__' :
93
127
rospy .init_node ('time_signal' )
0 commit comments