net_speed: allow bps or Bps

This patch provides a feature that allows the user to select between
Bits or Bytes when displaying the calculated network speed.  Also fixes
an issue that would sometimes cause the widget to break when
speedtest_cli couldn't find a good server.
This commit is contained in:
David Wahlstrom 2015-06-08 13:20:55 -07:00 committed by enkore
parent 9c246bc60f
commit 3ad6fc495c

View File

@ -4,6 +4,9 @@ import requests
import time import time
import os import os
from urllib.parse import urlparse from urllib.parse import urlparse
import contextlib
import sys
from io import StringIO
class NetSpeed(IntervalModule): class NetSpeed(IntervalModule):
@ -15,22 +18,43 @@ class NetSpeed(IntervalModule):
settings = ( settings = (
("url", "Target URL to download a file from. Uses speedtest_cli to " ("url", "Target URL to download a file from. Uses speedtest_cli to "
"find the 'best' server if none is supplied."), "find the 'best' server if none is supplied."),
("units", "Valid values are B, b, bytes, or bits"),
"format" "format"
) )
color = "#FFFFFF" color = "#FFFFFF"
interval = 300 interval = 300
url = None url = None
units = 'bits'
format = "{speed} ({hosting_provider})" format = "{speed} ({hosting_provider})"
def run(self): def run(self):
# since speedtest_cli likes to print crap, we need to squelch it
@contextlib.contextmanager
def nostdout():
save_stdout = sys.stdout
sys.stdout = StringIO()
yield
sys.stdout = save_stdout
if not self.url: if not self.url:
with nostdout():
try:
config = speedtest_cli.getConfig() config = speedtest_cli.getConfig()
servers = speedtest_cli.closestServers(config['client']) servers = speedtest_cli.closestServers(config['client'])
best = speedtest_cli.getBestServer(servers) best = speedtest_cli.getBestServer(servers)
# 1500x1500 is about 4.3MB, which seems like a reasonable place to # 1500x1500 is about 4.3MB, which seems like a reasonable place to
# start, i guess... # start, i guess...
url = '%s/random1500x1500.jpg' % os.path.dirname(best['url']) url = '%s/random1500x1500.jpg' % os.path.dirname(best['url'])
except KeyError:
url = None
if not url:
cdict = {
"speed": 0,
"hosting_provider": 'null',
}
else:
with open('/dev/null', 'wb') as devnull: with open('/dev/null', 'wb') as devnull:
start = time.time() start = time.time()
req = requests.get(url, stream=True) req = requests.get(url, stream=True)
@ -44,23 +68,35 @@ class NetSpeed(IntervalModule):
# note: dl_time is in seconds # note: dl_time is in seconds
dl_time = float(end - start) dl_time = float(end - start)
if total_length < 999: if self.units == 'bits' or self.units == 'b':
unit = "Bps" unit = 'bps'
bps = total_length / dl_time kilo = 1000
mega = 1000000
giga = 1000000000
factor = 8
elif self.units == 'bytes' or self.units == 'B':
unit = 'Bps'
kilo = 8000
mega = 8000000
giga = 8000000000
factor = 1
if total_length >= 1000 < 999999: if total_length < kilo:
unit = "KBps" bps = float(total_length / dl_time)
bps = (total_length / 1024.0) / dl_time
if total_length >= 1000000 < 999999999: if total_length >= kilo and total_length < mega:
unit = "MBps" unit = "K" + unit
bps = (total_length / (1024.0 * 1024.0)) / dl_time bps = float((total_length / 1024.0) / dl_time)
if total_length >= 10000000: if total_length >= mega and total_length < giga:
unit = "GBps" unit = "M" + unit
bps = (total_length / (1024.0 * 1024.0 * 1024.0)) / dl_time bps = float((total_length / (1024.0 * 1024.0)) / dl_time)
bps = "%.2f" % bps if total_length >= giga:
unit = "G" + unit
bps = float((total_length / (1024.0 * 1024.0 * 1024.0)) / dl_time)
bps = "%.2f" % (bps * factor)
speed = "%s %s" % (bps, unit) speed = "%s %s" % (bps, unit)
hosting_provider = '.'.join(urlparse(url).hostname.split('.')[-2:]) hosting_provider = '.'.join(urlparse(url).hostname.split('.')[-2:])