From ce0151beccd31cbaf44ba25b37b027c9c54a246d Mon Sep 17 00:00:00 2001 From: Bjoern Weidlich Date: Wed, 1 Jan 2020 23:06:26 -0800 Subject: [PATCH] Added SpaceX Module --- app/widget_maker.go | 4 +++ modules/spacex/client.go | 50 +++++++++++++++++++++++++++ modules/spacex/settings.go | 22 ++++++++++++ modules/spacex/widget.go | 71 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 modules/spacex/client.go create mode 100644 modules/spacex/settings.go create mode 100644 modules/spacex/widget.go diff --git a/app/widget_maker.go b/app/widget_maker.go index f0c3d450..b9a3fcdc 100644 --- a/app/widget_maker.go +++ b/app/widget_maker.go @@ -47,6 +47,7 @@ import ( "github.com/wtfutil/wtf/modules/resourceusage" "github.com/wtfutil/wtf/modules/rollbar" "github.com/wtfutil/wtf/modules/security" + "github.com/wtfutil/wtf/modules/spacex" "github.com/wtfutil/wtf/modules/spotify" "github.com/wtfutil/wtf/modules/spotifyweb" "github.com/wtfutil/wtf/modules/status" @@ -227,6 +228,9 @@ func MakeWidget( case "security": settings := security.NewSettingsFromYAML(moduleName, moduleConfig, config) widget = security.NewWidget(app, settings) + case "spacex": + settings := spacex.NewSettingsFromYAML(moduleName, moduleConfig, config) + widget = spacex.NewWidget(app, settings) case "spotify": settings := spotify.NewSettingsFromYAML(moduleName, moduleConfig, config) widget = spotify.NewWidget(app, pages, settings) diff --git a/modules/spacex/client.go b/modules/spacex/client.go new file mode 100644 index 00000000..632906da --- /dev/null +++ b/modules/spacex/client.go @@ -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 +} diff --git a/modules/spacex/settings.go b/modules/spacex/settings.go new file mode 100644 index 00000000..53a46b50 --- /dev/null +++ b/modules/spacex/settings.go @@ -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 +} diff --git a/modules/spacex/widget.go b/modules/spacex/widget.go new file mode 100644 index 00000000..61e9367f --- /dev/null +++ b/modules/spacex/widget.go @@ -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 +}