Skip to content

Instantly share code, notes, and snippets.

@andresrcs
Created January 17, 2020 00:14
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 andresrcs/f42462c05bca7cf5656c8678bfe6c37a to your computer and use it in GitHub Desktop.
Save andresrcs/f42462c05bca7cf5656c8678bfe6c37a to your computer and use it in GitHub Desktop.
Python scrip for ETL speedtest-cli data into PostgreSQL
#!/usr/bin/python
import os
import re
import subprocess
import time
import pyodbc
try:
response = subprocess.Popen('/usr/local/bin/speedtest-cli', shell=True, stdout=subprocess.PIPE).stdout.read()
except:
pass
isp = 'movistar'
time = time.strftime('%Y-%m-%d %H:%M:%S')
ip = re.findall('Peru \((.*?)\)', response, re.MULTILINE)
ping = re.findall('km]:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\sM', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\sM', response, re.MULTILINE)
cnxn = pyodbc.connect(r'Driver={PostgreSQL ANSI};'
r'Uid=pi;'
r'Pwd=xxxxxx;'
r'Server=localhost;'
r'Port=5432;'
r'Database=speed_test;')
if ip != []:
ip = ip[0]
else:
ip = None
if ping != []:
ping = ping[0].replace(',', '.')
else:
ping = 0
if download != []:
download = download[0].replace(',', '.')
else:
download = 0
if upload != []:
upload = upload[0].replace(',', '.')
else:
upload = 0
try:
cursor = cnxn.cursor()
cursor.execute("insert into public.speed_test values (?, ?, ?, ?, ?, ?)", time, ip, ping, download, upload, isp)
cnxn.commit()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment