Allow multiple searches with different keywords; allow multiple keywords in each search
This commit is contained in:
@@ -8,6 +8,9 @@ interval = 5
|
|||||||
# Keyword to search for within title and text
|
# Keyword to search for within title and text
|
||||||
keyword = cute
|
keyword = cute
|
||||||
|
|
||||||
|
# Addional keyword search, split by commas
|
||||||
|
keyword = pretty, bun
|
||||||
|
|
||||||
# SMTP information
|
# SMTP information
|
||||||
[smtp]
|
[smtp]
|
||||||
smtp_server = localhost
|
smtp_server = localhost
|
||||||
|
61
main.go
61
main.go
@@ -15,13 +15,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
subreddit, keyword, smtpServer, smtpTo, smtpFrom, smtpUsername, smtpPassword string
|
subreddit, smtpServer, smtpTo, smtpFrom, smtpUsername, smtpPassword string
|
||||||
interval, smtpPort int
|
keywords []string
|
||||||
|
interval, smtpPort int
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Load config
|
// Load config
|
||||||
cfg, err := ini.Load("config.ini")
|
cfg, err := ini.ShadowLoad("config.ini")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
quitConfigParseError(err.Error())
|
quitConfigParseError(err.Error())
|
||||||
@@ -42,7 +43,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("app").HasKey("keyword") {
|
if cfg.Section("app").HasKey("keyword") {
|
||||||
keyword = cfg.Section("app").Key("keyword").String()
|
keywords = cfg.Section("app").Key("keyword").ValueWithShadows()
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'keyword'")
|
quitConfigParseError("Missing 'keyword'")
|
||||||
}
|
}
|
||||||
@@ -123,22 +124,23 @@ func loop() {
|
|||||||
index := strconv.Itoa(i)
|
index := strconv.Itoa(i)
|
||||||
title, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "title")
|
title, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "title")
|
||||||
text, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "selftext")
|
text, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "selftext")
|
||||||
alert := false
|
|
||||||
|
|
||||||
// Check if keyword matches
|
for _, keys := range keywords {
|
||||||
if strings.Contains(strings.ToLower(title), keyword) {
|
// Check for keywords
|
||||||
alert = true
|
alert := false
|
||||||
} else if strings.Contains(strings.ToLower(text), keyword) {
|
if compareToKeywords(strings.ToLower(title), strings.ToLower(keys)) {
|
||||||
alert = true
|
alert = true
|
||||||
|
} else if compareToKeywords(strings.ToLower(text), strings.ToLower(keys)) {
|
||||||
|
alert = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send alert if keyword found
|
||||||
|
if alert {
|
||||||
|
url, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "url")
|
||||||
|
timestamp, _ := jsonparser.GetFloat(body, "data", "children", "["+index+"]", "data", "created_utc")
|
||||||
|
validateAlert(title, text, url, int64(timestamp), keys)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send alert if keyword found
|
|
||||||
if alert {
|
|
||||||
url, _ := jsonparser.GetString(body, "data", "children", "["+index+"]", "data", "url")
|
|
||||||
timestamp, _ := jsonparser.GetFloat(body, "data", "children", "["+index+"]", "data", "created_utc")
|
|
||||||
validateAlert(title, text, url, int64(timestamp))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sleep for interval time
|
// Sleep for interval time
|
||||||
@@ -151,20 +153,37 @@ func quitConfigParseError(msg string) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func compareToKeywords(text string, keyword string) bool {
|
||||||
|
// Split keywords on commas
|
||||||
|
keys := strings.Split(keyword, ",")
|
||||||
|
found := false
|
||||||
|
|
||||||
|
// Check to ensure it contains ALL the keywords
|
||||||
|
for _, key := range keys {
|
||||||
|
if strings.Contains(text, strings.TrimSpace(key)) {
|
||||||
|
found = true
|
||||||
|
} else {
|
||||||
|
found = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
// Validate the alert to ensure that it needs to be sent
|
// Validate the alert to ensure that it needs to be sent
|
||||||
func validateAlert(title string, text string, url string, timestamp int64) {
|
func validateAlert(title string, text string, url string, timestamp int64, keyword string) {
|
||||||
// Get timestamp of interval period
|
// Get timestamp of interval period
|
||||||
currentTs := time.Now()
|
currentTs := time.Now()
|
||||||
intervalTs := currentTs.Add(-time.Minute * time.Duration(interval))
|
intervalTs := currentTs.Add(-time.Minute * time.Duration(interval))
|
||||||
|
|
||||||
// Only send alert if it's newer than interval time period
|
// Only send alert if it's newer than interval time period
|
||||||
if timestamp > intervalTs.Unix() {
|
if timestamp > intervalTs.Unix() {
|
||||||
sendAlert(title, text, url)
|
sendAlert(title, text, url, keyword)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the alert out
|
// Send the alert out
|
||||||
func sendAlert(title string, text string, url string) {
|
func sendAlert(title string, text string, url string, keyword string) {
|
||||||
// Setup
|
// Setup
|
||||||
m := gomail.NewMessage()
|
m := gomail.NewMessage()
|
||||||
m.SetHeader("From", smtpFrom)
|
m.SetHeader("From", smtpFrom)
|
||||||
|
Reference in New Issue
Block a user