Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

departures by airport + [:GHPAGES:] #8

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ docs: ## Generates html documents
#=================================================

.PHONY: install.tools
install.tools: .install.pre-commit .install.codespell .install.golangci-lint ## Install needed tools
install.tools: .install.pre-commit .install.codespell .install.golangci-lint .install.sphinx-build## Install needed tools

.PHONY: .install.codespell
.install.codespell:
Expand All @@ -54,6 +54,10 @@ install.tools: .install.pre-commit .install.codespell .install.golangci-lint ##
.install.golangci-lint:
VERSION=1.51.1 ./hack/install_golangci.sh

.PHONY: .install.sphinx-build
.install.sphinx-build:
sudo ${PKG_MANAGER} -y install python-sphinx python-sphinx_rtd_theme

#=================================================
# Linting/Formatting/Code Validation targets
#=================================================
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
depAirport = *flightData.EstDepartureAirport
}

fmt.Printf("ICAO24: %s, Departure Airport: %4s, LastSeen: %s\n",
fmt.Printf("ICAO24: %s, Departed Airport: %4s, LastSeen: %s\n",
flightData.Icao24,
depAirport,
time.Unix(flightData.LastSeen, 0),
Expand All @@ -79,18 +79,18 @@ func main() {
output:

```
ICAO24: 406544, Departure Airport: EGPH, LastSeen: 2023-10-10 07:33:07 +1100 AEDT
ICAO24: 896180, Departure Airport: , LastSeen: 2023-10-10 05:07:35 +1100 AEDT
ICAO24: 738065, Departure Airport: LLBG, LastSeen: 2023-10-10 03:14:58 +1100 AEDT
ICAO24: 4bc848, Departure Airport: LTFJ, LastSeen: 2023-10-10 01:31:15 +1100 AEDT
ICAO24: 4891b6, Departure Airport: , LastSeen: 2023-10-09 20:52:38 +1100 AEDT
ICAO24: 39856a, Departure Airport: LFBO, LastSeen: 2023-10-09 20:45:12 +1100 AEDT
ICAO24: 4ba9c9, Departure Airport: LTFM, LastSeen: 2023-10-09 18:52:45 +1100 AEDT
ICAO24: 738075, Departure Airport: LFPG, LastSeen: 2023-10-09 16:03:10 +1100 AEDT
ICAO24: 39e68b, Departure Airport: ESSA, LastSeen: 2023-10-09 07:23:04 +1100 AEDT
ICAO24: 01020a, Departure Airport: , LastSeen: 2023-10-09 05:46:24 +1100 AEDT
ICAO24: 39e698, Departure Airport: LOWW, LastSeen: 2023-10-09 04:51:45 +1100 AEDT
ICAO24: 398569, Departure Airport: LJLJ, LastSeen: 2023-10-09 02:03:00 +1100 AEDT
ICAO24: 406544, Departed Airport: EGPH, LastSeen: 2023-10-10 07:33:07 +1100 AEDT
ICAO24: 896180, Departed Airport: , LastSeen: 2023-10-10 05:07:35 +1100 AEDT
ICAO24: 738065, Departed Airport: LLBG, LastSeen: 2023-10-10 03:14:58 +1100 AEDT
ICAO24: 4bc848, Departed Airport: LTFJ, LastSeen: 2023-10-10 01:31:15 +1100 AEDT
ICAO24: 4891b6, Departed Airport: , LastSeen: 2023-10-09 20:52:38 +1100 AEDT
ICAO24: 39856a, Departed Airport: LFBO, LastSeen: 2023-10-09 20:45:12 +1100 AEDT
ICAO24: 4ba9c9, Departed Airport: LTFM, LastSeen: 2023-10-09 18:52:45 +1100 AEDT
ICAO24: 738075, Departed Airport: LFPG, LastSeen: 2023-10-09 16:03:10 +1100 AEDT
ICAO24: 39e68b, Departed Airport: ESSA, LastSeen: 2023-10-09 07:23:04 +1100 AEDT
ICAO24: 01020a, Departed Airport: , LastSeen: 2023-10-09 05:46:24 +1100 AEDT
ICAO24: 39e698, Departed Airport: LOWW, LastSeen: 2023-10-09 04:51:45 +1100 AEDT
ICAO24: 398569, Departed Airport: LJLJ, LastSeen: 2023-10-09 02:03:00 +1100 AEDT
```

## License
Expand Down
92 changes: 2 additions & 90 deletions cmd/gopensky/arrivals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"text/tabwriter"
"time"

"github.com/navidys/gopensky"
"github.com/rs/zerolog/log"
Expand All @@ -19,7 +15,7 @@ var arrivalsCommand = &cobra.Command{
Use: "arrivals",
Short: "Retrieve flights for a certain airport which arrived within a given time interval",
Run: runArrivals,
PreRunE: preArrivalsRun,
PreRunE: preRunFlightArrivalsDepartures,
}

func runArrivals(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -47,90 +43,6 @@ func runArrivals(cmd *cobra.Command, args []string) {

fmt.Printf("%s\n", jsonResult) //nolint:forbidigo
} else {
printArrivalsTable(flights)
printFlightTable(flights)
}
}

func printArrivalsTable(flights []gopensky.FlighData) { //nolint:funlen
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)

info := []string{
"", // just to print a new line
"EDA = EstDepartureAirport",
"EAA = EstArrivalAirport",
"EDAHD = EstDepartureAirportHorizDistance",
"EDAVD = EstDepartureAirportVertDistance",
"EAAHD = EstArrivalAirportHorizDistance",
"EAAVD = EstArrivalAirportVertDistance",
"DACC = DepartureAirportCandidatesCount",
"AACC = ArrivalAirportCandidatesCount",
"", // just to print a new line
}

if _, err := fmt.Fprintln(os.Stdout, strings.Join(info, "\n")); err != nil {
log.Error().Msgf("%v", err)
}

header := fmt.Sprintf("%s\t%s\t%s\t%8s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"Icao24", "EDA", "EAA", "Callsign", "EDAHD", "EDAVD",
"EAAHD", "EAAVD", "DACC", "AACC", "FirstSeen", "LastSeen")

if _, err := fmt.Fprintln(writer, header); err != nil {
log.Error().Msgf("%v", err)
}

for _, flightData := range flights {
firstSeen := time.Unix(flightData.FirstSeen, 0).UTC()
lastSeen := time.Unix(flightData.LastSeen, 0).UTC()
estDepartureAirport := ""
estArrivalAirport := ""
callsign := ""

if flightData.EstDepartureAirport != nil {
estDepartureAirport = *flightData.EstDepartureAirport
}

if flightData.EstArrivalAirport != nil {
estDepartureAirport = *flightData.EstArrivalAirport
}

if flightData.Callsign != nil {
estDepartureAirport = *flightData.Callsign
}

data := fmt.Sprintf("%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s",
flightData.Icao24,
estDepartureAirport,
estArrivalAirport,
callsign,
flightData.EstDepartureAirportVertDistance,
flightData.EstDepartureAirportVertDistance,
flightData.EstArrivalAirportHorizDistance,
flightData.EstArrivalAirportVertDistance,
flightData.DepartureAirportCandidatesCount,
flightData.ArrivalAirportCandidatesCount,
firstSeen,
lastSeen,
)

if _, err := fmt.Fprintln(writer, data); err != nil {
log.Error().Msgf("%v", err)
}
}

if err := writer.Flush(); err != nil {
log.Error().Msgf("failed to flush template: %v", err)
}
}

func preArrivalsRun(cmd *cobra.Command, args []string) error {
if strings.TrimSpace(cmdAirport) == "" {
return gopensky.ErrEmptyAirportName
}

if cmdBeginTime <= 0 || cmdEndTime <= 0 {
return gopensky.ErrInvalidUnixTime
}

return nil
}
48 changes: 48 additions & 0 deletions cmd/gopensky/departures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/navidys/gopensky"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

//nolint:gochecknoglobals
var departuresCommand = &cobra.Command{
Use: "departures",
Short: "Retrieve flights for a certain airport which departed within a given time interval",
Run: runDepartures,
PreRunE: preRunFlightArrivalsDepartures,
}

func runDepartures(cmd *cobra.Command, args []string) {
conn, err := gopensky.NewConnection(context.Background(), cmdUsername, cmdPassword)
if err != nil {
log.Error().Msgf("%v", err)

return
}

flights, err := gopensky.GetDeparturesByAirport(conn, cmdAirport, cmdBeginTime, cmdEndTime)
if err != nil {
log.Error().Msgf("%v", err)

return
}

if cmdPrintJSON {
jsonResult, err := json.MarshalIndent(flights, "", " ")
if err != nil {
log.Error().Msgf("%v", err)

return
}

fmt.Printf("%s\n", jsonResult) //nolint:forbidigo
} else {
printFlightTable(flights)
}
}
13 changes: 12 additions & 1 deletion cmd/gopensky/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func init() { //nolint:gochecknoinits
statesCommand.Flags().Float64SliceVar(&cmdStatesBoundingBox, "box", nil,
"query a certain area defined by a bounding box of WGS84 coordinates (lamin,lomin,lamax,lomax)")

// flights command
// flights arrivals command
arrivalsCommand.Flags().StringVarP(&cmdAirport, "airport", "a", cmdAirport,
"ICAO identifier for the airport")

Expand All @@ -112,6 +112,17 @@ func init() { //nolint:gochecknoinits
arrivalsCommand.Flags().Int64VarP(&cmdEndTime, "end", "e", cmdEndTime,
"end of time interval to retrieve flights for as Unix time (seconds since epoch)")

// flights departures command
departuresCommand.Flags().StringVarP(&cmdAirport, "airport", "a", cmdAirport,
"ICAO identifier for the airport")

departuresCommand.Flags().Int64VarP(&cmdBeginTime, "being", "b", cmdBeginTime,
"start of time interval to retrieve flights for as Unix time (seconds since epoch)")

departuresCommand.Flags().Int64VarP(&cmdEndTime, "end", "e", cmdEndTime,
"end of time interval to retrieve flights for as Unix time (seconds since epoch)")

rootCmd.AddCommand(statesCommand)
rootCmd.AddCommand(arrivalsCommand)
rootCmd.AddCommand(departuresCommand)
}
97 changes: 97 additions & 0 deletions cmd/gopensky/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"

"github.com/navidys/gopensky"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func preRunFlightArrivalsDepartures(cmd *cobra.Command, args []string) error {
if strings.TrimSpace(cmdAirport) == "" {
return gopensky.ErrEmptyAirportName
}

if cmdBeginTime <= 0 || cmdEndTime <= 0 {
return gopensky.ErrInvalidUnixTime
}

return nil
}

func printFlightTable(flights []gopensky.FlighData) { //nolint:funlen
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)

info := []string{
"", // just to print a new line
"EDA = EstDepartureAirport",
"EAA = EstArrivalAirport",
"EDAHD = EstDepartureAirportHorizDistance",
"EDAVD = EstDepartureAirportVertDistance",
"EAAHD = EstArrivalAirportHorizDistance",
"EAAVD = EstArrivalAirportVertDistance",
"DACC = DepartureAirportCandidatesCount",
"AACC = ArrivalAirportCandidatesCount",
"", // just to print a new line
}

if _, err := fmt.Fprintln(os.Stdout, strings.Join(info, "\n")); err != nil {
log.Error().Msgf("%v", err)
}

header := fmt.Sprintf("%s\t%s\t%s\t%8s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"Icao24", "EDA", "EAA", "Callsign", "EDAHD", "EDAVD",
"EAAHD", "EAAVD", "DACC", "AACC", "FirstSeen", "LastSeen")

if _, err := fmt.Fprintln(writer, header); err != nil {
log.Error().Msgf("%v", err)
}

for _, flightData := range flights {
firstSeen := time.Unix(flightData.FirstSeen, 0).UTC()
lastSeen := time.Unix(flightData.LastSeen, 0).UTC()
estDepartureAirport := ""
estArrivalAirport := ""
callsign := ""

if flightData.EstDepartureAirport != nil {
estDepartureAirport = *flightData.EstDepartureAirport
}

if flightData.EstArrivalAirport != nil {
estArrivalAirport = *flightData.EstArrivalAirport
}

if flightData.Callsign != nil {
estDepartureAirport = *flightData.Callsign
}

data := fmt.Sprintf("%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s",
flightData.Icao24,
estDepartureAirport,
estArrivalAirport,
callsign,
flightData.EstDepartureAirportVertDistance,
flightData.EstDepartureAirportVertDistance,
flightData.EstArrivalAirportHorizDistance,
flightData.EstArrivalAirportVertDistance,
flightData.DepartureAirportCandidatesCount,
flightData.ArrivalAirportCandidatesCount,
firstSeen,
lastSeen,
)

if _, err := fmt.Fprintln(writer, data); err != nil {
log.Error().Msgf("%v", err)
}
}

if err := writer.Flush(); err != nil {
log.Error().Msgf("failed to flush template: %v", err)
}
}
Loading
Loading