https://github.com/google/aiyprojects-raspbian
Intermediate
Source: https://github.com/ktinkerer/aiyprojects-raspbian/blob/voicekit/src/headlines.py
sudo pip install feedparser
sudo nano /home/pi/voice-recognizer-raspi/src/main.py
import feedparser
def headlines():
url = "http://feeds.bbci.co.uk/news/rss.xml"
number_of_headlines = 3
feed = feedparser.parse(url)
headlines = ""
for i in range(0, number_of_headlines):
title = feed.entries[i].title
summary = (feed.entries[i].summary)
headlines = headlines + " " + title + " " + summary
aiy.audio.say(headlines)
url = "http://feeds.bbci.co.uk/news/rss.xml" - variable to store url to the headlines
number_of_headlines = 3 - number of headlines that will be readed
feed = feedparser.parse(url) - variable to store parsed feed from the url
headlines = "" - empty variable to store headlines
for i in range(0, number_of_headlines): - loop that repeats as many times as you set in number_of_headlines
title = feed.entries[i].title - variable to store title of the article
summary = (feed.entries[i].summary) - variable to store summary of the article
headlines = headlines + " " + title + " " + summary - add title and summary to the headlines variable
aiy.audio.say(headlines) - make Assistant say headlines
Create your trigger command
elif text == 'headlines':
assistant.stop_conversation()
headlines()
It checks if you said given phrase. If so, it will not send command to the Google Assistant API and will execute given function.
Source: https://github.com/ktinkerer/aiyprojects-raspbian/blob/voicekit/src/radio_mod.py
sudo pip install python-vlc
sudo nano /home/pi/voice-recognizer-raspi/src/main.py
import vlc
def radio_off():
try:
player.stop()
except NameError as e:
logging.info("Player isn't playing")
The function is trying to stop the player. In the case the radio is not playing, function will print an error message.
def get_station(station_name):
stations = {
'1': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
'one': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
'2': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_two.m3u8',
'3': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_three.m3u8',
'4': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_fourfm.m3u8',
'5': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_five_live.m3u8',
'5 sports': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_five_live_sports_extra.m3u8',
'6': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8',
'1xtra': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_1xtra.m3u8',
'4 extra': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_four_extra.m3u8',
'nottingham': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_nottingham.m3u8',
'planet rock': 'http://icy-e-bz-08-boh.sharp-stream.com/planetrock.mp3',
}
return stations[station_name]
For example 'planet rock': is a station name and 'http://icy-e-bz-08-boh.sharp-stream.com/planetrock.mp3', is a direct link to the broadcast. When you say radio planet rock the program will search for station with that name and will play its broadcast. You can also add your stations with direct links to them.
def radio(text):
logging.info("Radio command received: %s ", text)
station_name = (text.replace('radio', '', 1)).strip()
if station_name == "off":
logging.info("Switching radio off")
radio_off()
return
try:
station = get_station(station_name)
except KeyError as e:
logging.error("Error finding station %s", station_name)
# Set a default station here
station = 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8'
logging.info("Playing radio: %s ", station)
instance = vlc.Instance()
global player
player = instance.media_player_new()
media = instance.media_new(station)
player.set_media(media)
player.play()
logging.info("Radio command received: %s ", text) - print a received radio commandstation_name = (text.replace('radio', '', 1)).strip() - get the station name from the command if station_name == "off":
logging.info("Switching radio off")
radio_off()
return
If the command is radio off program will print a message and will execute the function that turns off the player
try:
station = get_station(station_name)
Try to find given station in get_station function
except KeyError as e:
logging.error("Error finding station %s", station_name)
# Set a default station here
station = 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8'
If the program has not found the station it will print an error message and will play a default one (you can set there any station you want)
logging.info("Playing radio: %s ", station) - print the station that will be played
instance = vlc.Instance() -create a new VLC instance
global player - create a global player variable
player = instance.media_player_new() - create a new MediaPlayer instance
media = instance.media_new(station) - create a new media instance with the station
player.play() - play a radio
Create your trigger command
elif 'radio' in text:
assistant.stop_conversation()
radio(text)
It checks if there is a word radio in the phrase. If so, it will not send command to the Google Assistant API and will start radio player.