Fix alert content filtering
[weather.git] / weather.py
index 4103dee..8df769e 100644 (file)
@@ -336,7 +336,7 @@ def get_alert(
     if alert:
         if verbose: return alert
         else:
-            if alert.find("\nNATIONAL WEATHER SERVICE") == -1:
+            if alert.find("\nNational Weather Service") == -1:
                 muted = False
             else:
                 muted = True
@@ -350,7 +350,7 @@ def get_alert(
 
             # report alerts and forecasts that expired less than offset ago;
             # this is a cheap hack since expiration times seem to be relative
-            # to the zone's local timezone, and converting from the user's
+            # to the zone's local time zone, and converting from the user's
             # would get complicated, but also there can sometimes be a lag
             # between expiration and the next update
             valid_time = time.strftime(
@@ -360,7 +360,7 @@ def get_alert(
                 if line.startswith("Expires:") \
                     and "Expires:" + valid_time > line:
                     return ""
-                if muted and line.startswith("NATIONAL WEATHER SERVICE"):
+                if muted and line.startswith("National Weather Service"):
                     muted = False
                     line = ""
                 elif line == "&&":
@@ -1239,7 +1239,7 @@ def gecos(formatted):
     return tuple(coordinates)
 
 def correlate():
-    import codecs, csv, datetime, hashlib, os, re, sys, time, zipfile
+    import codecs, csv, datetime, hashlib, os, re, sys, time, zipfile, zoneinfo
     if pyversion("3"): import configparser
     else: import ConfigParser as configparser
     for filename in os.listdir("."):
@@ -1578,6 +1578,37 @@ def correlate():
                     zones[zone]["zone_forecast"] = (
                         "https://tgftp.nws.noaa.gov/data/forecasts/zone/"
                         "%s/%s.txt" % (state.lower(), zone))
+                tzcode = fields[7]
+                if tzcode == "A":
+                    zones[zone]["tz"] = "US/Alaska"
+                elif tzcode == "AH":
+                    zones[zone]["tz"] = "US/Aleutian"
+                elif tzcode in ("C", "CE", "CM"):
+                    zones[zone]["tz"] = "US/Central"
+                elif tzcode in ("E", "e"):
+                    zones[zone]["tz"] = "US/Eastern"
+                elif tzcode == "F":
+                    zones[zone]["tz"] = "Pacific/Guadalcanal"
+                elif tzcode == "G":
+                    zones[zone]["tz"] = "Pacific/Guam"
+                elif tzcode == "H":
+                    zones[zone]["tz"] = "US/Hawaii"
+                elif tzcode == "J":
+                    zones[zone]["tz"] = "Japan"
+                elif tzcode == "K":
+                    zones[zone]["tz"] = "Pacific/Kwajalein"
+                elif tzcode in ("M", "MC", "MP"):
+                    zones[zone]["tz"] = "US/Mountain"
+                elif tzcode == "m":
+                    zones[zone]["tz"] = "US/Arizona"
+                elif tzcode == "P":
+                    zones[zone]["tz"] = "US/Pacific"
+                elif tzcode == "S":
+                    zones[zone]["tz"] = "US/Samoa"
+                elif tzcode == "V":
+                    zones[zone]["tz"] = "America/Virgin"
+                else:
+                    zones[zone]["tz"] = ""
                 county = fields[5]
                 if county:
                     if description.endswith(county):
@@ -2107,6 +2138,7 @@ def correlate():
             zctas_nocentroid += 1
     zones_nocentroid = 0
     zones_nodescription = 0
+    zones_notz = 0
     zones_noforecast = 0
     zones_overlapping = 0
     zonetable = {}
@@ -2134,6 +2166,10 @@ def correlate():
         if not zones.has_option(zone, "description"):
             qalog.append("%s: no description\n" % zone)
             zones_nodescription += 1
+        if not zones.has_option(zone, "tz") or not zones.get(
+                zone, "tz") in zoneinfo.available_timezones():
+            qalog.append("%s: no time zone\n" % zone)
+            zones_notz += 1
         if not zones.has_option(zone, "zone_forecast"):
             qalog.append("%s: no forecast\n" % zone)
             zones_noforecast += 1
@@ -2170,6 +2206,8 @@ def correlate():
             print("   %s zones with no centroid"%zones_nocentroid)
         if zones_nodescription:
             print("   %s zones with no description"%zones_nodescription)
+        if zones_notz:
+            print("   %s zones with no time zone"%zones_notz)
         if zones_noforecast:
             print("   %s zones with no forecast"%zones_noforecast)
         if zones_overlapping: