Python script: scarlet internet usage

Python and python framework

Moderator: leecollings

Post Reply
nood
Posts: 22
Joined: Sunday 26 July 2015 13:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python script: scarlet internet usage

Post by nood »

Hi

I made a small web scraping script with python to check how much of our monthly internetvolume we used.

Code: Select all

import requests
from bs4 import BeautifulSoup
import re

DOMOTICZ_IP = 'http://YOUR_DOMOTICZ_IP'
DEVICE_IDX = 'YOUR_DEVICE_IDX'

POST_LOGIN_URL = "https://www.scarlet.be/customercare/logon.do?language=nl"
REQUEST_URL ="https://www.scarlet.be/customercare/usage/dispatch.do"
payload = {
    "username": "YOUR_USERNAME",
    "password": "YOUR_PASSWORD"
}

with requests.Session() as session:
    post = session.post(POST_LOGIN_URL, data=payload)
#    print(post.status_code)
    page = session.get(REQUEST_URL)
#    print(page.status_code) 
    soup = BeautifulSoup(page.text,'html.parser')
    td = soup.find_all('td')
    number = (td[4].text)

numb = re.findall("\d+\.\d+", number)
value = numb[0]
fltvalue = float(value)
D_value = str(round(fltvalue,2))

requests.get(DOMOTICZ_IP + "/json.htm?type=command&param=udevice&idx=" + DEVICE_IDX + "&svalue=" + D_value)
you need 3 libraries for this script: Bs4, requests and re.
Also make a dummy counter device in domoticz and remember the IDX
Then adjust the parameters in the script.
I use crontab to run this script once a day.

I also made a custom sensor icon with the scarlet logo which I will send to the domoticz icons e-mailadres.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest