Skip to content

Commit

Permalink
use openweathermap instead of livedoor
Browse files Browse the repository at this point in the history
  • Loading branch information
knorth55 committed Aug 7, 2020
1 parent b18fe75 commit 7248abb
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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')
Expand Down

0 comments on commit 7248abb

Please sign in to comment.