added bare setup for notifier

Got 582 offers. Filtered offers: 3
           ID|     Ram|             HDD|                           CPU|    Price|   Score|  Reduce time|Specials
  SB57-931121|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V2 (12518)|  57.00 €|  103.12|      40h 16m|ECC, Ent. HDD, iNIC
  SB69-927780|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V3 (13335)|  69.00 €|   89.92|      36h 02m|ECC, Ent. HDD, iNIC
  SB74-910394|  128 GB|  3x 2 TB (6144)|  Intel Xeon E5-1650V2 (12518)|  74.00 €|   84.96|      23h 00m|ECC, Ent. HDD, iNIC
This commit is contained in:
Mahmoud Rahbar Azad 2018-10-23 13:32:15 +02:00
parent 66001f861f
commit 796da12a07
No known key found for this signature in database
GPG Key ID: 7DBBD39E2BFEB784
2 changed files with 116 additions and 0 deletions

22
main.go
View File

@ -6,6 +6,7 @@ import (
"github.com/mrahbar/my-bloody-hetzner-sb-notifier/client"
c "github.com/mrahbar/my-bloody-hetzner-sb-notifier/crawler"
"github.com/mrahbar/my-bloody-hetzner-sb-notifier/hetzner"
n "github.com/mrahbar/my-bloody-hetzner-sb-notifier/notifier"
)
var (
@ -64,6 +65,24 @@ var (
"set max benchmark",
)
notifierSender = flag.String(
"notifier-sender",
"",
"set notifier sender",
)
notifierPassword = flag.String(
"notifier-password",
"",
"set notifier password",
)
notifierRecipient = flag.String(
"notifier-recipient",
"",
"set notifier recipient",
)
alertOnScore = flag.Int(
"alert-on-score",
0,
@ -86,6 +105,9 @@ func main() {
fmt.Printf("Got %d offers. Filtered offers: %d\n", len(offers.Server), len(servers))
crawler.Print(servers)
notifier := n.NewNotifier(*notifierRecipient, *notifierSender, *notifierPassword)
notifier.Act(servers)
} else {
fmt.Println("Got no offers.")
}

View File

@ -1 +1,95 @@
package notifier
import (
"bytes"
"fmt"
"github.com/mrahbar/my-bloody-hetzner-sb-notifier/hetzner"
"mime/quotedprintable"
"net/smtp"
"strings"
)
const (
gmailSmtpServer = "smtp.gmail.com"
contentTypeHtml = "text/html"
contentTypePlain = "text/plain"
)
type Notifier struct {
to string
contentType string
user string
password string
server string
port int
history map[string]float64
}
func NewNotifier(Recipient, Username, Password string) Notifier {
notifier := Notifier{
Recipient,
contentTypePlain,
Username,
Password,
gmailSmtpServer,
587,
make(map[string]float64),
}
return notifier
}
func (n Notifier) Act(servers []hetzner.Server) {
//absentFromHistory := false
//
//for _, s := range servers {
// // TODO check if server in history, if a single server is missing send email and store state
//}
}
func (n Notifier) sendMail(Dest []string, Subject, bodyMessage string) {
msg := "From: " + n.user + "\n" +
"To: " + strings.Join(Dest, ",") + "\n" +
"Subject: " + Subject + "\n" + bodyMessage
err := smtp.SendMail(fmt.Sprintf("%s:%d", n.server, n.port),
smtp.PlainAuth("", n.user, n.password, n.server),
n.user, Dest, []byte(msg))
if err != nil {
fmt.Printf("smtp error: %s", err)
return
}
fmt.Println("Mail sent successfully!")
}
func (n Notifier) writeEmail(subject, bodyMessage string) string {
header := make(map[string]string)
header["From"] = n.user
header["To"] = n.to
header["Subject"] = subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = fmt.Sprintf("%s; charset=\"utf-8\"", n.contentType)
header["Content-Transfer-Encoding"] = "quoted-printable"
header["Content-Disposition"] = "inline"
message := ""
for key, value := range header {
message += fmt.Sprintf("%s: %s\r\n", key, value)
}
var encodedMessage bytes.Buffer
finalMessage := quotedprintable.NewWriter(&encodedMessage)
finalMessage.Write([]byte(bodyMessage))
finalMessage.Close()
message += "\r\n" + encodedMessage.String()
return message
}