Skip to content

Instantly share code, notes, and snippets.

@kudarap
Created July 1, 2020 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kudarap/c9f046e26809a4849baad1de70bd0843 to your computer and use it in GitHub Desktop.
Save kudarap/c9f046e26809a4849baad1de70bd0843 to your computer and use it in GitHub Desktop.
dota 2 blog content watcher
package main
import (
"flag"
"fmt"
"log"
"strings"
"net/http"
"io/ioutil"
"os/exec"
"math/rand"
"time"
)
// Usage:
// ./bpalert -key=Compass of the Rising Gale
func main() {
keyPtr := flag.String("key", "Battle Pass", "keyword")
countPtr := flag.Int("count", 3, "keyword count")
sleepPtr := flag.Int("sleep", 100, "base sleep seconds")
flag.Parse()
for {
check(*keyPtr, *countPtr)
t := randSleep(*sleepPtr) * time.Second
log.Println("next check after", t)
time.Sleep(t)
}
}
func check(keyword string, count int) {
log.Println("requesting...")
resp, err := http.Get("http://blog.dota2.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
s := fmt.Sprintf("%s", body)
c := len(strings.Split(s, keyword))
if c > count {
log.Println("its here!")
for { beep() }
} else {
log.Println("not yet!")
}
}
func beep() {
cmd := exec.Command("osascript", "-e", "beep")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
func randSleep(sleepTime int) time.Duration {
rand.Seed(time.Now().UnixNano())
return time.Duration(rand.Intn(sleepTime) + 60)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment