{"id":12,"date":"2017-05-10T23:17:09","date_gmt":"2017-05-10T22:17:09","guid":{"rendered":"http:\/\/www.markwilliamdavies.co.uk\/blog\/?p=12"},"modified":"2017-05-19T16:31:14","modified_gmt":"2017-05-19T15:31:14","slug":"upgrading-the-aiy-project-from-magpi-issue-57","status":"publish","type":"post","link":"http:\/\/www.markwilliamdavies.co.uk\/blog\/?p=12","title":{"rendered":"Upgrading the AIY Project from MagPi issue 57 Part 1"},"content":{"rendered":"<p>So with the release of MagPi issue 57 it came equipped with some hardware provided in partnership with Google to aid in creating a <a href=\"https:\/\/www.google.co.uk\/search?q=google+home&amp;oq=google+home&amp;aqs=chrome..69i57j0l5.2500j0j7&amp;sourceid=chrome&amp;ie=UTF-8\">Google Home<\/a>\u00a0type device.<\/p>\n<p>Obviously a bit more basic but with the ability to update it and make it your own.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><strong>The Code<\/strong><\/p>\n<hr \/>\n<pre><code language=\"python\">\r\n# 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)\r\n# Use pip to install feedparser (sudo pip install feedparser)\r\n# include this at the top of the action.py\r\nimport feedparser\r\n\r\n#this is the class to read rss feeds (add under the section of \"Makers! Implement your own actions here\")\r\nclass ReadRssFeed(object):\r\n# This is the bbc rss feed for top news in the uk\r\n# http:\/\/feeds.bbci.co.uk\/news\/rss.xml?edition=uk#\r\n\r\n#######################################################################################\r\n# constructor\r\n# url - rss feed url to read\r\n# feedCount - number of records to read\r\n# -(for example bbc rss returns around 62 items and you may not want all of\r\n# them read out so this allows limiting\r\n#######################################################################################\r\ndef __init__(self, say, url, feedCount):\r\nself.say = say\r\nself.rssFeedUrl = url\r\nself.feedCount = feedCount\r\n\r\ndef run(self, voice_command):\r\nres = self.getNewsFeed()\r\n\r\n# If res is empty then let user know\r\nif res == \"\":\r\nself.say('Cannot get the feed')\r\n\r\n# loop res and speak the title of the rss feed\r\n# here you could add further fields to read out\r\nfor item in res:\r\nself.say(item.title_detail.value)\r\n\r\ndef getNewsFeed(self):\r\n# parse the feed and get the result in res\r\nres = feedparser.parse(self.rssFeedUrl)\r\n\r\n# get the total number of entries returned\r\nresCount = len(res.entries)\r\n\r\n# exit out if empty\r\nif resCount == 0:\r\nreturn \"\"\r\n\r\n# if the resCount is less than the feedCount specified cap the feedCount to the resCount\r\nif resCount &lt; self.feedCount:\r\nself.feedCount = resCount\r\n\r\n# create empty array\r\nresultList = []\r\n\r\n# loop from 0 to feedCount so we append the right number of entries to the return list\r\nfor x in range(0,self.feedCount):\r\nresultList.append(res.entries[x])\r\n\r\nreturn resultList\r\n\r\n#Under the \"Makers! Add your own voice commands here\" you can use the following to add a news reader from the bbc\r\n#by saying 'the news' you will be told of the top 10 news headlines from within the uk\r\nactor.add_keyword(_('the news'), ReadRssFeed(say, \"http:\/\/feeds.bbci.co.uk\/news\/rss.xml?edition=uk#\",10))\r\n<\/code><\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0type 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 &hellip; <a href=\"http:\/\/www.markwilliamdavies.co.uk\/blog\/?p=12\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Upgrading the AIY Project from MagPi issue 57 Part 1<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[5,6,3],"_links":{"self":[{"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12"}],"collection":[{"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":12,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":27,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions\/27"}],"wp:attachment":[{"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.markwilliamdavies.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}