/
rss.go
90 lines (80 loc) · 2.05 KB
/
rss.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
package main
import (
"bytes"
"encoding/json"
"fmt"
rss "github.com/jteeuwen/go-pkg-rss"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
)
func main() {
feed := rss.New(5, true, chanHandler, itemHandler)
for {
refreshInterests()
if err := feed.Fetch("http://feeds.feedburner.com/TheFlightDeal", nil); err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}
<-time.After(1 * time.Hour)
}
}
func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
}
type OredInterests []string
type Interests struct {
Email string
AndedInterests []OredInterests
}
var interests []Interests
func getRecipientsForItem(item *rss.Item) []string {
var recipients []string
for _, interest := range interests {
ciTit := strings.ToLower(item.Title)
matchesAnd := true
for _, oredInterest := range interest.AndedInterests {
matchesOr := false
for _, wantedSubstring := range oredInterest {
matchesOr = matchesOr || strings.Contains(ciTit, wantedSubstring)
if matchesOr {
break
}
}
matchesAnd = matchesAnd && matchesOr
if !matchesAnd {
break
}
}
if matchesAnd {
recipients = append(recipients, interest.Email)
}
}
return recipients
}
func sendEmailForItem(item *rss.Item, email string) {
println(email, item.Title)
// _ = exec.Command
// _ = bytes.NewReader
cmd := exec.Command("mail", "-s", "Flight deal: "+item.Title, email)
cmd.Stdin = bytes.NewReader([]byte(fmt.Sprintf("Link: %s\n\nDescription: %s\n", item.Comments, item.Description)))
if output, err := cmd.CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "[e] mail error: %s; output: %q\n", err, string(output))
}
}
func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
for _, newItem := range newitems {
recipients := getRecipientsForItem(newItem)
for _, recipient := range recipients {
sendEmailForItem(newItem, recipient)
}
}
}
func refreshInterests() {
if bytes, err := ioutil.ReadFile(os.Args[1]); err != nil {
panic(err)
} else if err := json.Unmarshal(bytes, &interests); err != nil {
panic(err)
}
}