import re 
import subprocess
from influxdb import InfluxDBClient

response = subprocess.Popen('/usr/local/bin/speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
ping = re.findall('Ping:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)

ping = ping[0].replace(',', '.')
download = download[0].replace(',', '.')
upload = upload[0].replace(',', '.')

speed_data = [
	{
		"measurement" : "internet_speed",
		"tags" : {
			"host" : "RaspberryPiMyLife"
		},
		"fields" : {
			"download" : float(download),
			"upload" : float(upload),
			"ping" : float(ping)
		}
	}
]

client = InfluxDBClient('localhost', 8086, 'speedmonitor', 'nisarg1996', 'internetspeed')
client.write_points(speed_data)
