Skip to content

Instantly share code, notes, and snippets.

@ziouf
Last active March 13, 2019 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziouf/7c8cbaefd428422379a727116e7314fd to your computer and use it in GitHub Desktop.
Save ziouf/7c8cbaefd428422379a727116e7314fd to your computer and use it in GitHub Desktop.
Home monitoring

# Command line

PHONE_ID=... python main.py

#! /usr/bin/env python
# coding: utf-8
import os
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
_url = 'https://measurements.mobile-alerts.eu'
_phoneId = os.environ.get('PHONE_ID')
def getSensorName(sensor):
if 'date' in sensor.text:
return 'Date'
return sensor.text
def getSummary():
r = requests.get(
'{}/Home/SensorsOverview?phoneid={}'.format(_url, _phoneId))
return [getDetail(sensor)
for sensor in BeautifulSoup(r.text, 'html.parser').find_all('div', 'sensor')]
def getDetail(sensor):
post_data = {
'fromepoch': (datetime.now() - timedelta(days=1)).timestamp(),
'toepoch': datetime.now().timestamp(),
'pagesize': 250
}
r = requests.post('{}{}'.format(_url, sensor.a['href']), data=post_data)
soup = BeautifulSoup(r.text, 'html.parser').find(
'form', {'id': 'MeasurementDetails'})
header = [getSensorName(h) for h in soup.select(
'table thead tr th:nth-child(1n+2)')]
for line in soup.select('table tbody tr'):
publishIntoDb({
"measurement": "mobilealerts",
"fields": { header[i]: td.text for i, td in enumerate(line.select('td:nth-child(1n+2)')) },
"tags": {
"location": sensor.a.text,
},
"date": line.select('td')[0].text,
})
def publishIntoDb(data):
print(data)
# TODO: write here code to publish into influxDb
if __name__ == "__main__":
getSummary()
requests
beautifulsoup4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment