X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=stevens_renderer.py;h=95a6d54c35d296339073f2cea938836eb92f0f96;hb=fa85ebf815dd7973250a5137e0152c2cb10a8b5e;hp=0916aee9fd694b1a6a1b84e0bb251c6d459b8111;hpb=86f7e14f34b43ed8eb8cf5eaf113a4ecca976327;p=kiosk.git diff --git a/stevens_renderer.py b/stevens_renderer.py index 0916aee..95a6d54 100644 --- a/stevens_renderer.py +++ b/stevens_renderer.py @@ -1,98 +1,111 @@ #!/usr/bin/env python3 -import datetime import json import logging import requests from typing import Dict -import datetime_utils import file_writer import renderer -logger = logging.getLogger(__file__) +logger = logging.getLogger(__name__) class stevens_renderer(renderer.abstaining_renderer): - URL = 'https://wsdot.com/Travel/Real-time/Service/api/MountainPass/Details/10' + URL = "https://wsdot.com/Travel/Real-time/Service/api/MountainPass/Details/10" def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None: super().__init__(name_to_timeout_dict) def render_conditions(mp: Dict[str, str], conditions: Dict[str, str]) -> str: - ret = f''' + ret = f""" - - + + - - + + - - -''' - if 'restrictionOne' in conditions or 'restrictionTwo' in conditions: - ret += ''' + + +""" + if "restrictionOne" in conditions and "restrictionTwo" in conditions: + ret += """ - -
temperature:{conditions['temperature']}°{conditions['temperatureUnit'][0]}Temp:{conditions['temperature']}°{conditions['temperatureUnit'][0]}
weather:{conditions['weather']}Weather:{conditions['weather']}
road:{conditions['roadCondition']}
Roadway:{conditions['roadCondition']}
restrictions:''' - if 'restrictionOne' in conditions: - ret += f''' - {conditions['restrictionOne']['travelDirectionName']}: - {conditions['restrictionOne']['publicPage']}
''' - if 'restrictionTwo' in conditions: - ret += f''' - {conditions['restrictionTwo']['travelDirectionName']}: - {conditions['restrictionTwo']['publicPage']}''' - ret += '
' + Restrictions: + """ + count = 0 + msg = conditions["restrictionOne"].get("publicPage", "no restrictions") + if msg.lower() != "no restrictions": + count += 1 + msg = conditions["restrictionTwo"].get("publicPage", "no restrictions") + if msg.lower() != "no restrictions": + count += 1 + if count == 2: + ret += f""" + {conditions['restrictionOne']['travelDirectionName']}: + {conditions['restrictionOne']['publicPage']}
+ {conditions['restrictionTwo']['travelDirectionName']}: + {conditions['restrictionTwo']['publicPage']}""" + elif count == 1: + msg = conditions["restrictionOne"].get("publicPage", "no restrictions") + if msg.lower() != "no restrictions": + ret += f""" + {conditions['restrictionOne']['travelDirectionName']}: + {conditions['restrictionOne']['publicPage']}
""" + else: + ret += f""" + {conditions['restrictionTwo']['travelDirectionName']}: + {conditions['restrictionTwo']['publicPage']}
""" + else: + ret += """None.
""" + ret += "
" return ret def render_forecast(forecasts: Dict[str, str]) -> str: - ret = '' - fc = forecasts['forecast']['forecastData'] + ret = "
" + fc = forecasts["forecast"]["forecastData"] for n, f in enumerate(fc): - color = '' + color = "" if n % 2 == 0: color = ' BGCOLOR="#dfefff"' - ret += f''' + ret += f""" - {f['periodText']} + {f['forecastText']} -''' - ret += '
{f['periodText']}
' +""" + ret += "" return ret def render_image(cameras: Dict[str, str]) -> str: for camera in cameras: - if camera['cameraId'] == 8063: - return f''' + if camera["cameraId"] == 8063: + return f"""
- + + +
- {camera['cameraLabel']} ({camera['direction']}) -
''' - return '' + {camera['cameraLabel']} ({camera['direction']}) +""" + return "" def periodic_render(self, unused: str) -> bool: page = requests.get(stevens_renderer.URL) if page.status_code == 200: contents = json.loads(page.content) - mp = contents['mountainPass'] - conditions = contents['condition'] - cameras = contents['cameras'] - forecasts = contents['stationForecasts'][0] - now = datetime_utils.now_pacific() - tss = conditions['displayDate'] - tss = tss.replace('Z', '+00:00') - ts = datetime.datetime.strptime(tss, '%Y-%m-%dT%H:%M:%S.%f%z') - tss = datetime_utils.describe_timedelta_briefly(now - ts) - with file_writer.file_writer('stevens-conditions_5_3000.html') as f: - f.write(f''' -

Stevens Pass Conditions ~{tss} ago:

+ mp = contents["mountainPass"] + conditions = contents["condition"] + cameras = contents["cameras"] + forecasts = contents["stationForecasts"][0] + with file_writer.file_writer("stevens-conditions_5_3000.html") as f: + f.write( + f""" +

Stevens Pass Conditions:


@@ -108,9 +121,12 @@ class stevens_renderer(renderer.abstaining_renderer): {stevens_renderer.render_forecast(forecasts)} -
''') +""" + ) return True return False -test = stevens_renderer({"Test", 123}) -test.periodic_render("Test") + +# Test: +# test = stevens_renderer({"Test", 123}) +# test.periodic_render("Test")