Web Design IES
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Newsreader is a Python program that uses feedparser to download RSS headlines from your favourite news site, and gets the current weather using the Open Weather API. It appeared in a tutorial in The MagPi magazine, issue 121.
You can incorporate Newsreader in other Python projects. I've written a short program to display the headlines on a Display-O-Tron HAT, as an example. It also forms the basis of the news bulletins in my Raspberry Radio project.
UPDATE: You can now download both parts of my Raspberry Radio tutorial in a single PDF here.
On this webpage, you can download the code for the project.
A news headline shown on a Display-O-Tron HAT.
You'll need to install the required libraries. Open a terminal window and then enter the following instructions:
pip install feedparser
pip install requests
Save this program as rr_newsreader.py, so that it works with the other programs that will import it.
# rr_newsreader generates news, weather, and date reports
# From Raspberry Radio project in The MagPi by Sean McManus
import feedparser
import requests
import random
from datetime import datetime
def get_weather():
data = requests.get(
"http://api.openweathermap.org/data/2.5/weather?q=London,uk&units=metric&APPID=YOUR_API_KEY")
if data.status_code == 200:
report = random.choice(["The weather today is ",
"We're looking at ",
"Today, expect ",
"There's going to be ",
"Out and about today, you'll see "])
weather_forecast = data.json().get('weather')
description = weather_forecast[0].get('description')
report += description
main = data.json().get('main')
temperature = main.get('temp')
report += f". The temperature is {temperature} Celcius."
return report, temperature
else:
return "There is no weather report today.", False
def get_news():
news_list = []
rss_feed = feedparser.parse('https://www.theguardian.com/uk-news/rss')
news_list.append("Here's the news from The Guardian.")
for i in range(3):
news_list.append(rss_feed.entries[i].title)
return news_list
def get_date():
date = datetime.today()
date_text = f"It's {date.strftime('%A')} {date.strftime('%d')} {date.strftime('%B')}."
return date_text
Here's a simple program to output the date, news, and weather using rr_newsreader.py. You need to put both programs in the same folder.
# Print the weather, date and news from rr_newsreader.py
# From Raspberry Radio project in The MagPi by Sean McManus
import rr_newsreader
weather_report, temperature = rr_newsreader.get_weather()
print(weather_report)
date_report = rr_newsreader.get_date()
print(date_report)
news_report = rr_newsreader.get_news()
for line in news_report:
print(line)
Here's a program that displays the news headlines on a Display-O-Tron HAT, with a simple animation effect.
# News display for Displayotron HAT
# From Raspberry Radio project in The MagPi by Sean McManus
import rr_newsreader
import time
import dot3k.backlight as backlight
import dot3k.lcd as lcd
def display_text(text):
backlight.rgb(200, 200, 255)
for i in range(min(len(text), 48)):
substring = text[:i+1]
lcd.clear()
lcd.write(substring)
time.sleep(0.1)
backlight.rgb(255, 255, 255)
time.sleep(3)
date_report = rr_newsreader.get_date()
display_text(date_report)
news_report = rr_newsreader.get_news()
for line in news_report:
display_text(line)
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!
In this entertaining techno-thriller for adults, Sean McManus takes a slice through the music industry: from the boardroom to the stage; from the studio to the record fair.