Compare commits
2 Commits
2923aae54d
...
legacy_gol
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e235649ce | |||
| 3a7d1882e0 |
@@ -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
|
||||||
|
|||||||
76
main.go
76
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())
|
||||||
@@ -30,6 +31,7 @@ func main() {
|
|||||||
// Parse and check config values
|
// Parse and check config values
|
||||||
if cfg.Section("app").HasKey("subreddit") {
|
if cfg.Section("app").HasKey("subreddit") {
|
||||||
subreddit = cfg.Section("app").Key("subreddit").String()
|
subreddit = cfg.Section("app").Key("subreddit").String()
|
||||||
|
printConfig("subreddit", subreddit)
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'subreddit'")
|
quitConfigParseError("Missing 'subreddit'")
|
||||||
}
|
}
|
||||||
@@ -37,18 +39,23 @@ func main() {
|
|||||||
if cfg.Section("app").HasKey("interval") {
|
if cfg.Section("app").HasKey("interval") {
|
||||||
// default to 5 minutes
|
// default to 5 minutes
|
||||||
interval = cfg.Section("app").Key("interval").MustInt(5)
|
interval = cfg.Section("app").Key("interval").MustInt(5)
|
||||||
|
printConfig("interval", strconv.Itoa(interval))
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'interval'")
|
quitConfigParseError("Missing 'interval'")
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
|
for _, keys := range keywords {
|
||||||
|
printConfig("keyword", keys)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'keyword'")
|
quitConfigParseError("Missing 'keyword'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("smtp").HasKey("smtp_server") {
|
if cfg.Section("smtp").HasKey("smtp_server") {
|
||||||
smtpServer = cfg.Section("smtp").Key("smtp_server").String()
|
smtpServer = cfg.Section("smtp").Key("smtp_server").String()
|
||||||
|
printConfig("smtp_server", smtpServer)
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_server'")
|
quitConfigParseError("Missing 'smtp_server'")
|
||||||
}
|
}
|
||||||
@@ -56,30 +63,35 @@ func main() {
|
|||||||
if cfg.Section("smtp").HasKey("smtp_port") {
|
if cfg.Section("smtp").HasKey("smtp_port") {
|
||||||
// default to port 25
|
// default to port 25
|
||||||
smtpPort = cfg.Section("smtp").Key("smtp_port").MustInt(25)
|
smtpPort = cfg.Section("smtp").Key("smtp_port").MustInt(25)
|
||||||
|
printConfig("smtp_port", strconv.Itoa(smtpPort))
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_port'")
|
quitConfigParseError("Missing 'smtp_port'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("smtp").HasKey("smtp_username") {
|
if cfg.Section("smtp").HasKey("smtp_username") {
|
||||||
smtpUsername = cfg.Section("smtp").Key("smtp_username").String()
|
smtpUsername = cfg.Section("smtp").Key("smtp_username").String()
|
||||||
|
printConfig("smtp_username", smtpUsername)
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_username'")
|
quitConfigParseError("Missing 'smtp_username'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("smtp").HasKey("smtp_password") {
|
if cfg.Section("smtp").HasKey("smtp_password") {
|
||||||
smtpPassword = cfg.Section("smtp").Key("smtp_password").String()
|
smtpPassword = cfg.Section("smtp").Key("smtp_password").String()
|
||||||
|
printConfig("smtp_password", "<redacted>")
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_password'")
|
quitConfigParseError("Missing 'smtp_password'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("smtp").HasKey("smtp_to") {
|
if cfg.Section("smtp").HasKey("smtp_to") {
|
||||||
smtpTo = cfg.Section("smtp").Key("smtp_to").String()
|
smtpTo = cfg.Section("smtp").Key("smtp_to").String()
|
||||||
|
printConfig("smtp_to", smtpTo)
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_to'")
|
quitConfigParseError("Missing 'smtp_to'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Section("smtp").HasKey("smtp_from") {
|
if cfg.Section("smtp").HasKey("smtp_from") {
|
||||||
smtpFrom = cfg.Section("smtp").Key("smtp_from").String()
|
smtpFrom = cfg.Section("smtp").Key("smtp_from").String()
|
||||||
|
printConfig("smtp_from", smtpFrom)
|
||||||
} else {
|
} else {
|
||||||
quitConfigParseError("Missing 'smtp_from'")
|
quitConfigParseError("Missing 'smtp_from'")
|
||||||
}
|
}
|
||||||
@@ -123,22 +135,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
|
||||||
@@ -146,25 +159,46 @@ func loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printConfig(key string, value string) {
|
||||||
|
fmt.Println("Loaded "+key+": ", value)
|
||||||
|
}
|
||||||
|
|
||||||
func quitConfigParseError(msg string) {
|
func quitConfigParseError(msg string) {
|
||||||
fmt.Println("Error parsing config.ini: ", msg)
|
fmt.Println("Error parsing config.ini: ", msg)
|
||||||
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