trying to switch instrumentation

Got 544 offers. Filtered offers: 3
           ID|     Ram|             HDD|                           CPU|    Price|  Score|  Reduce time|Specials
  SB64-935022|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V2 (12518)|  64.00 €|  91.84|      48h 17m|ECC, Ent. HDD, iNIC
  SB72-927788|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V3 (13335)|  72.00 €|  86.17|      21h 37m|ECC, Ent. HDD, iNIC
  SB73-910394|  128 GB|  3x 2 TB (6144)|  Intel Xeon E5-1650V2 (12518)|  73.00 €|  86.13|      03h 33m|ECC, Ent. HDD, iNIC
This commit is contained in:
Mahmoud Rahbar Azad 2018-10-25 20:24:07 +02:00
parent 796da12a07
commit 0dfa241f0d
No known key found for this signature in database
GPG Key ID: 7DBBD39E2BFEB784
5 changed files with 24 additions and 98 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
go.sum

11
go.mod
View File

@ -1 +1,12 @@
module github.com/mrahbar/my-bloody-hetzner-sb-notifier
require (
cloud.google.com/go v0.31.0
github.com/golang/protobuf v1.2.0
github.com/googleapis/gax-go v2.0.0+incompatible // indirect
go.opencensus.io v0.18.0 // indirect
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519 // indirect
google.golang.org/api v0.0.0-20181021000519-a2651947f503 // indirect
google.golang.org/genproto v0.0.0-20181016170114-94acd270e44e
google.golang.org/grpc v1.16.0 // indirect
)

View File

@ -0,0 +1,11 @@
package instrumentation
type Instrumenter struct{
projectId string
}
func NewInstrumenter(projectId string) *Instrumenter {
return &Instrumenter{
projectId: projectId,
}
}

View File

@ -6,7 +6,6 @@ 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 (
@ -106,8 +105,7 @@ 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)
//notifier := n.NewInstrumenter(*notifierRecipient, *notifierSender, *notifierPassword)
} else {
fmt.Println("Got no offers.")
}

View File

@ -1,95 +0,0 @@
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
}