-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (100 loc) · 2.5 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
package main
import (
"bytes"
"fmt"
"html/template"
"net/smtp"
"strings"
"github.com/gocolly/colly"
)
var auth smtp.Auth
type request struct {
from string
to []string
subject string
body string
}
type htmlData struct {
Link string
Img string
}
type templateData struct {
Data []htmlData
}
func main() {
m := templateData{}
c := colly.NewCollector(
colly.AllowedDomains("www.amctheatres.com"),
)
c.OnHTML("div[class=slick-list]", func(e *colly.HTMLElement) {
links := e.ChildAttrs("a", "href")
images := e.ChildAttrs("img", "src")
uniqueImages := unique(images)
uniqueLinks := unique(links)
showtimesRemoved := []string{}
for _, link := range uniqueLinks {
if !strings.Contains(link, "showtimes") {
link = fmt.Sprintf("https://www.amctheatres.com%s", link)
showtimesRemoved = append(showtimesRemoved, link)
}
}
out := []htmlData{}
for i := range uniqueImages {
t := htmlData{
Link: showtimesRemoved[i],
Img: uniqueImages[i],
}
out = append(out, t)
}
m.Data = out
})
c.Visit("https://www.amctheatres.com/movie-theatres/phoenix/amc-ahwatukee-24")
auth = smtp.PlainAuth("", "etokatlian@gmail.com", "", "smtp.gmail.com")
r := newRequest([]string{"etokatlian@gmail.com", "tsmith93036@gmail.com"}, "This weeks movie briefing", "Hello, World!")
err := r.ParseTemplate("template.html", m)
if err := r.ParseTemplate("template.html", m); err == nil {
ok, _ := r.SendEmail()
fmt.Println(ok)
}
fmt.Println(err)
}
func newRequest(to []string, subject, body string) *request {
return &request{
to: to,
subject: subject,
body: body,
}
}
func (r *request) SendEmail() (bool, error) {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := "Subject: " + r.subject + "!\n"
msg := []byte(subject + mime + "\n" + r.body)
addr := "smtp.gmail.com:587"
if err := smtp.SendMail(addr, auth, "etokatlian@gmail.com", r.to, msg); err != nil {
return false, err
}
return true, nil
}
func (r *request) ParseTemplate(templateFileName string, data interface{}) error {
t, err := template.ParseFiles(templateFileName)
if err != nil {
return err
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return err
}
r.body = buf.String()
return nil
}
func unique(strSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range strSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}