Version 7 (modified by 4 years ago) (diff) | ,
---|
PV277 Programming Applications for Social Robots (autumn 2019)
How to get info from web?
Use API
If API exists, you can usually use JSON data after send the right parameters. Example to get weather data.
import requests url = "http://api.openweathermap.org/data/2.5/weather" params = {'q': 'Brno', 'units': 'metric', 'lang': 'cz'} r = requests.get(url=url, params=params) data = r.json() print(data['main']['temp'])
(Full example attachment:forecast.py)
Parse webpage data
If no API is provided, you can download and parse the webpage.
from bs4 import BeautifulSoup import requests page = requests.get(url).text soup = BeautifulSoup(page, features="lxml") results = soup.findAll('h2', attrs={'class':'article-title'})
(Full example attachment:news.py)
How to integrate info with robot?
Dialog variables
In dialog file, set value of variable, use it in Python script in behavior.
u:(["Můj [oblíbený nejoblíbenější] tým je" "Fandím {týmu}"] _*) $team=$1
First word after sentence is available in $1
and passed to the script as team
variable.
def onInput_onStart(self, team): self.team_usr = team
Use TTS in script
veta = self.team_name + " má " + str(act_team.points) + " bodů" self.tts.say(veta)
Run script as service and parse service output
Specify service script in manifest.xml:
<services> <service autorun="true" execStart="/usr/bin/python2 scripts/rozvrh.py" name="rozvrh" /> </services>
In dialog, detect variables, pass them to service, and parse call result to say answer:
u:(rozvrh) Pro jakou místnost bys chtěl znát rozvrh? u1:({pro} {místnost učebnu} _~letter _~number) Podívám se na rozvrh pro $1 $2 ^call(Rozvrh.get_current_lesson($1, $2)) \pau=500\ c1:(In _* teaches _* course _*) V místnosti $1 je právě $3 s vyučujícím, který se jmenuje $2. c1:(In _* currently _*) V místnosti $1 je právě $2
u1
detects letter and number (concepts defined earlier in dialog) and calls get_current_lesson(). This function return strings, that are parsed in dialog and answer is translated to each locale.
def get_current_lesson(self, letter, number): room = letter+str(number) lesson = self.rozvrh.find_current_lesson(room) if lesson: if lesson.teacher: teacher = lesson.teacher.split(' ')[-1] return "In %s teaches %s course %s" % (lesson.room, teacher, lesson.name) else: return "In %s currently %s" % (lesson.room, lesson.name)
Attachments (7)
- dialoginputs.png (13.1 KB) - added by 4 years ago.
- pepper_connect.png (19.1 KB) - added by 4 years ago.
- pepper_virtual_port.png (14.4 KB) - added by 4 years ago.
- statapp.png (32.0 KB) - added by 4 years ago.
- virustat.zip (5.9 KB) - added by 4 years ago.
- news.py (458 bytes) - added by 11 months ago.
- forecast.py (1.3 KB) - added by 11 months ago.
Download all attachments as: .zip