X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=stevens_renderer.py;h=bf97785256933beb350d84d9ee47b87a59ca8ec2;hb=addd4980077f6e3857c5c035b49784dc3ceca49a;hp=6d8768ee387a12ecd50dfb68471bb2d8d97c27ef;hpb=c06bfef53f70551e7920bc4facce27f47b89e2ba;p=kiosk.git diff --git a/stevens_renderer.py b/stevens_renderer.py index 6d8768e..bf97785 100644 --- a/stevens_renderer.py +++ b/stevens_renderer.py @@ -1,55 +1,129 @@ #!/usr/bin/env python3 -import http.client -from typing import List, Dict -import xml.etree.ElementTree as ET +import json +import logging +import requests +from typing import Dict -import renderer import file_writer +import renderer + + +logger = logging.getLogger(__file__) + +class stevens_renderer(renderer.abstaining_renderer): + URL = 'https://wsdot.com/Travel/Real-time/Service/api/MountainPass/Details/10' -class stevens_pass_conditions_renderer(renderer.debuggable_abstaining_renderer): - """Renders a page about Stevens Pass conditions.""" - - def __init__( - self, name_to_timeout_dict: Dict[str, int], feed_site: str, feed_uris: List[str] - ) -> None: - super(stevens_pass_conditions_renderer, self).__init__( - name_to_timeout_dict, False - ) - self.feed_site = feed_site - self.feed_uris = feed_uris - - def debug_prefix(self) -> str: - return "stevens" - - def periodic_render(self, key: str) -> bool: - with file_writer.file_writer("stevens-conditions_1_86400.html") as f: - for uri in self.feed_uris: - self.conn = http.client.HTTPSConnection(self.feed_site) - self.conn.request("GET", uri, None, {"Accept-Charset": "utf-8"}) - response = self.conn.getresponse() - if response.status == 200: - raw = response.read() - rss = ET.fromstring(raw) - channel = rss[0] - for item in channel.getchildren(): - if item.tag == "title": - f.write(f"

{item.text}


") - f.write( - '' - ) - elif item.tag == "item": - for x in item.getchildren(): - if x.tag == "description": - text = x.text - text = text.replace( - "Stevens Pass US2
", "" - ) - text = text.replace("

", "
") - text = text.replace( - "Elevation Meters:1238
", "" - ) - f.write("

\n%s\n" % text) - return True + 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''' + + + + + + + + + + + + +''' + if 'restrictionOne' in conditions and 'restrictionTwo' in conditions: + ret += ''' + + +
Temp:{conditions['temperature']}°{conditions['temperatureUnit'][0]}
Weather:{conditions['weather']}
Roadway:{conditions['roadCondition']}
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'] + for n, f in enumerate(fc): + color = '' + if n % 2 == 0: + color = ' BGCOLOR="#dfefff"' + ret += f''' + + + {f['forecastText']} +''' + ret += '
{f['periodText']}
' + return ret + + def render_image(cameras: Dict[str, str]) -> str: + for camera in cameras: + if camera['cameraId'] == 8063: + return f''' +

+ + + +
+ {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] + with file_writer.file_writer('stevens-conditions_5_3000.html') as f: + f.write(f''' +

Stevens Pass Conditions:

+
+ + + + + + + + +
+ {stevens_renderer.render_conditions(mp, conditions)} + + {stevens_renderer.render_image(cameras)} +
+ {stevens_renderer.render_forecast(forecasts)} +
''') + return True return False + +# Test: +#test = stevens_renderer({"Test", 123}) +#test.periodic_render("Test")