diff --git a/jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py b/jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py index 0ad28da1512..1f3f2851886 100755 --- a/jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py +++ b/jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py @@ -23,8 +23,10 @@ def __init__(self): self.day = self.now_time.strftime('%a') reload(sys) sys.setdefaultencoding('utf-8') - # 130010 is tokyo. See http://weather.livedoor.com/forecast/rss/primary_area.xml ## NOQA - self.citycode = '130010' + api_key_file = rospy.get_param( + '~api_key_file', '/var/lib/robot/openweathermap_api_key.txt') + with open(api_key_file, 'r') as f: + self.appid = f.read().split('\n')[0] def speak(self, client, speech_text, lang=None): client.wait_for_server(timeout=rospy.Duration(1.0)) @@ -60,10 +62,12 @@ def speak_jp(self): speech_text += '掃除の時間です。' # weather forecast - if self.now_hour == 0 or self.now_hour == 19: - url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city={}'.format(self.citycode) # NOQA - resp = json.loads(urllib2.urlopen(url)) - speech_text += '今日の天気は' + resp['forecasts'][0]['telop'] + 'です。' + if self.now_hour in [0, 12, 19]: + try: + forecast_text = self._get_weather_forecast(lang='ja') + speech_text += forecast_text + except Exception: + pass self.speak(self.client_jp, speech_text, lang='jp') def speak_en(self): @@ -73,6 +77,16 @@ def speak_en(self): speech_text += " Let's go home." if self.now_hour == 12: speech_text += " Let's go to lunch." + if self.now_hour == 19: + speech_text += "Let's go to dinner" + + # weather forecast + if self.now_hour in [0, 12, 19]: + try: + forecast_text = self._get_weather_forecast(lang='en') + speech_text += forecast_text + except Exception: + pass self.speak(self.client_en, speech_text) def _get_text(self, hour): @@ -85,9 +99,29 @@ def _get_text(self, hour): text = str(hour % 12) + ' PM' else: text = str(hour % 12) + ' AM' - text = "It's " + text + "" + text = "It's " + text + "." return text + def _get_weather_forecast(lang='en'): + url = 'http://api.openweathermap.org/data/2.5/weather?q=tokyo&appid={}&lang={}'.format(self.appid, lang) # NOQA + resp = json.loads(urllib2.urlopen(url)) + weather = resp['weather']['main'] + temp = float(resp['main']['temp']) - 273.15 + humidity = resp['main']['humidity'] + wind_speed = resp['wind']['speed'] + forecast_text = "" + if lang == 'ja': + forecast_text = "現在、天気は" + weather + "、" + forecast_text += "気温は" + temp + "度、" + forecast_text += "湿度は" + humidity + "%です。" + forecast_text += "風速は" + wind_speed + "メートル秒です。" + else: + forecast_text = "The weather is " + weather + " now." + forecast_text += "The temperature is" + temp + "celsius," + forecast_text += " and the humidity is " + humidity + "%." + forecast_text += "The wind speed is " + wind_speed + " meter per second." # NOQA + return forecast_text + if __name__ == '__main__': rospy.init_node('time_signal')