mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
added ipinfo mod
This commit is contained in:
parent
fe67bedc86
commit
5035933f15
81
ipinfo/widget.go
Normal file
81
ipinfo/widget.go
Normal file
@ -0,0 +1,81 @@
|
||||
package ipinfo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
result string
|
||||
}
|
||||
|
||||
type ipinfo struct {
|
||||
Ip string `json:"ip"`
|
||||
Hostname string `json:"hostname"`
|
||||
City string `json:"city"`
|
||||
Region string `json:"region"`
|
||||
Country string `json:"country"`
|
||||
Coordinates string `json:"loc"`
|
||||
PostalCode string `json:"postal"`
|
||||
Organization string `json:"org"`
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget("Ipinfo", "ipinfo", false),
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.ipinfo()
|
||||
widget.View.Clear()
|
||||
widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name))
|
||||
|
||||
fmt.Fprintf(widget.View, "%s", 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", "https://ipinfo.io/", 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.result = fmt.Sprintf("[red]IP Address:[white] %s\n[red]Hostname:[white] %v\n[red]City:[white] %s\n[red]Region:[white] %s\n[red]Country:[white] %s\n[red]Coordinates:[white] %v\n[red]Postal Code:[white] %s\n[red]Organization:[white] %v",
|
||||
info.Ip, info.Hostname, info.City, info.Region, info.Country, info.Coordinates, info.PostalCode, info.Organization)
|
||||
|
||||
}
|
3
wtf.go
3
wtf.go
@ -27,6 +27,7 @@ import (
|
||||
"github.com/senorprogrammer/wtf/todo"
|
||||
"github.com/senorprogrammer/wtf/weather"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
"github.com/senorprogrammer/wtf/ipinfo"
|
||||
)
|
||||
|
||||
/* -------------------- Functions -------------------- */
|
||||
@ -157,6 +158,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
|
||||
gcal.Config = Config
|
||||
git.Config = Config
|
||||
github.Config = Config
|
||||
ipinfo.Config = Config
|
||||
jira.Config = Config
|
||||
newrelic.Config = Config
|
||||
opsgenie.Config = Config
|
||||
@ -176,6 +178,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
|
||||
gcal.NewWidget(),
|
||||
git.NewWidget(app, pages),
|
||||
github.NewWidget(app, pages),
|
||||
ipinfo.NewWidget(),
|
||||
jira.NewWidget(),
|
||||
newrelic.NewWidget(),
|
||||
opsgenie.NewWidget(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user