1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

create another module to use another ipinfo api

This commit is contained in:
FengYa 2018-06-08 13:44:38 +08:00 committed by Chris Cummer
parent 0d8861e358
commit cd983a8e8f
2 changed files with 145 additions and 0 deletions

141
ipinfohigherlimit/widget.go Normal file
View File

@ -0,0 +1,141 @@
package ipinfohigherlimit
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"text/template"
"bytes"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
// Widget widget struct
type Widget struct {
wtf.TextWidget
result string
colors struct {
name, value string
}
}
type ipinfo struct {
Query string `json:"query"`
ISP string `json:"isp"`
AS string `json:"as"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lon"`
PostalCode string `json:"zip"`
Organization string `json:"org"`
Timezone string `json:"timezone"`
}
// NewWidget constructor
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" IPInfo ", "ipinfohigherlimit", false),
}
widget.View.SetWrap(false)
widget.config()
return &widget
}
// Refresh refresh the module
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.UpdateRefreshedAt()
widget.ipinfo()
widget.View.Clear()
widget.View.SetText(widget.result)
}
//this method reads the config and calls ipinfo for ip information
func (widget *Widget) ipinfo() {
client := &http.Client{}
req, err := http.NewRequest("GET", "http://ip-api.com/json", nil)
if err != nil {
widget.result = fmt.Sprintf("%s", err.Error())
return
}
req.Header.Set("User-Agent", "curl")
response, err := client.Do(req)
if err != nil {
widget.result = fmt.Sprintf("%s", err.Error())
return
}
defer response.Body.Close()
if err != nil {
widget.result = fmt.Sprintf("%s", err.Error())
return
}
var info ipinfo
err = json.NewDecoder(response.Body).Decode(&info)
if err != nil {
widget.result = fmt.Sprintf("%s", err.Error())
return
}
widget.setResult(&info)
}
// read module configs
func (widget *Widget) config() {
nameColor, valueColor := Config.UString("wtf.mods.ipinfo.colors.name", "red"), Config.UString("wtf.mods.ipinfo.colors.value", "white")
widget.colors.name = nameColor
widget.colors.value = valueColor
}
func (widget *Widget) setResult(info *ipinfo) {
resultTemplate, _ := template.New("ipinfo_result").Parse(
formatableText("IP Address", "Ip") +
formatableText("ISP", "ISP") +
formatableText("AS", "AS") +
formatableText("City", "City") +
formatableText("Region", "Region") +
formatableText("Country", "Country") +
formatableText("Coordinates", "Coordinates") +
formatableText("Postal Code", "PostalCode") +
formatableText("Organization", "Organization") +
formatableText("Timezone", "Timezone"),
)
resultBuffer := new(bytes.Buffer)
resultTemplate.Execute(resultBuffer, map[string]string{
"nameColor": widget.colors.name,
"valueColor": widget.colors.value,
"Ip": info.Query,
"ISP": info.ISP,
"AS": info.AS,
"City": info.City,
"Region": info.Region,
"Country": info.Country,
"Coordinates": strconv.FormatFloat(info.Latitude, 'f', 6, 64) + "," + strconv.FormatFloat(info.Longitude, 'f', 6, 64),
"PostalCode": info.PostalCode,
"Organization": info.Organization,
"Timezone": info.Timezone,
})
widget.result = resultBuffer.String()
}
func formatableText(key, value string) string {
return fmt.Sprintf(" [{{.nameColor}}]%s: [{{.valueColor}}]{{.%s}}\n", key, value)
}

4
wtf.go
View File

@ -22,6 +22,7 @@ import (
"github.com/senorprogrammer/wtf/gitlab"
"github.com/senorprogrammer/wtf/help"
"github.com/senorprogrammer/wtf/ipinfo"
"github.com/senorprogrammer/wtf/ipinfohigherlimit"
"github.com/senorprogrammer/wtf/jira"
"github.com/senorprogrammer/wtf/newrelic"
"github.com/senorprogrammer/wtf/opsgenie"
@ -188,6 +189,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) {
Widgets = append(Widgets, gitlab.NewWidget(app, pages))
case "ipinfo":
Widgets = append(Widgets, ipinfo.NewWidget())
case "ipinfohigherlimit":
Widgets = append(Widgets, ipinfohigherlimit.NewWidget())
case "jira":
Widgets = append(Widgets, jira.NewWidget())
case "newrelic":
@ -229,6 +232,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
github.Config = Config
gitlab.Config = Config
ipinfo.Config = Config
ipinfohigherlimit.Config = Config
jira.Config = Config
newrelic.Config = Config
opsgenie.Config = Config