mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			68 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package prettyweather
 | |
| 
 | |
| import (
 | |
| 	"io/ioutil"
 | |
| 	"net/http"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/rivo/tview"
 | |
| 	"github.com/wtfutil/wtf/view"
 | |
| 	"github.com/wtfutil/wtf/wtf"
 | |
| )
 | |
| 
 | |
| type Widget struct {
 | |
| 	view.TextWidget
 | |
| 
 | |
| 	result   string
 | |
| 	settings *Settings
 | |
| }
 | |
| 
 | |
| func NewWidget(app *tview.Application, settings *Settings) *Widget {
 | |
| 	widget := Widget{
 | |
| 		TextWidget: view.NewTextWidget(app, settings.common),
 | |
| 
 | |
| 		settings: settings,
 | |
| 	}
 | |
| 
 | |
| 	return &widget
 | |
| }
 | |
| 
 | |
| func (widget *Widget) Refresh() {
 | |
| 	widget.prettyWeather()
 | |
| 
 | |
| 	widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false })
 | |
| }
 | |
| 
 | |
| //this method reads the config and calls wttr.in for pretty weather
 | |
| func (widget *Widget) prettyWeather() {
 | |
| 	client := &http.Client{}
 | |
| 
 | |
| 	city := widget.settings.city
 | |
| 	unit := widget.settings.unit
 | |
| 	view := widget.settings.view
 | |
| 
 | |
| 	req, err := http.NewRequest("GET", "https://wttr.in/"+city+"?"+view+"?"+unit, nil)
 | |
| 	if err != nil {
 | |
| 		widget.result = err.Error()
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	req.Header.Set("Accept-Language", widget.settings.language)
 | |
| 	req.Header.Set("User-Agent", "curl")
 | |
| 	response, err := client.Do(req)
 | |
| 	if err != nil {
 | |
| 		widget.result = err.Error()
 | |
| 		return
 | |
| 
 | |
| 	}
 | |
| 	defer func() { _ = response.Body.Close() }()
 | |
| 
 | |
| 	contents, err := ioutil.ReadAll(response.Body)
 | |
| 	if err != nil {
 | |
| 		widget.result = err.Error()
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents)))
 | |
| }
 |