Created
September 6, 2016 13:09
-
-
Save kiliankoe/066994dcbcba5eebaa961d5d84189f38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"html" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
"github.com/SlyMarbo/rss" | |
"github.com/gregdel/pushover" | |
) | |
func main() { | |
quote, link, id := getLastQuote() | |
if isNew(id) { | |
err := ioutil.WriteFile("lastquote.txt", []byte(id), 0644) | |
if err == nil { | |
// Panicking on the error but sending the notifications nonetheless | |
// would spam pushover and slack every minute until I fix it due | |
// to cron. Let's not do that. | |
sendPushoverNotifications(quote, link) | |
sendSlackNotification(id, link) | |
} | |
} | |
} | |
func getLastQuote() (string, string, string) { | |
bashFeedURL := os.Getenv("BASH_FEED_URL") | |
if bashFeedURL == "" { | |
panic("BASH_FEED_URL not set") | |
} | |
feed, err := rss.Fetch(bashFeedURL) | |
if err != nil { | |
panic(err) | |
} | |
r, _ := regexp.Compile(`\d+`) | |
lastQuote := feed.Items[0] | |
content := lastQuote.Content | |
content = html.UnescapeString(content) | |
return content, lastQuote.Link, r.FindString(lastQuote.Link) | |
} | |
func isNew(id string) bool { | |
if _, err := os.Stat("lastquote.txt"); os.IsNotExist(err) { | |
// lastquote.txt does not yet exist | |
err := ioutil.WriteFile("lastquote.txt", []byte{}, 0644) | |
if err != nil { | |
panic(err) | |
} | |
} | |
last, err := ioutil.ReadFile("lastquote.txt") | |
if err != nil { | |
panic(err) | |
} | |
return id != strings.TrimSpace(string(last)) | |
} | |
func sendPushoverNotifications(quote, link string) { | |
pushoverAPIToken := os.Getenv("PUSHOVER_API_TOKEN") | |
pushoverRecipient := os.Getenv("PUSHOVER_RECIPIENT") | |
if pushoverAPIToken == "" || pushoverRecipient == "" { | |
panic("PUSHOVER_API_TOKEN or PUSHOVER_RECIPIENT not set") | |
} | |
app := pushover.New(pushoverAPIToken) | |
recipient := pushover.NewRecipient(pushoverRecipient) | |
message := &pushover.Message{ | |
Title: "fsr.bash", | |
Message: quote, | |
URL: link, | |
} | |
_, err := app.SendMessage(message, recipient) | |
if err != nil { | |
panic(err) | |
} | |
} | |
func sendSlackNotification(id, link string) { | |
hc := http.Client{} | |
slackHookURL := os.Getenv("SLACK_HOOK_URL") | |
if slackHookURL == "" { | |
panic("SLACK_HOOK_URL not set") | |
} | |
quoteString := fmt.Sprintf("Neues Bash Zitat -> <%s|%s> 🤐", link, id) | |
payload := map[string]string{"text": quoteString} | |
jsonPayload, _ := json.Marshal(payload) | |
req, _ := http.NewRequest("POST", slackHookURL, bytes.NewBufferString(string(jsonPayload))) | |
resp, _ := hc.Do(req) | |
if resp.StatusCode != 200 { | |
panic("Post to Slack failed /o\\") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment