mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Move IP address modules and weather modules into own subdirectories
This commit is contained in:
64
weatherservices/prettyweather/widget.go
Normal file
64
weatherservices/prettyweather/widget.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package prettyweather
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
result string
|
||||
unit string
|
||||
city string
|
||||
view string
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" Pretty Weather ", "prettyweather", false),
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.prettyWeather()
|
||||
|
||||
widget.View.SetText(fmt.Sprintf("%s", widget.result))
|
||||
}
|
||||
|
||||
//this method reads the config and calls wttr.in for pretty weather
|
||||
func (widget *Widget) prettyWeather() {
|
||||
client := &http.Client{}
|
||||
widget.unit = Config.UString("wtf.mods.prettyweather.unit", "m")
|
||||
widget.city = Config.UString("wtf.mods.prettyweather.city", "")
|
||||
widget.view = Config.UString("wtf.mods.prettyweather.view", "0")
|
||||
req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, 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()
|
||||
contents, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
widget.result = fmt.Sprintf("%s", err.Error())
|
||||
return
|
||||
}
|
||||
widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents)))
|
||||
|
||||
}
|
||||
77
weatherservices/weather/display.go
Normal file
77
weatherservices/weather/display.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package weather
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
owm "github.com/briandowns/openweathermap"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.View.Clear()
|
||||
|
||||
if widget.apiKeyValid() == false {
|
||||
fmt.Fprintf(widget.View, "%s", " Environment variable WTF_OWM_API_KEY is not set")
|
||||
return
|
||||
}
|
||||
|
||||
cityData := widget.currentData()
|
||||
if cityData == nil {
|
||||
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (1)")
|
||||
return
|
||||
}
|
||||
|
||||
if len(cityData.Weather) == 0 {
|
||||
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (2)")
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.title(cityData))
|
||||
|
||||
str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
|
||||
str = str + widget.description(cityData) + "\n\n"
|
||||
str = str + widget.temperatures(cityData) + "\n"
|
||||
str = str + widget.sunInfo(cityData)
|
||||
|
||||
fmt.Fprintf(widget.View, "%s", str)
|
||||
}
|
||||
|
||||
func (widget *Widget) description(cityData *owm.CurrentWeatherData) string {
|
||||
descs := []string{}
|
||||
for _, weather := range cityData.Weather {
|
||||
descs = append(descs, fmt.Sprintf(" %s", weather.Description))
|
||||
}
|
||||
|
||||
return strings.Join(descs, ",")
|
||||
}
|
||||
|
||||
func (widget *Widget) sunInfo(cityData *owm.CurrentWeatherData) string {
|
||||
return fmt.Sprintf(
|
||||
" Rise: %s Set: %s",
|
||||
wtf.UnixTime(int64(cityData.Sys.Sunrise)).Format("15:04 MST"),
|
||||
wtf.UnixTime(int64(cityData.Sys.Sunset)).Format("15:04 MST"),
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string {
|
||||
tempUnit := Config.UString("wtf.mods.weather.tempUnit", "C")
|
||||
|
||||
str := fmt.Sprintf("%8s: %4.1fΒ° %s\n", "High", cityData.Main.TempMax, tempUnit)
|
||||
|
||||
str = str + fmt.Sprintf(
|
||||
"%8s: [%s]%4.1fΒ° %s[white]\n",
|
||||
"Current",
|
||||
Config.UString("wtf.mods.weather.colors.current", "green"),
|
||||
cityData.Main.Temp,
|
||||
tempUnit,
|
||||
)
|
||||
|
||||
str = str + fmt.Sprintf("%8s: %4.1fΒ° %s\n", "Low", cityData.Main.TempMin, tempUnit)
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) title(cityData *owm.CurrentWeatherData) string {
|
||||
return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name)
|
||||
}
|
||||
263
weatherservices/weather/widget.go
Normal file
263
weatherservices/weather/widget.go
Normal file
@@ -0,0 +1,263 @@
|
||||
package weather
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
owm "github.com/briandowns/openweathermap"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/olebedev/config"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object.
|
||||
var Config *config.Config
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for Weather:
|
||||
|
||||
/: Show/hide this help window
|
||||
h: Previous weather location
|
||||
l: Next weather location
|
||||
|
||||
arrow left: Previous weather location
|
||||
arrow right: Next weather location
|
||||
`
|
||||
|
||||
// Widget is the container for weather data.
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
app *tview.Application
|
||||
pages *tview.Pages
|
||||
|
||||
APIKey string
|
||||
Data []*owm.CurrentWeatherData
|
||||
Idx int
|
||||
}
|
||||
|
||||
// NewWidget creates and returns a new instance of the weather Widget.
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(" Weather ", "weather", true),
|
||||
|
||||
app: app,
|
||||
pages: pages,
|
||||
|
||||
APIKey: os.Getenv("WTF_OWM_API_KEY"),
|
||||
Idx: 0,
|
||||
}
|
||||
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// Fetch retrieves OpenWeatherMap data from the OpenWeatherMap API.
|
||||
// It takes a list of OpenWeatherMap city IDs.
|
||||
// It returns a list of OpenWeatherMap CurrentWeatherData structs, one per valid city code.
|
||||
func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData {
|
||||
data := []*owm.CurrentWeatherData{}
|
||||
|
||||
for _, cityID := range cityIDs {
|
||||
result, err := widget.currentWeather(widget.APIKey, cityID)
|
||||
if err == nil {
|
||||
data = append(data, result)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Refresh fetches new data from the OpenWeatherMap API and loads the new data into the.
|
||||
// widget's view for rendering
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.apiKeyValid() {
|
||||
widget.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
|
||||
}
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.display()
|
||||
}
|
||||
|
||||
// Next displays data for the next city data in the list. If the current city is the last
|
||||
// city, it wraps to the first city.
|
||||
func (widget *Widget) Next() {
|
||||
widget.Idx = widget.Idx + 1
|
||||
if widget.Idx == len(widget.Data) {
|
||||
widget.Idx = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
// Prev displays data for the previous city in the list. If the previous city is the first
|
||||
// city, it wraps to the last city.
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Idx = widget.Idx - 1
|
||||
if widget.Idx < 0 {
|
||||
widget.Idx = len(widget.Data) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) apiKeyValid() bool {
|
||||
if widget.APIKey == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(widget.APIKey) != 32 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (widget *Widget) currentData() *owm.CurrentWeatherData {
|
||||
if len(widget.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.Data) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return widget.Data[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) {
|
||||
weather, err := owm.NewCurrent(Config.UString("wtf.mods.weather.tempUnit", "C"), Config.UString("wtf.mods.weather.language", "EN"), apiKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = weather.CurrentByID(cityCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return weather, nil
|
||||
}
|
||||
|
||||
func (widget *Widget) defaultCityCodes() []interface{} {
|
||||
defaultArr := []int{3370352}
|
||||
|
||||
var defaults = make([]interface{}, len(defaultArr))
|
||||
for i, d := range defaultArr {
|
||||
defaults[i] = d
|
||||
}
|
||||
|
||||
return defaults
|
||||
}
|
||||
|
||||
// icon returns an emoji for the current weather
|
||||
// src: https://github.com/chubin/wttr.in/blob/master/share/translations/en.txt
|
||||
// Note: these only work for English weather status. Sorry about that
|
||||
//
|
||||
// FIXME: Move these into a configuration file so they can be changed without a compile
|
||||
func (widget *Widget) icon(data *owm.CurrentWeatherData) string {
|
||||
var icon string
|
||||
|
||||
if len(data.Weather) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
switch data.Weather[0].Description {
|
||||
case "broken clouds":
|
||||
icon = "βοΈ"
|
||||
case "clear":
|
||||
icon = "βοΈ"
|
||||
case "clear sky":
|
||||
icon = "βοΈ"
|
||||
case "cloudy":
|
||||
icon = "β
οΈ"
|
||||
case "few clouds":
|
||||
icon = "π€"
|
||||
case "fog":
|
||||
icon = "π«"
|
||||
case "haze":
|
||||
icon = "π«"
|
||||
case "heavy intensity rain":
|
||||
icon = "π¦"
|
||||
case "heavy rain":
|
||||
icon = "π¦"
|
||||
case "heavy snow":
|
||||
icon = "βοΈ"
|
||||
case "light intensity shower rain":
|
||||
icon = "βοΈ"
|
||||
case "light rain":
|
||||
icon = "π¦"
|
||||
case "light shower snow":
|
||||
icon = "π¦βοΈ"
|
||||
case "light snow":
|
||||
icon = "π¨"
|
||||
case "mist":
|
||||
icon = "π¬"
|
||||
case "moderate rain":
|
||||
icon = "π§"
|
||||
case "moderate snow":
|
||||
icon = "π¨"
|
||||
case "overcast":
|
||||
icon = "π₯"
|
||||
case "overcast clouds":
|
||||
icon = "π₯"
|
||||
case "partly cloudy":
|
||||
icon = "π€"
|
||||
case "scattered clouds":
|
||||
icon = "βοΈ"
|
||||
case "shower rain":
|
||||
icon = "βοΈ"
|
||||
case "snow":
|
||||
icon = "βοΈ"
|
||||
case "sunny":
|
||||
icon = "βοΈ"
|
||||
case "thunderstorm":
|
||||
icon = "β"
|
||||
default:
|
||||
icon = "π₯"
|
||||
}
|
||||
|
||||
return icon
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.showHelp()
|
||||
return nil
|
||||
case "h":
|
||||
widget.Prev()
|
||||
return nil
|
||||
case "l":
|
||||
widget.Next()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyLeft:
|
||||
widget.Prev()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
widget.Next()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) showHelp() {
|
||||
closeFunc := func() {
|
||||
widget.pages.RemovePage("help")
|
||||
widget.app.SetFocus(widget.View)
|
||||
}
|
||||
|
||||
modal := wtf.NewBillboardModal(HelpText, closeFunc)
|
||||
|
||||
widget.pages.AddPage("help", modal, false, true)
|
||||
widget.app.SetFocus(modal)
|
||||
}
|
||||
Reference in New Issue
Block a user