Skip to main content

Making a weather status screen in Python

As part of my Raspberry Pi powered status script I show the current weather for my city of Montreal. At first I used the widget provided by Weather Canada, but I decided to move to a custom web page by using a simple Python script that reads the RSS feed from the same Weather Canada service, and then show the result using Font Awesome icons and the Bootstrap framework.

This is what the result looks like:

The first thing to do is import the needed libraries, including feedparser which allows us to parse the RSS feed, then connect to the feed and extract the information. Finally, we display a bootstrap table with the data for the few next upcoming days.

This is the complete code:

#!/usr/bin/env python3

import os
import sys
import time
import connix
import feedparser
import re

# This is the feed URL
rss = feedparser.parse("")


print("<html><head><link href='' rel='stylesheet' /><link href='' rel='stylesheet' /><style>body{background-color:#000000;color:#FFFFFF;font-family:Verdana;}*{font-size:24px!important;}</style></head><body>")

def get_temp(t):
    # Return the current temperature information from the title
    return str(t).split(':')[1]

def get_icon(a):
    # Return an appropriate icon based on the title information
    icon = "<i class='fa fa-question'></i>"
    if "rain" in str(a).lower():
        icon = "<i class='fa fa-cloud-rain'></i>"
    elif "shower" in str(a).lower():
        icon = "<i class='fa fa-cloud-showers-heavy'></i>"
    elif "cloud" in str(a).lower():
        icon = "<i class='fa fa-cloud'></i>"
    elif "snow" in str(a).lower():
        icon = "<i class='fa fa-snow'></i>"
    elif "sun" in str(a).lower():
        icon = "<i class='fa fa-sun'></i>"
    elif "clear" in str(a).lower():
        icon = "<i class='fa fa-cloud-sun'></i>"
    return icon

print("<table class='table table-bordered'>")
print("<tr><td><b>Currently:</b></td><td>{} {}</td></tr>".format(get_icon(rss['items'][1]['title']), get_temp(rss['items'][1]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][2]['title']).split(':')[0], get_icon(rss['items'][2]['title']), get_temp(rss['items'][2]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][3]['title']).split(':')[0], get_icon(rss['items'][3]['title']), get_temp(rss['items'][3]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][4]['title']).split(':')[0], get_icon(rss['items'][4]['title']), get_temp(rss['items'][4]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][5]['title']).split(':')[0], get_icon(rss['items'][5]['title']), get_temp(rss['items'][5]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][6]['title']).split(':')[0], get_icon(rss['items'][6]['title']), get_temp(rss['items'][6]['title'])))