-
Notifications
You must be signed in to change notification settings - Fork 1
/
weekend.go
120 lines (110 loc) · 3.14 KB
/
weekend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"flag"
"fmt"
"log"
"sort"
"time"
. "github.com/logrusorgru/aurora"
"github.com/mkfsn/flyjapan"
"github.com/mkfsn/flyjapan/airlines"
"github.com/mkfsn/flyjapan/airlines/factory"
)
type config struct {
airline string
}
func main() {
var cfg config
flag.StringVar(&cfg.airline, "airline", string(airlines.AirlinePeach), "airline to query")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
args = []string{"FUK", "HND", "KIX"}
}
airline, err := factory.CreateAirline(airlines.AirlineName((cfg.airline)))
if err != nil {
log.Fatalf("err: %v\n", err)
}
for _, city := range args {
queries := buildQueries(airline, city)
fetch(queries)
}
}
func fetch(queries []*flyjapan.Query) {
worker := flyjapan.NewWorker(queries)
resCh, errCh := worker.Work(context.Background())
for resCh != nil && errCh != nil {
select {
case res, ok := <-resCh:
if !ok {
resCh = nil
break
}
handleResult(res)
case err, ok := <-errCh:
if !ok {
errCh = nil
break
}
log.Printf("error: %v", err)
}
}
}
func buildQueries(airline airlines.Airline, city string) []*flyjapan.Query {
// Query(From(date), To(date), Airport(), Airport(), SortBy(fn), Airline(), Airline(), RepeatWeeks(n), FilterBy(fn))
today := time.Now().Truncate(time.Hour * 24)
friday := today.Add(time.Duration(12-int(today.Weekday())%7) * 24 * time.Hour)
queries := make([]*flyjapan.Query, 0)
for i := 0; i < 28; i, friday = i+1, friday.Add(time.Hour*24*7) {
saturday, sunday, monday := friday.Add(time.Hour*24), friday.Add(time.Hour*24*2), friday.Add(time.Hour*24*3)
q := flyjapan.NewQuery(
flyjapan.Airline(airline),
flyjapan.DestinationAirport(city),
flyjapan.SourceAirport("TPE"),
flyjapan.DateFromTo(friday, sunday),
flyjapan.DateFromTo(friday, monday),
flyjapan.DateFromTo(saturday, monday),
)
queries = append(queries, q)
}
return queries
}
func handleResult(res *flyjapan.Result) {
inBound := res.Inbound.FilterBy(airlines.AvailableFlight)
outBound := res.Outbound.FilterBy(airlines.AvailableFlight)
sort.Sort(airlines.SortByBaseFare(inBound))
sort.Sort(airlines.SortByBaseFare(outBound))
for i := 0; i < len(inBound) && i < 3; i++ {
for j := 0; j < len(outBound) && j < 3; j++ {
x, y := inBound[i], outBound[j]
a, b := x.Cheapest(), y.Cheapest()
if a+b > 4500 {
continue
}
dateFormat := "2006/01/02 15:04:05"
fmt.Printf("%s%v%s%s%v%5v",
BgBlue(Black(fmt.Sprintf("[%s]", x.Origin.Code))),
BgMagenta(Black(x.DepartureTime.Format(dateFormat))),
BgGray(Black("->")),
BgBlue(Black(fmt.Sprintf("[%s]", x.Destination.Code))),
BgMagenta(Black(x.ArrivalTime.Format(dateFormat))),
BgBrown(Black(a)),
)
fmt.Printf(" + ")
fmt.Printf("%s%v%s%s%v%5v",
BgBlue(Black(fmt.Sprintf("[%s]", y.Origin.Code))),
BgMagenta(Black(y.DepartureTime.Format(dateFormat))),
BgGray(Black("->")),
BgBlue(Black(fmt.Sprintf("[%s]", y.Destination.Code))),
BgMagenta(Black(y.ArrivalTime.Format(dateFormat))),
BgBrown(Black(b)),
)
if a+b < 3000 {
fmt.Printf(" = %5v\n", BgGreen(Red(a+b)))
} else {
fmt.Printf(" = %5v\n", BgGreen(Black(a+b)))
}
}
}
}