from elementtree.ElementTree import ElementTree from urllib import urlopen from os import execlp # URL of the CAP file to read. Modify this line to represent the state you want CAP alerts from. capurl = 'http://www.nws.noaa.gov/alerts/tx.cap' # FIPS Geocode for your area as it appears in the CAP file # As a string, not a number! This does a string compare against the CAP file # Usually will be 0 + state code + county code # http://www.itl.nist.gov/fipspubs/co-codes/states.htm geocode = '048111' # The event type you want to get an alert for. event = 'Flood Warning' # This is the CAP XML Namespace cns = '{http://www.incident.com/cap/1.0}' cap = ElementTree(file = urlopen(capurl)) allalerts = cap.getroot().findall(cns+'info') # print("Alerts obtained") # alerts = [a for a in allalerts if a.findtext(cns+'area/'+cns+'geocode') == geocode] alerts = [a for a in allalerts if (a.findtext(cns+'event') == event) & (a.findtext(cns+'area/'+cns+'geocode') == geocode)] for alert in alerts: headline = alert.findtext(cns+'event') if headline: headline = headline.strip() # execlp('mythtvosd', 'mythtvosd', '--template=alert', '--alert_text="%s"' % headline) # print("%s" % headline) # print("ALERTS Printed") execlp('mythtvosd', 'mythtvosd', '--template=scroller', '--scroll_text="%s"' % headline)