summaryrefslogtreecommitdiff
path: root/cached/weather_data.py
blob: 29a1d544b042101042eeb9d0a1eb781de094e427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# © Copyright 2021-2022, Scott Gasch

"""How's the weather?"""

import datetime
import json
import logging
import os
import urllib.request
from dataclasses import dataclass
from typing import Any, List

from overrides import overrides

import argparse_utils
import config
import datetime_utils
import list_utils
import persistent

logger = logging.getLogger(__name__)

cfg = config.add_commandline_args(
    f'Cached Weather Data List ({__file__})',
    'Arguments controlling cached weather data',
)
cfg.add_argument(
    '--weather_data_cachefile',
    type=str,
    default=f'{os.environ["HOME"]}/cache/.weather_summary_cache',
    metavar='FILENAME',
    help='File in which to cache weather data',
)
cfg.add_argument(
    '--weather_data_stalest_acceptable',
    type=argparse_utils.valid_duration,
    default=datetime.timedelta(seconds=7200),  # 2 hours
    metavar='DURATION',
    help='Maximum acceptable age of cached data.  If zero, forces a refetch',
)


@dataclass
class WeatherData:
    date: datetime.date  # The date
    high: float  # The predicted high in F
    low: float  # The predicted low in F
    precipitation_inches: float  # Number of inches of precipitation / day
    conditions: List[str]  # Conditions per ~3h window
    most_common_condition: str  # The most common condition
    icon: str  # An icon to represent it


@persistent.persistent_autoloaded_singleton()  # type: ignore
class CachedWeatherData(persistent.Persistent):
    def __init__(self, weather_data=None):
        if weather_data is not None:
            self.weather_data = weather_data
            return
        self.weather_data = {}
        icon_by_condition = {
            "Thunderstorm": "⚡",
            "Drizzle": "",
            "Rain": "☂️",
            "Snow": "❄️",
            "Clear": "☀️",
            "Clouds": "⛅",
            "Mist": "🌫",
            "Smoke": "🚬",
            "Haze": "🌥️",
            "Dust": "💨",
            "Fog": "🌁",
            "Sand": "🏜️",
            "Ash": "🌋",
            "Squall": "🌬",
            "Tornado": "🌪️",
        }
        now = datetime.datetime.now()
        dates = set()
        highs = {}
        lows = {}
        conditions = {}
        precip = {}
        param = "id=5786882"  # Bellevue, WA
        key = "c0b160c49743622f62a9cd3cda0270b3"
        www = urllib.request.urlopen(
            f'http://api.openweathermap.org/data/2.5/weather?zip=98005,us&APPID={key}&units=imperial'
        )
        response = www.read()
        www.close()
        parsed_json = json.loads(response)
        logger.debug(parsed_json)
        dt = datetime.datetime.fromtimestamp(parsed_json["dt"]).date()
        dates.add(dt)
        condition = parsed_json["weather"][0]["main"]
        icon = icon_by_condition.get(condition, '?')
        p = 0.0
        if 'rain' in parsed_json:
            if '3h' in parsed_json['rain']:
                p += float(parsed_json['rain']['3h'])
            elif '1h' in parsed_json['rain']:
                p += float(parsed_json['rain']['1h'])
        if 'snow' in parsed_json:
            if '3h' in parsed_json['snow']:
                p += float(parsed_json['snow']['3h'])
            elif '1h' in parsed_json['snow']:
                p += float(parsed_json['snow']['1h'])
        if dt == now.date() and now.hour > 18 and condition == 'Clear':
            icon = '🌙'
        self.weather_data[dt] = WeatherData(
            date=dt,
            high=float(parsed_json["main"]["temp_max"]),
            low=float(parsed_json["main"]["temp_min"]),
            precipitation_inches=p / 25.4,
            conditions=[condition],
            most_common_condition=condition,
            icon=icon,
        )

        www = urllib.request.urlopen(
            f"http://api.openweathermap.org/data/2.5/forecast?{param}&APPID={key}&units=imperial"
        )
        response = www.read()
        www.close()
        parsed_json = json.loads(response)
        logger.debug(parsed_json)
        count = parsed_json["cnt"]
        for x in range(count):
            data = parsed_json["list"][x]
            dt = datetime.datetime.strptime(data['dt_txt'], '%Y-%m-%d %H:%M:%S')
            dt = dt.date()
            dates.add(dt)
            if dt not in highs:
                highs[dt] = None
                lows[dt] = None
                conditions[dt] = []
            for temp in (
                data["main"]["temp"],
                data['main']['temp_min'],
                data['main']['temp_max'],
            ):
                if highs[dt] is None or temp > highs[dt]:
                    highs[dt] = temp
                if lows[dt] is None or temp < lows[dt]:
                    lows[dt] = temp
            cond = data["weather"][0]["main"]
            precip[dt] = 0.0
            if 'rain' in parsed_json:
                if '3h' in parsed_json['rain']:
                    precip[dt] += float(parsed_json['rain']['3h'])
                elif '1h' in parsed_json['rain']:
                    precip[dt] += float(parsed_json['rain']['1h'])
            if 'snow' in parsed_json:
                if '3h' in parsed_json['snow']:
                    precip[dt] += float(parsed_json['snow']['3h'])
                elif '1h' in parsed_json['snow']:
                    precip[dt] += float(parsed_json['snow']['1h'])
            conditions[dt].append(cond)

        today = datetime_utils.now_pacific().date()
        for dt in sorted(dates):
            if dt == today:
                high = highs.get(dt, None)
                if high is not None and self.weather_data[today].high < high:
                    self.weather_data[today].high = high
                continue
            most_common_condition = list_utils.most_common(conditions[dt])
            icon = icon_by_condition.get(most_common_condition, '?')
            if dt == now.date() and now.hour > 18 and condition == 'Clear':
                icon = '🌙'
            self.weather_data[dt] = WeatherData(
                date=dt,
                high=highs[dt],
                low=lows[dt],
                precipitation_inches=precip[dt] / 25.4,
                conditions=conditions[dt],
                most_common_condition=most_common_condition,
                icon=icon,
            )

    @classmethod
    @overrides
    def load(cls) -> Any:
        if persistent.was_file_written_within_n_seconds(
            config.config['weather_data_cachefile'],
            config.config['weather_data_stalest_acceptable'].total_seconds(),
        ):
            import pickle

            with open(config.config['weather_data_cachefile'], 'rb') as rf:
                weather_data = pickle.load(rf)
                return cls(weather_data)
        return None

    @overrides
    def save(self) -> bool:
        import pickle

        with open(config.config['weather_data_cachefile'], 'wb') as wf:
            pickle.dump(
                self.weather_data,
                wf,
                pickle.HIGHEST_PROTOCOL,
            )
        return True