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

Migrate all modules to their own subfolder

Handles #375
This commit is contained in:
Sean Smith
2019-02-18 11:16:34 -05:00
parent c28c31aedb
commit 8030380f89
123 changed files with 51 additions and 51 deletions

View File

@@ -0,0 +1,66 @@
package prettyweather
import (
"io/ioutil"
"net/http"
"strings"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
result string
unit string
city string
view string
language string
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Pretty Weather", "prettyweather", false),
}
return &widget
}
func (widget *Widget) Refresh() {
widget.prettyWeather()
widget.View.SetText(widget.result)
}
//this method reads the config and calls wttr.in for pretty weather
func (widget *Widget) prettyWeather() {
client := &http.Client{}
widget.unit = wtf.Config.UString("wtf.mods.prettyweather.unit", "m")
widget.city = wtf.Config.UString("wtf.mods.prettyweather.city", "")
widget.view = wtf.Config.UString("wtf.mods.prettyweather.view", "0")
widget.language = wtf.Config.UString("wtf.mods.prettyweather.language", "en")
req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, nil)
if err != nil {
widget.result = err.Error()
return
}
req.Header.Set("Accept-Language", widget.language)
req.Header.Set("User-Agent", "curl")
response, err := client.Do(req)
if err != nil {
widget.result = err.Error()
return
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
widget.result = err.Error()
return
}
//widget.result = strings.TrimSpace(string(contents))
widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents)))
}

View File

@@ -0,0 +1,77 @@
package weather
import (
"fmt"
"strings"
owm "github.com/briandowns/openweathermap"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
if widget.apiKeyValid() == false {
widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set")
return
}
cityData := widget.currentData()
if cityData == nil {
widget.View.SetText(" Weather data is unavailable: no city data")
return
}
if len(cityData.Weather) == 0 {
widget.View.SetText(" Weather data is unavailable: no weather data")
return
}
widget.View.SetTitle(widget.title(cityData))
content := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
content = content + widget.description(cityData) + "\n\n"
content = content + widget.temperatures(cityData) + "\n"
content = content + widget.sunInfo(cityData)
widget.View.SetText(content)
}
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 := wtf.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",
wtf.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 {
str := fmt.Sprintf("%s %s", widget.emojiFor(cityData), cityData.Name)
return widget.ContextualTitle(str)
}

View File

@@ -0,0 +1,49 @@
package weather
import (
owm "github.com/briandowns/openweathermap"
)
var weatherEmoji = map[string]string{
"default": "💥",
"broken clouds": "🌤",
"clear": " ",
"clear sky": " ",
"cloudy": "⛅️",
"few clouds": "🌤",
"fog": "🌫",
"haze": "🌫",
"heavy intensity rain": "💦",
"heavy rain": "💦",
"heavy snow": "⛄️",
"light intensity drizzle": "🌧",
"light intensity shower rain": "☔️",
"light rain": "🌦",
"light shower snow": "🌦⛄️",
"light snow": "🌨",
"mist": "🌬",
"moderate rain": "🌧",
"moderate snow": "🌨",
"overcast": "🌥",
"overcast clouds": "🌥",
"partly cloudy": "🌤",
"scattered clouds": "🌤",
"shower rain": "☔️",
"smoke": "🔥",
"snow": "❄️",
"sunny": "☀️",
"thunderstorm": "⛈",
}
func (widget *Widget) emojiFor(data *owm.CurrentWeatherData) string {
if len(data.Weather) == 0 {
return ""
}
emoji := weatherEmoji[data.Weather[0].Description]
if emoji == "" {
emoji = weatherEmoji["default"]
}
return emoji
}

View File

@@ -0,0 +1,188 @@
package weather
import (
"os"
owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
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.HelpfulWidget
wtf.TextWidget
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 {
configKey := "weather"
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget(app, "Weather", configKey, true),
Idx: 0,
}
widget.loadAPICredentials()
widget.HelpfulWidget.SetView(widget.View)
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(wtf.Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
}
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(
wtf.Config.UString("wtf.mods.weather.tempUnit", "C"),
wtf.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
}
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
}
}
// loadAPICredentials loads the API authentication credentials for this module
// First checks to see if they're in the config file. If not, checks the ENV var
func (widget *Widget) loadAPICredentials() {
widget.APIKey = wtf.Config.UString(
"wtf.mods.weather.apiKey",
os.Getenv("WTF_OWM_API_KEY"),
)
}