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

Merge branch 'spacex' of github.com:bjoernw/wtf into bjoernw-spacex

This commit is contained in:
Chris Cummer 2020-01-15 21:34:43 -08:00
commit 7ce90d92df
4 changed files with 147 additions and 0 deletions

View File

@ -47,6 +47,7 @@ import (
"github.com/wtfutil/wtf/modules/resourceusage" "github.com/wtfutil/wtf/modules/resourceusage"
"github.com/wtfutil/wtf/modules/rollbar" "github.com/wtfutil/wtf/modules/rollbar"
"github.com/wtfutil/wtf/modules/security" "github.com/wtfutil/wtf/modules/security"
"github.com/wtfutil/wtf/modules/spacex"
"github.com/wtfutil/wtf/modules/spotify" "github.com/wtfutil/wtf/modules/spotify"
"github.com/wtfutil/wtf/modules/spotifyweb" "github.com/wtfutil/wtf/modules/spotifyweb"
"github.com/wtfutil/wtf/modules/status" "github.com/wtfutil/wtf/modules/status"
@ -227,6 +228,9 @@ func MakeWidget(
case "security": case "security":
settings := security.NewSettingsFromYAML(moduleName, moduleConfig, config) settings := security.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = security.NewWidget(app, settings) widget = security.NewWidget(app, settings)
case "spacex":
settings := spacex.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = spacex.NewWidget(app, settings)
case "spotify": case "spotify":
settings := spotify.NewSettingsFromYAML(moduleName, moduleConfig, config) settings := spotify.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = spotify.NewWidget(app, pages, settings) widget = spotify.NewWidget(app, pages, settings)

50
modules/spacex/client.go Normal file
View File

@ -0,0 +1,50 @@
package spacex
import (
"github.com/wtfutil/wtf/utils"
"net/http"
)
const (
spacexLaunchAPI = "https://api.spacexdata.com/v3/launches/next"
)
type Launch struct {
FlightNumber int `json:"flight_number"`
MissionName string `json:"mission_name"`
LaunchDate int64 `json:"launch_date_unix"`
IsTentative bool `json:"tentative"`
Rocket Rocket `json:"rocket"`
LaunchSite LaunchSite `json:"launch_site"`
Links Links `json:"links"`
Details string `json:"details"`
}
type LaunchSite struct {
Name string `json:"site_name_long"`
}
type Rocket struct {
Name string `json:"rocket_name"`
}
type Links struct {
RedditLink string `json:"reddit_campaign"`
YouTubeLink string `json:"video_link"`
}
func NextLaunch() (*Launch, error) {
resp, err := http.Get(spacexLaunchAPI)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var data Launch
err = utils.ParseJSON(&data, resp.Body)
if err != nil {
return nil, err
}
return &data, nil
}

View File

@ -0,0 +1,22 @@
package spacex
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const (
defaultFocusable = false
)
type Settings struct {
common *cfg.Common
}
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
spacex := ymlConfig.UString("spacex")
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, spacex, defaultFocusable, ymlConfig, globalConfig),
}
return &settings
}

71
modules/spacex/widget.go Normal file
View File

@ -0,0 +1,71 @@
package spacex
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
"time"
)
type Widget struct {
view.TextWidget
settings *Settings
err error
launch Launch
}
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := &Widget{
TextWidget: view.NewTextWidget(app, settings.common),
settings: settings,
}
return widget
}
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.Redraw(widget.content)
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
var title = "Next SpaceX 🚀"
if widget.CommonSettings().Title != "" {
title = widget.CommonSettings().Title
}
launch, err := NextLaunch()
var str string
if err != nil {
handleError(widget, err)
} else {
str = fmt.Sprintf("[%s]Mission[white]\n", widget.settings.common.Colors.Subheading)
str += fmt.Sprintf("%s: %s\n", "Name", launch.MissionName)
str += fmt.Sprintf("%s: %s\n", "Date", wtf.UnixTime(launch.LaunchDate).Format(time.RFC822))
str += fmt.Sprintf("%s: %s\n", "Site", launch.LaunchSite.Name)
str += "\n"
str += fmt.Sprintf("[%s]Links[white]\n", widget.settings.common.Colors.Subheading)
str += fmt.Sprintf("%s: %s\n", "YouTube", launch.Links.YouTubeLink)
str += fmt.Sprintf("%s: %s\n", "Reddit", launch.Links.RedditLink)
if widget.CommonSettings().Height >= 2 {
str += "\n"
str += fmt.Sprintf("[%s]Details[white]\n", widget.settings.common.Colors.Subheading)
str += fmt.Sprintf("%s: %s\n", "RocketName", launch.Rocket.Name)
str += fmt.Sprintf("%s: %s\n", "Details", launch.Details)
}
}
return title, str, true
}
func handleError(widget *Widget, err error) {
widget.err = err
}