145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hamcha/clessy/tg"
|
|
)
|
|
|
|
func assert(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
var lastPage string
|
|
var broker *tg.Broker
|
|
var apichat map[int]*tg.APIChat
|
|
|
|
const devid = 75510391
|
|
|
|
const upd8url = "http://tjb0607.me/upd8/check.js"
|
|
|
|
func process(b *tg.Broker, update tg.APIMessage) {
|
|
if broker == nil {
|
|
broker = b
|
|
}
|
|
if update.Text == nil {
|
|
return
|
|
}
|
|
if *(update.Text) == "!starths" {
|
|
apichat[update.Chat.ChatID] = update.Chat
|
|
broker.SendTextMessage(update.Chat, "Ok! Cercherò di notificarti di eventuali updates..", &update.MessageID)
|
|
}
|
|
if *(update.Text) == "!stophs" {
|
|
delete(apichat, update.Chat.ChatID)
|
|
broker.SendTextMessage(update.Chat, "Ok! Non ti notificherò più", &update.MessageID)
|
|
}
|
|
/*
|
|
if *(update.Text) == "!tesths" {
|
|
isok, data := getupd8("showupd8(\"009309\", \"[S] ACT 6 ACT 6 ACT 5\", 12);\r\n")
|
|
if isok {
|
|
advert(data)
|
|
} else {
|
|
broker.SendTextMessage(update.Chat, "Test fallito :/", &update.MessageID)
|
|
}
|
|
}
|
|
*/
|
|
}
|
|
|
|
func advert(upd8 Upd8) {
|
|
if broker != nil {
|
|
pageint := strings.TrimLeft(upd8.Page, "0")
|
|
msg := "È uscita una nuova pagina!"
|
|
if upd8.Count > 1 {
|
|
msg = "Sono uscite " + strconv.Itoa(upd8.Count) + " pagine!"
|
|
}
|
|
text := "<b>" + msg + "</b>\n\n#" + pageint + "\n" + upd8.Title + "\n\n<a href=\"http://www.mspaintadventures.com/?s=6&p=" + upd8.Page + "\">Clicca qui per leggerla</a>"
|
|
for _, v := range apichat {
|
|
broker.SendTextMessage(v, text, nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
type Upd8 struct {
|
|
IsUpd8 bool
|
|
Page string
|
|
Title string
|
|
Count int
|
|
}
|
|
|
|
func getupd8(body string) (isupd8 bool, out Upd8) {
|
|
parts := strings.Split(body, "(")
|
|
if parts[0] == "shownoupd8" {
|
|
isupd8 = false
|
|
return
|
|
}
|
|
isupd8 = true
|
|
if len(parts) < 2 {
|
|
fmt.Println("Invalid reply from server: " + body)
|
|
isupd8 = false
|
|
return
|
|
}
|
|
args := strings.Split(parts[1], ",")
|
|
if len(args) < 3 {
|
|
fmt.Println("Invalid reply from server: " + body)
|
|
isupd8 = false
|
|
return
|
|
}
|
|
out.Page = strings.Trim(args[0], "\" ")
|
|
out.Title = strings.Trim(args[1], "\" ")
|
|
pagestr := strings.Trim(args[2], " );\r\n")
|
|
var err error
|
|
out.Count, err = strconv.Atoi(pagestr)
|
|
if err != nil {
|
|
fmt.Println("Invalid page count: " + pagestr)
|
|
isupd8 = false
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func startPolling(delay int64) {
|
|
panicFn := func(err error) {
|
|
broker.SendTextMessage(&tg.APIChat{ChatID: devid}, "Errore cercando di leggere l'update feed! @HAMCHA LEGGI LA CONSOLE!", nil)
|
|
panic(err)
|
|
}
|
|
for broker == nil {
|
|
// Wait for broker
|
|
time.Sleep(time.Second)
|
|
}
|
|
for {
|
|
resp, err := http.Get(upd8url + "?" + strconv.FormatInt(time.Now().Unix(), 10))
|
|
if err != nil {
|
|
panicFn(err)
|
|
}
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
panicFn(err)
|
|
}
|
|
isupd8, upd8data := getupd8(string(data))
|
|
if isupd8 {
|
|
advert(upd8data)
|
|
}
|
|
time.Sleep(time.Second * time.Duration(delay))
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
|
delay := flag.Int64("delau", 10, "How many seconds between each poll")
|
|
flag.Parse()
|
|
lastPage = ""
|
|
apichat = make(map[int]*tg.APIChat)
|
|
|
|
go startPolling(*delay)
|
|
|
|
err := tg.CreateBrokerClient(*brokerAddr, process)
|
|
assert(err)
|
|
}
|