After practicing the Python box with Choregraphe, from Livedoor's Weather Web Service I would like NAO (Pepper) to talk about the forecast for the present / tomorrow / the day after tomorrow.
I borrowed the wisdom of my ancestors! Please see here for details. Python Box Concept (Pepper Tech Fes Technical Session)
The relationship between the boxes is like this.
Box name | Setting |
---|---|
Set Language | Language: Japanese |
Speech Reco. | Word list:Today's Tenki;Tomorrow's weather;The weather the day after tomorrow;The end |
Switch Case | Speech Reco.The character string specified in the Word list of""Enclose in (double quotation marks) and set in each line |
Say | Double-click the box and set Language (Japanese) and any text |
Python Script | Write weather get from weather web service here |
Say Text | Two boxes, one for the acquired weather forecast and one for the end |
Python Script
In the default Python Script, all inputs and outputs are van
, so
Right-click the box> select Edit Box and set the input onStart and output onStopped Type to string
.
I think it would be easier to write, but for the time being, the code that worked. .. ..
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
pass
def onUnload(self):
pass
def onInput_onStart(self, question):
import urllib2, json
url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=030010"
r = urllib2.urlopen(url)
root = json.loads(r.read())
if question == "Today's Tenki":
result = "The current tenki is" + root['forecasts'][0]['telop'].encode('utf8') + "is."
self.onStopped(result)
elif question == "Tomorrow's weather":
result = "Tomorrow's weather" + root['forecasts'][1]['telop'].encode('utf8') + "is."
self.onStopped(result)
elif question == "The weather the day after tomorrow":
result = "The weather the day after tomorrow" + root['forecasts'][2]['telop'].encode('utf8') + "is."
self.onStopped(result)
else:
self.onStopped(question)
def onInput_onStop(self):
self.onUnload()
The code for city =
sets Morioka. * For other area codes, click here.
The feeling that it is not smart is amazing.
Recommended Posts