Skip to content

Instantly share code, notes, and snippets.

@martin-kokos
Last active August 31, 2020 18:48
Show Gist options
  • Save martin-kokos/24ad95bd9c850505de64c738b63eb5f9 to your computer and use it in GitHub Desktop.
Save martin-kokos/24ad95bd9c850505de64c738b63eb5f9 to your computer and use it in GitHub Desktop.
Kravi hora occupancy provider
'''
Provides occupancy number http://localhost:8080/kravi_hora
'''
import logging
import re
import requests
from bs4 import BeautifulSoup
from aiohttp import web
log = logging.getLogger(__name__)
def get_metric(soup, name_text):
try:
name_e = soup.find_all(string=re.compile(name_text))[0]
value = name_e.parent.find_all('strong')[0].text
value = re.search(r'(^\d+(?:\.\d+)?)', value).group(0)
value = float(value)
except Exception as e:
log.error('Failed to parse %s', name_text, exc_info=e)
return None
return value
async def kravi_hora(request):
metrics = {}
resp = requests.get('https://www.kravihora-brno.cz/venkovni-bazeny')
soup = BeautifulSoup(resp.text, 'html.parser')
metrics.update({
'venku_obsazenost': get_metric(soup, 'Obsazenost'),
'venku_t_50m_bazen': get_metric(soup, 'Teplota 50 m bazénu'),
'venku_t_detsky_bazen': get_metric(soup, 'Teplota dětského bazénu'),
'venku_t_vzduchu': get_metric(soup, 'Teplota vzduch venku'),
})
resp = requests.get('https://www.kravihora-brno.cz/kryta-plavecka-hala')
soup = BeautifulSoup(resp.text, 'html.parser')
metrics.update({
'vnitrni_obsazenost': get_metric(soup, 'Obsazenost'),
'vnitrni_t_bazen': get_metric(soup, 'Teplota vnitřní bazén'),
'vnitrni_t_detsky_bazen': get_metric(soup, 'Teplota dětský bazén'),
'vnitrni_t_vyrivka': get_metric(soup, 'Teplota vířivka'),
'vnitrni_t_vzduchu': get_metric(soup, 'Teplota vzduch hala'),
})
prometheus_format = '\n'.join(f'{name} {value}' for name, value in metrics.items())
return web.Response(text=prometheus_format)
app = web.Application()
app.add_routes([
web.get('/kravi_hora', kravi_hora),
])
if __name__ == '__main__':
web.run_app(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment