Upgrading the AIY Project from MagPi issue 57 Part 1

So with the release of MagPi issue 57 it came equipped with some hardware provided in partnership with Google to aid in creating a Google Home type device.

Obviously a bit more basic but with the ability to update it and make it your own.

Out the box what is provided is fine, but you get thinking about what you want it to do, and what I wanted was a way for it to tell me the top 10 news headlines from the BBC for the UK.

So by updating the action.py file I created a new class called ReadRssFeed, so instead of only reading the headlines of bbc I can now have it to read any rss feed.

The code is early stages and needs some changing to make it a bit more dynamic but it will give you an idea of what to do.

The Code



# Feedparser is used to parse the rss feeds (for more information view this for a good intro http://www.pythonforbeginners.com/feedparser/using-feedparser-in-python)
# Use pip to install feedparser (sudo pip install feedparser)
# include this at the top of the action.py
import feedparser

#this is the class to read rss feeds (add under the section of "Makers! Implement your own actions here")
class ReadRssFeed(object):
# This is the bbc rss feed for top news in the uk
# http://feeds.bbci.co.uk/news/rss.xml?edition=uk#

#######################################################################################
# constructor
# url - rss feed url to read
# feedCount - number of records to read
# -(for example bbc rss returns around 62 items and you may not want all of
# them read out so this allows limiting
#######################################################################################
def __init__(self, say, url, feedCount):
self.say = say
self.rssFeedUrl = url
self.feedCount = feedCount

def run(self, voice_command):
res = self.getNewsFeed()

# If res is empty then let user know
if res == "":
self.say('Cannot get the feed')

# loop res and speak the title of the rss feed
# here you could add further fields to read out
for item in res:
self.say(item.title_detail.value)

def getNewsFeed(self):
# parse the feed and get the result in res
res = feedparser.parse(self.rssFeedUrl)

# get the total number of entries returned
resCount = len(res.entries)

# exit out if empty
if resCount == 0:
return ""

# if the resCount is less than the feedCount specified cap the feedCount to the resCount
if resCount < self.feedCount:
self.feedCount = resCount

# create empty array
resultList = []

# loop from 0 to feedCount so we append the right number of entries to the return list
for x in range(0,self.feedCount):
resultList.append(res.entries[x])

return resultList

#Under the "Makers! Add your own voice commands here" you can use the following to add a news reader from the bbc
#by saying 'the news' you will be told of the top 10 news headlines from within the uk
actor.add_keyword(_('the news'), ReadRssFeed(say, "http://feeds.bbci.co.uk/news/rss.xml?edition=uk#",10))

Hopefully this is of some use to someone else as well. Any updates I do I will post back up here so everyone is aware.

Leave a Reply

Your email address will not be published. Required fields are marked *