7 from typing import Dict
14 logger = logging.getLogger(__file__)
17 class stevens_renderer(renderer.abstaining_renderer):
18 URL = 'https://wsdot.com/Travel/Real-time/Service/api/MountainPass/Details/10'
20 def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
21 super().__init__(name_to_timeout_dict)
23 def render_conditions(mp: Dict[str, str], conditions: Dict[str, str]) -> str:
27 <TD><B>temperature:</B></TD>
28 <TD>{conditions['temperature']}°{conditions['temperatureUnit'][0]}</TD>
31 <TD><B>weather:</B></TD>
32 <TD>{conditions['weather']}</TD>
36 <TD>{conditions['roadCondition']}</TD>
38 if 'restrictionOne' in conditions or 'restrictionTwo' in conditions:
41 <TD><B>restrictions:</B></TD>
43 if 'restrictionOne' in conditions:
45 {conditions['restrictionOne']['travelDirectionName']}:
46 {conditions['restrictionOne']['publicPage']}<BR>'''
47 if 'restrictionTwo' in conditions:
49 {conditions['restrictionTwo']['travelDirectionName']}:
50 {conditions['restrictionTwo']['publicPage']}'''
51 ret += '</TR></TABLE>'
54 def render_forecast(forecasts: Dict[str, str]) -> str:
56 fc = forecasts['forecast']['forecastData']
57 for n, f in enumerate(fc):
60 color = ' BGCOLOR="#dfefff"'
63 <TD{color}><B>{f['periodText']}</B></TD>
64 <TD{color}>{f['forecastText']}</TD>
69 def render_image(cameras: Dict[str, str]) -> str:
70 for camera in cameras:
71 if camera['cameraId'] == 8063:
74 <IMG SRC={camera['cameraUrl']} WIDTH={camera['width']}>
76 <B>{camera['cameraLabel']} ({camera['direction']})</B>
80 def periodic_render(self, unused: str) -> bool:
81 page = requests.get(stevens_renderer.URL)
82 if page.status_code == 200:
83 contents = json.loads(page.content)
84 mp = contents['mountainPass']
85 conditions = contents['condition']
86 cameras = contents['cameras']
87 forecasts = contents['stationForecasts'][0]
88 now = datetime_utils.now_pacific()
89 tss = conditions['displayDate']
90 tss = tss.replace('Z', '+00:00')
91 ts = datetime.datetime.strptime(tss, '%Y-%m-%dT%H:%M:%S.%f%z')
92 tss = datetime_utils.describe_timedelta_briefly(now - ts)
93 with file_writer.file_writer('stevens-conditions_5_3000.html') as f:
95 <H2>Stevens Pass Conditions ~{tss} ago:</H2>
100 {stevens_renderer.render_conditions(mp, conditions)}
103 {stevens_renderer.render_image(cameras)}
108 {stevens_renderer.render_forecast(forecasts)}
115 test = stevens_renderer({"Test", 123})
116 test.periodic_render("Test")