-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (114 loc) · 3.44 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"context"
"flag"
"fmt"
"log"
"os"
"os/user"
"github.com/asgaines/pupsniffer/pupservice"
"github.com/asgaines/pupsniffer/pupservice/fetcher"
"github.com/asgaines/pupsniffer/utils"
)
func init() {
flag.Usage = func() {
fmt.Println(`
____ _ ________
/ __ \__ ______ _________ (_) __/ __/__ ____
/ /_/ / / / / __ \/ ___/ __ \/ / /_/ /_/ _ \/ ___/
/ ____/ /_/ / /_/ (__ ) / / / / __/ __/ __/ /
/_/ \__,_/ .___/____/_/ /_/_/_/ /_/ /\___/_/
/_/
__ _ ______ __
___ ___ / /_ ___ ___ (_/ _/ _/__ ___/ /
_ _ _ / _ \/ -_/ __/ (_-</ _ \/ / _/ _/ -_/ _ /
(_|_|_) \_, /\__/\__/ /___/_//_/_/_//_/ \__/\_,_/
/___/
`)
fmt.Println("Welcome to pupsniffer, a way to know about new pups at the Boulder Humane Society!")
fmt.Println()
fmt.Fprintln(flag.CommandLine.Output(), "Usage of pupsniffer:")
flag.PrintDefaults()
}
}
func main() {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
var kennelPath string
var filterPups bool
var newOnly bool
var email bool
flag.StringVar(&kennelPath, "kennel", fmt.Sprintf("%s/.config/pupsniffer/kennel", usr.HomeDir), "Path to kennel (where previous searches are stored for comparison)")
flag.BoolVar(&filterPups, "filter", true, "Should pups be filtered?")
flag.BoolVar(&newOnly, "newonly", true, "Only show pups not before seen?")
flag.BoolVar(&email, "email", false, "Send report to email")
flag.Parse()
log.Printf("filtering: %v, new only: %v, email: %v", filterPups, newOnly, email)
if err := os.MkdirAll(kennelPath, os.ModePerm); err != nil {
log.Fatal(err)
}
ctx := context.Background()
pupsvc := pupservice.New(
fetcher.NewFetcher(),
kennelPath,
"./static",
)
pupIDs, err := pupsvc.FetchPupIDs(ctx)
if err != nil {
log.Fatal(err)
}
numTotalPups := len(pupIDs)
if numTotalPups == 0 {
log.Println("There aren't any pups at the Boulder Humane Society right now...")
os.Exit(0)
}
log.Printf("%d %s at the Boulder Humane Society right now\n", len(pupIDs), utils.Pluralize("pup", "pups", len(pupIDs)))
if newOnly {
newPupIDs, err := pupsvc.SniffOutNew(pupIDs)
if err != nil {
log.Fatal(err)
}
if len(newPupIDs) == 0 {
log.Println("Looks like you've already seen all the pups currently at the center. Check back soon!")
os.Exit(0)
}
if err := pupsvc.KennelPups(pupIDs); err != nil {
log.Fatal(err)
}
pupIDs = newPupIDs
} else {
if err := pupsvc.KennelPups(pupIDs); err != nil {
log.Fatal(err)
}
}
pups, err := pupsvc.FetchPups(ctx, pupIDs)
if err != nil {
log.Fatal(err)
}
if filterPups {
filteredPups, err := pupsvc.FilterPups(pups)
if err != nil {
log.Fatal(err)
}
log.Printf("%d/%d new %s %s your criteria!\n", len(filteredPups), len(pups), utils.Pluralize("pup", "pups", len(filteredPups)), utils.Pluralize("meets", "meet", len(filteredPups)))
pups = filteredPups
}
buf := &bytes.Buffer{}
if err := pupsvc.PupReport(numTotalPups, pups, buf); err != nil {
log.Fatal(err)
}
if len(pups) > 0 && email {
recipients := []string{
}
if err := pupsvc.Mailman(buf, recipients); err != nil {
log.Fatal(err)
}
}
}