Skip to content

Instantly share code, notes, and snippets.

@Skarlso
Created September 23, 2019 20:43
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 Skarlso/318ddd6f8d71dbda8fbbd1a908fdb159 to your computer and use it in GitHub Desktop.
Save Skarlso/318ddd6f8d71dbda8fbbd1a908fdb159 to your computer and use it in GitHub Desktop.
Idle RPG checker
package main
import (
"context"
"crypto/tls"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
mailgun "github.com/mailgun/mailgun-go"
log "github.com/sirupsen/logrus"
)
const (
appVersion = "1.0.0"
infoURI = "https://idlerpg.lolhosting.net/xml.php"
msgTemplate = `Dear %s
You seem to be offline based on https://idlerpg.lolhosting.net/xml.php?player=%s`
)
var (
domain = os.Getenv("MG_DOMAIN")
mgAPIKey = os.Getenv("MG_API_KEY")
)
type Player struct {
XMLName xml.Name `xml:"player"`
Username string `xml:"username"`
Level int `xml:"level"`
Online bool `xml:"online"`
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
}
func getUserInfo(username string) Player {
client := &http.Client{}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
request, _ := http.NewRequest("GET", infoURI, nil)
request.Header.Add("Accept", "text/xml")
query := request.URL.Query()
query.Add("player", username)
request.URL.RawQuery = query.Encode()
response, err := client.Do(request)
if err != nil {
panic(err.Error())
}
defer response.Body.Close()
content, _ := ioutil.ReadAll(response.Body)
var info Player
xml.Unmarshal(content, &info)
return info
}
func isOnline(username string) bool {
return getUserInfo(username).Online
}
func sendEmailNotification(username string, email string) {
mg := mailgun.NewMailgun(domain, mgAPIKey)
sender := fmt.Sprintf("no-reply@%s", domain)
subject := fmt.Sprintf("[%s] IdleRPG Notifier", time.Now().Format("2006-01-02"))
body := fmt.Sprintf(msgTemplate, username, username)
message := mg.NewMessage(sender, subject, body, email)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
mg.Send(ctx, message)
}
func main() {
username := flag.String("username", "", "IdleRPG Username")
email := flag.String("email", "", "Send notification if user is offline")
flag.Parse()
if *username == "" {
panic("Username is not specified")
}
if *email != "" && !isOnline(*username) {
log.Infoln("Notify user:", *username)
sendEmailNotification(*username, *email)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment