pythonxmlfor-loopfeedparsercap

Python XML Feed parser cant find specific event


I am making a python script to read the NWS CAP alerts, and look for a tornado warning. I have the script far enough to read the XML, However I cant get it to return true when a tornado warning is issued. This is because my script will only read one of the events in the XML. I Now have made it read all of the XML however it wont return true if there is a tornado warning unless the tornado warning is the last event because of how it loops through. Can anyone give me any help in terms of whenever a tornado warning is issued, even with other alerts, it will return true?

import feedparser
import time

next_check = time.time()
tornado = False

while True:        

    if time.time() > next_check:
        url = 'http://127.0.0.1/test.xml'
        feed = feedparser.parse(url)

        for entry in feed.entries:
            if entry.has_key("cap_event") is False:
                tornado = False    
            else:
                if entry.cap_event == "Tornado Warning":
                    tornado = True    
            else:
                tornado = False


        print("Tornado: " + str(tornado))
        print("EVENT: " + entry.cap_event)
        next_check = time.time() + 5

Here is an XML Example to test it

 <?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>

<!--
This atom/xml feed is an index to active advisories, watches and warnings 
issued by the National Weather Service.  This index file is not the complete 
Common Alerting Protocol (CAP) alert message.  To obtain the complete CAP 
alert, please follow the links for each entry in this index.  Also note the 
CAP message uses a style sheet to convey the information in a human readable 
format.  Please view the source of the CAP message to see the complete data 
set.  Not all information in the CAP message is contained in this index of 
active alerts.
-->

<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>

<!-- TZN = <CDT> -->
<!-- TZO = <-5> -->
<!-- http-date = Mon, 04 May 2015 01:30:00 GMT -->
<id>https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&amp;y=0</id>
<generator>NWS CAP Server</generator>
<updated>2015-05-03T20:30:00-05:00</updated>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Current Watches, Warnings and Advisories for Lancaster (NEC109) Nebraska Issued by the National Weather Service</title>
<link href='https://alerts.weather.gov/cap/wwaatmget.php?x=NEC109&amp;y=0'/>
<entry>
<id>...</id>
<updated>2015-05-03T20:30:00-05:00</updated>
<published>2015-05-03T20:30:00-05:00</published>
<author>...</author>
<title>...</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/>
<summary>...</summary>
<cap:event>Flood Warning</cap:event>
<cap:effective>2015-05-03T20:30:00-05:00</cap:effective>
<cap:expires>2015-05-03T21:00:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Extreme</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc>
<cap:polygon>
40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79
</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031067 031097 031109</value>
<valueName>UGC</valueName>
<value>NEC067 NEC097 NEC109</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value>
</cap:parameter>
</entry>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e</id>
<updated>2015-05-03T20:30:00-05:00</updated>
<published>2015-05-03T20:30:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Tornado Warning issued May 03 at 8:30PM CDT until May 03 at 9:00PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23CC8.TornadoWarning.1253A7A25820NE.OAXTOROAX.267e2b2c2775d89536dfa15ab433866e"/>
<summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * TORNADO WARNING FOR... NORTHWESTERN JOHNSON COUNTY IN SOUTHEASTERN NEBRASKA... NORTHEASTERN GAGE COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 900 PM CDT</summary>
<cap:event>Tornado Warning</cap:event>
<cap:effective>2015-05-03T20:30:00-05:00</cap:effective>
<cap:expires>2015-05-03T21:00:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Extreme</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Gage; Johnson; Lancaster</cap:areaDesc>
<cap:polygon>40.52,-96.79 40.66,-96.64 40.57,-96.46 40.52,-96.46 40.52,-96.37 40.46,-96.25 40.26,-96.48 40.52,-96.79</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031067 031097 031109</value>
<valueName>UGC</valueName>
<value>NEC067 NEC097 NEC109</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.TO.W.0002.150504T0130Z-150504T0200Z/</value>
</cap:parameter>
</entry>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7</id>
<updated>2015-05-03T20:08:00-05:00</updated>
<published>2015-05-03T20:08:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Severe Thunderstorm Warning issued May 03 at 8:08PM CDT until May 03 at 8:45PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=NE1253A7A23430.SevereThunderstormWarning.1253A7A242A4NE.OAXSVROAX.d1cb461c9fb96a7e2ebbf6ddabdd25c7"/>
<summary>THE NATIONAL WEATHER SERVICE IN OMAHA HAS ISSUED A * SEVERE THUNDERSTORM WARNING FOR... SOUTHEASTERN LANCASTER COUNTY IN SOUTHEASTERN NEBRASKA... SOUTHWESTERN OTOE COUNTY IN SOUTHEASTERN NEBRASKA... * UNTIL 845 PM CDT * AT 808 PM CDT...A SEVERE THUNDERSTORM WAS LOCATED OVER PANAMA...OR</summary>
<cap:event>Severe Thunderstorm Warning</cap:event>
<cap:effective>2015-05-03T20:08:00-05:00</cap:effective>
<cap:expires>2015-05-03T20:45:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Immediate</cap:urgency>
<cap:severity>Severe</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Lancaster; Otoe</cap:areaDesc>
<cap:polygon>40.53,-96.6 40.67,-96.57 40.67,-96.32 40.52,-96.33 40.52,-96.56 40.53,-96.6</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>031109 031131</value>
<valueName>UGC</valueName>
<value>NEC109 NEC131</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.NEW.KOAX.SV.W.0045.150504T0108Z-150504T0145Z/</value>
</cap:parameter>
</entry>
</feed>

Solution

  • If I understand you question correctly this should hopefully be a quick fix.

    My understanding of the question is that you want the use the feedparser to check an xml file to see if it contains the following xml entry:

    <cap:event>Tornado Warning</cap:event>
    

    If it does anywhere in the document you want to have the variable tornado stored as True.

    (You mention that you want to 'return' true but as you are not in a function, I speculated that you meant just change the variable to True)

    Basically you just need to remove the else part of your statement. So it is like this:

    (I removed 'if entry.has_key("cap_event") is False:' as I think it might be redundant as well)

    import feedparser
    import time
    
    next_check = time.time()
    #tornado = False 
    
    while True:
    
        if time.time() > next_check:
    
            feed = feedparser.parse(r'/home/tomnl/test.xml')
    
            # if tornado is declared here it means the file is checked a fresh
            # each time
            tornado = False 
    
            for entry in feed.entries:
    
                if entry.cap_event == "Tornado Warning":
                    tornado = True
    
                print("Tornado: " + str(tornado))
                print("EVENT: " + entry.cap_event)
                next_check = time.time() + 5
    
            print("FINAL tornado", tornado)
    

    Hope it helps.