From 4e8f0017c752883e02f71d3edd99d54ea953ac55 Mon Sep 17 00:00:00 2001 From: John Peck Date: Sat, 29 Nov 2025 18:00:49 +0000 Subject: [PATCH] cocorahs --- covid.py | 139 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 122 insertions(+), 17 deletions(-) diff --git a/covid.py b/covid.py index ade4946..472ab25 100644 --- a/covid.py +++ b/covid.py @@ -11,6 +11,25 @@ import datetime from html import escape from tabulate import tabulate +def clean_text(text): + """Clean text by removing problematic characters that cause encoding issues.""" + if text is None: + return '' + + # Convert to string if not already + text = str(text) + + # Remove the problematic 'Â' character that appears with degree symbols + text = text.replace('\u00a0', ' ') # Non-breaking space to regular space + text = text.replace('\xc2', '') # Remove standalone  character + text = text.replace('°', '°') # Fix degree symbol encoding issue + text = text.replace('°F', '°F') # Fix degree F encoding issue + text = text.replace('°C', '°C') # Fix degree C encoding issue + text = text.replace('\u00a0', ' ') # Remove non-breaking spaces + text = text.encode('utf-8', errors='ignore').decode('utf-8') # Handle other encoding issues + + return text + allobs = [] states = ['wv', 'oh', 'va', 'ky'] @@ -25,7 +44,7 @@ try: response = requests.get(url) response.raise_for_status() # Check for HTTP errors data = xmltodict.parse(response.content.decode('utf-8')) # Explicitly decode as UTF-8 - + try: daily_reports = data.get('Cocorahs', {}).get('DailyPrecipReports') if daily_reports is None: @@ -55,13 +74,13 @@ try: finalobs = [] for obs in allobs: tempob = [ - obs.get('DateTimeStamp', ''), - obs.get('StationNumber', ''), - obs.get('StationName', ''), - obs.get('TotalPrecipAmt', ''), - obs.get('NewSnowDepth', ''), - obs.get('TotalSnowDepth', ''), - obs.get('Notes', '') + clean_text(obs.get('DateTimeStamp', '')), + clean_text(obs.get('StationNumber', '')), + clean_text(obs.get('StationName', '')), + clean_text(obs.get('TotalPrecipAmt', '')), + clean_text(obs.get('NewSnowDepth', '')), + clean_text(obs.get('TotalSnowDepth', '')), + clean_text(obs.get('Notes', '')) ] finalobs.append(tempob) @@ -78,27 +97,30 @@ try: html_content = """ + Cocorahs Weather Data - """ + datewanted + """

Cocorahs Weather Data - """ + datewanted + """

- +
- - - - - - - + + + + + + + @@ -112,6 +134,89 @@ try: html_content += """
Date/Time of Ob (Z)Station NumberStation NameNew PrecipNew SnowSnow DepthCommentsDate/Time of Ob (Z)Station NumberStation NameNew PrecipNew SnowSnow DepthComments
+ + """