-
Notifications
You must be signed in to change notification settings - Fork 0
/
internet.go
35 lines (28 loc) · 848 Bytes
/
internet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package widgets
import (
"encoding/json"
"fmt"
"log"
"os/exec"
)
// getConnectionData will return a floating-point number indicating the
// download speed, which is mostly what we are concerned with.
func getConnectionData() float64 {
buffer, err := exec.Command("speedtest", "-f", "json").Output()
if err != nil {
log.Fatal(err)
}
// Parse the bytes as if they are JSON and store them in the variable payload
var payload interface{}
json.Unmarshal(buffer, &payload)
// Payload is now "extractable".
// We can transform and store the data as a map.
data := payload.(map[string]interface{})
dataDownload := data["download"].(map[string]interface{})
bandwidth := dataDownload["bandwidth"].(float64)
var result float64 = (bandwidth / float64(1024*1024)) * 8.0
return result
}
func Run() {
fmt.Println(getConnectionData())
}