Created
May 18, 2022 21:11
-
-
Save lachesis/6c7e5020b112fe8d7bcc83d99dae1f5f to your computer and use it in GitHub Desktop.
NWS weather textual discussion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import re | |
import sys | |
import requests | |
def main(): | |
SHOW_GROUPS = [ | |
r'SYNOPSIS', | |
r'DISCUSSION', | |
r'\w{3} WATCHES/WARNINGS/ADVISORIES', | |
r'NEAR TERM.*', | |
r'SHORT TERM.*', | |
r'LONG TERM.*', | |
r'AVIATION.*', | |
] | |
# Create the parser and add arguments | |
parser = argparse.ArgumentParser(description='Show National Weather Service textual products') | |
parser.add_argument('--list-products', action='store_true', help='List all available products for the city') | |
parser.add_argument('--product', '-p', default='AFD', help='NWS product (try: AFD, TAF, RVD, AFM)') | |
parser.add_argument('--raw', '-r', action='store_true', help='Print raw product? default is to parse only some groups') | |
parser.add_argument(dest='city', default='MTR', help="NWS city code (try: MTR, ILN)") | |
# Parse and print the results | |
args = parser.parse_args() | |
CITY = args.city.upper() | |
PRODUCT = args.product.upper() | |
if args.list_products: | |
txt = requests.get('https://forecast.weather.gov/product_types.php?site=' + CITY, timeout=30).text | |
for m in re.finditer(r'<a href="product_sites\.php\?site=[A-Z0-9]{3}&product=([A-Z0-9]{3})">([^<]{4,}?)</a>', txt): | |
print(m.group(1), "\t", m.group(2)) | |
return | |
txt = requests.get('https://forecast.weather.gov/product.php?site=NWS&issuedby='+CITY+'&product='+PRODUCT+'&format=txt&version=1&glossary=0', timeout=30).text | |
m = re.search(r'(?mis)<pre class="glossaryProduct">(.*)</pre>', txt) | |
if not m: | |
print("No product found") | |
return | |
rtxt = m.group(1) | |
if args.raw: | |
print(rtxt) | |
else: | |
printed = False | |
snipped = [] | |
groups = rtxt.split('&&\n') | |
for g in groups: | |
m = re.search(r'\.(.*?)\.\.\.', g) | |
if m: | |
if any(re.match(r, m.group(1)) for r in SHOW_GROUPS): | |
printed = True | |
print(g.strip()+'\n') | |
else: | |
snipped.append(m.group(1)) | |
if snipped: | |
print("Snipped groups:\n" + "\n".join(snipped)) | |
if not printed: | |
print(rtxt) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment