Skip to content

Commit 7248abb

Browse files
committed
use openweathermap instead of livedoor
1 parent b18fe75 commit 7248abb

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def __init__(self):
2323
self.day = self.now_time.strftime('%a')
2424
reload(sys)
2525
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]
2830

2931
def speak(self, client, speech_text, lang=None):
3032
client.wait_for_server(timeout=rospy.Duration(1.0))
@@ -60,10 +62,12 @@ def speak_jp(self):
6062
speech_text += '掃除の時間です。'
6163

6264
# 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
6771
self.speak(self.client_jp, speech_text, lang='jp')
6872

6973
def speak_en(self):
@@ -73,6 +77,16 @@ def speak_en(self):
7377
speech_text += " Let's go home."
7478
if self.now_hour == 12:
7579
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
7690
self.speak(self.client_en, speech_text)
7791

7892
def _get_text(self, hour):
@@ -85,9 +99,29 @@ def _get_text(self, hour):
8599
text = str(hour % 12) + ' PM'
86100
else:
87101
text = str(hour % 12) + ' AM'
88-
text = "It's " + text + ""
102+
text = "It's " + text + "."
89103
return text
90104

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+
91125

92126
if __name__ == '__main__':
93127
rospy.init_node('time_signal')

0 commit comments

Comments
 (0)