From 7c0a2a363a5cfca45a28e8613d7fc8b1cf3e4864 Mon Sep 17 00:00:00 2001 From: Nate Ijams Date: Sat, 28 Aug 2021 11:18:14 -0500 Subject: [PATCH] Add flights. --- engine/discord.go | 119 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 +- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/engine/discord.go b/engine/discord.go index 8a76961..beafd96 100644 --- a/engine/discord.go +++ b/engine/discord.go @@ -1,8 +1,10 @@ package engine import ( + "encoding/json" "log" "math/rand" + "net/http" "os" "os/signal" "strconv" @@ -132,6 +134,11 @@ func StartDiscord() { ctx.Reply("Your username is " + ctx.Msg.Author.Username) }).Desc("returns the user's username") + // TEMP SBK FLIGHT + router.On("fly", func(ctx *exrouter.Context) { + fly(ctx) + }).Desc("sbk flight info.") + // Create the help command router.Default = router.On("help", func(ctx *exrouter.Context) { var text = "" @@ -572,3 +579,115 @@ func setChannel(ctx *exrouter.Context) { } ctx.Reply("This channel is now the default channel for your discord guild!") } + +func fly(ctx *exrouter.Context) { + + type Airline struct { + Name string `json:"name"` + } + + type FlightInfo struct { + IATACode string `json:"iata"` + } + + type Destination struct { + Airport string `json:"airport"` + IATACode string `json:"iata"` + Estimated string `json:"estimated"` + } + + type LiveData struct { + IsGround bool `json:"is_ground"` + } + + type Flight struct { + Airline Airline `json:"airline"` + FlightInfo FlightInfo `json:"flight"` + Departure Destination `json:"departure"` + Arrival Destination `json:"arrival"` + Live LiveData `json:"live"` + Status string `json:"flight_status"` + } + + type Response struct { + Flights []Flight `json:"data"` + } + + httpClient := http.Client{} + + // Search parameters + + // limit : 1 : number of results we want : + // airline_name : "American Airlines" + // flight_number : "541" + + req, err := http.NewRequest("GET", "http://api.aviationstack.com/v1/flights", nil) + if err != nil { + panic(err) + } + + log.Println("req:", req) + + q := req.URL.Query() + q.Add("access_key", "088e740c6f4dde484159370de3840c15") + q.Add("limit", "1") + q.Add("flight_number", "541") + q.Add("airline_name", "American Airlines") + req.URL.RawQuery = q.Encode() + + log.Println("raw query:", req.URL.RawQuery) + log.Println("all req:", req) + + res, err := httpClient.Do(req) + if err != nil { + panic(err) + } + defer res.Body.Close() + + log.Println("res.Body:", res.Body) + + var apiResponse Response + json.NewDecoder(res.Body).Decode(&apiResponse) + log.Println("apiResponse:", apiResponse) + + var message string + + for _, flight := range apiResponse.Flights { + log.Println("Now:", time.Now()) + + log.Println("Estimated depart:", flight.Departure.Estimated) + + //loc, _ := time.LoadLocation("America/Chicago") + + thingy, _ := time.Parse(time.RFC3339, strings.Replace(flight.Departure.Estimated, `+00:00`, `-05:00`, -1)) + //thingy := flight.Departure.Estimated + log.Println("thingy:", thingy) + thingy2 := time.Until(thingy) + log.Println("thingy2:", thingy2) + thingy3 := thingy2.Minutes() + switch flight.Status { + case "scheduled": + message = "sbk's flight is scheduled. He should be departing in " + strconv.FormatFloat(thingy3, 'f', 0, 64) + " minutes. He is scheduled to arrive at " + flight.Arrival.Estimated + " Eastern Time." + case "active": + message = "sbk's flight is in the air! He should arrive around " + flight.Arrival.Estimated + " Eastern Time." + case "landed": + message = "sbk has landed! Yay!" + default: + message = "Uh oh. Something has gone with sbk's flight. Start praying." + } + } + + log.Println(message) + ctx.Reply(message) + /* + fmt.Println(fmt.Sprintf("%s flight %s from %s (%s) to %s (%s) is in the air. Status: %s", + flight.Airline.Name, + flight.FlightInfo.IATACode, + flight.Departure.Airport, + flight.Departure.IATACode, + flight.Arrival.Airport, + flight.Arrival.IATACode, + flight.Status)) + } + */ +} diff --git a/main.go b/main.go index 31363ca..712acc1 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,8 @@ import ( "os/signal" "syscall" - "git.sr.ht/~exprez135/go-hypixel/engine" - //"./engine" + //"git.sr.ht/~exprez135/go-hypixel/engine" + "./engine" ) var platformName = "discord" -- 2.45.2