-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (145 loc) · 4.18 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"crypto/sha256"
"flag"
"fmt"
"hash"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"gopkg.in/gomail.v2"
)
var (
urlFlag = flag.String("url", "https://www.daysofwonder.com/memoir44/en/mini-campaigns/remembrance", "url to download")
daemonFlag = flag.Bool("deamon", true, "should this run as a daemon")
sleepFlag = flag.Int("sleep", 480, "duration to this sleep in daemon mode")
toFlag = flag.String("to", os.Getenv("MEM44_TO_EMAIL"), "comma separated list of email addresses to send to")
sendMailFlag = flag.Bool("send", true, "should mail be sent")
smtpAcctFlag = flag.String("from", os.Getenv("MEM44_SMTP_ACCOUNT"), "smtp account holder")
smtpPassFlag = flag.String("password", os.Getenv("MEM44_SMTP_PASSWORD"), "smtp password")
smtpHostFlag = flag.String("host", "smtp.gmail.com", "smtp host")
smtpPortFlag = flag.Int("port", 587, "smtp port")
hashFileFlag = flag.String("hash", os.Getenv("HOME")+"/.mem44hash", "where to store the last hash")
)
const fullDateFormat = "Jan 2 2006, 15:04:05"
var (
lastDownloadTime = time.Now()
lastDownloadHash string
thisDownloadHash = sha256.New()
)
func subject() string {
return fmt.Sprintf("Memoir '44 Download from (%s)", time.Now().Format("Jan 2, 2006"))
}
func body() string {
return fmt.Sprintf(
`
<html>
<h1>Hello</h1>
<br>
<p>I am your friendly neighborhood Memoir '44 downloader. I download the Rememberance map on the
<a href=%q>Days of Wonder</a> website. I thought you wanted this month's map; therefore, I've
included it.</p>
<p>Here are some stats about me. I hope you find these interesting.</p>
<table>
<tr>
<th>lastDownloadTime:</th>
<th>%s</th>
</tr>
<tr>
<th>lastDownloadHash:</th>
<th>%s</th>
</tr>
<tr>
<th>thisDownloadHash:</th>
<th>%x</th>
</tr>
</table>
</html>
`,
*urlFlag,
lastDownloadTime.Format(fullDateFormat),
lastDownloadHash,
thisDownloadHash.Sum(nil),
)
}
func convertHash(h hash.Hash) string {
return fmt.Sprintf("%x", h.Sum(nil))
}
func main() {
flag.Parse()
if len(*urlFlag) == 0 {
log.Fatal("no -url specified")
}
emails := strings.Split(*toFlag, ",")
sleepDur := time.Duration(*sleepFlag) * time.Minute
lastHash, _ := ioutil.ReadFile(*hashFileFlag)
lastDownloadHash = string(lastHash)
for {
func() {
doc, err := goquery.NewDocument(*urlFlag)
if err != nil {
log.Fatalf("error getting document: %v\nerr: %v", *urlFlag, err)
}
log.Printf("connected to DoW")
var pdfURL string
doc.Find("a[href]").Each(func(index int, item *goquery.Selection) {
if href, _ := item.Attr("href"); strings.HasSuffix(href, ".pdf") {
pdfURL = href
}
})
if len(pdfURL) == 0 {
log.Fatalf("no pdf url at %v", *urlFlag)
}
log.Printf("downloading PDF")
resp, err := http.Get(pdfURL)
if err != nil {
log.Fatalf("error downloading: %v\nerr: %v", pdfURL, err)
}
defer resp.Body.Close()
tmp, err := ioutil.TempFile("/tmp", "mem44")
if err != nil {
log.Fatalf("error opening temp file: %v", err)
}
defer os.Remove(tmp.Name())
thisDownloadHash.Reset()
teedBody := io.TeeReader(resp.Body, thisDownloadHash)
sz, err := io.Copy(tmp, teedBody)
if err != nil {
log.Fatalf("error saving temp file: %v", err)
}
log.Printf("size: %d", sz)
if convertHash(thisDownloadHash) == lastDownloadHash {
log.Printf("hashes are equal, skipping download")
return
}
mesg := gomail.NewMessage()
mesg.SetHeader("From", *smtpAcctFlag)
mesg.SetHeader("To", emails...)
mesg.SetHeader("Subject", subject())
mesg.SetBody("text/html", body())
mesg.Attach(tmp.Name())
d := gomail.NewDialer(*smtpHostFlag, *smtpPortFlag, *smtpAcctFlag, *smtpPassFlag)
if *sendMailFlag {
if err := d.DialAndSend(mesg); err != nil {
log.Fatalf("error sending mail: %v", err)
}
log.Printf("mail sent")
} else {
log.Printf("mail skipped")
}
}()
if !*daemonFlag {
break
}
lastDownloadHash = convertHash(thisDownloadHash)
ioutil.WriteFile(*hashFileFlag, []byte(lastDownloadHash), 0600)
lastDownloadTime = time.Now()
log.Printf("sleeping till: %s", time.Now().Add(sleepDur).Format(fullDateFormat))
time.Sleep(sleepDur)
}
}