MSPA: Use upd8.ninja instead of the RSS feed

This commit is contained in:
Hamcha 2016-03-31 10:10:38 +01:00
parent 6125dd34e6
commit d97a39febd

View file

@ -1,11 +1,11 @@
package main package main
import ( import (
"encoding/xml"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time" "time"
@ -23,36 +23,68 @@ var lastPage string
var broker *tg.Broker var broker *tg.Broker
var apichat *tg.APIChat var apichat *tg.APIChat
const mspaRSS = "http://mspaintadventures.com/rss/rss.xml" const upd8url = "http://tjb0607.me/upd8/check.js"
const timelayout = "Mon, 2 Jan 2006 15:04:05 -0700"
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Date string `xml:"pubDate"`
}
type RSS struct {
Items []Item `xml:"channel>item"`
}
func process(b *tg.Broker, update tg.APIMessage) { func process(b *tg.Broker, update tg.APIMessage) {
if update.Chat.ChatID == *chatID && *(update.Text) == "!start" { if update.Chat.ChatID == *chatID && *(update.Text) == "!starths" {
broker = b broker = b
apichat = update.Chat apichat = update.Chat
broker.SendTextMessage(apichat, "Ok! Aspetto updates..", &update.MessageID) broker.SendTextMessage(apichat, "Ok! Aspetto updates..", &update.MessageID)
} }
if update.Chat.ChatID == *chatID && *(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(page Item) { func advert(upd8 Upd8) {
date, _ := time.Parse(timelayout, page.Date) pageint := strings.TrimLeft(upd8.Page, "0")
dateStr := date.Format("15:04:05") text := "<b>Nuova pagina uscita!</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>"
parts := strings.Split(page.Link, "p=")
pagen := strings.Trim(parts[1], "0")
text := "<b>Nuova pagina uscita!</b>\n\n#" + pagen + " - " + dateStr + "\n" + page.Title + "\n\n<a href=\"" + page.Link + "\">Clicca qui per leggerla</a>"
broker.SendTextMessage(apichat, text, nil) broker.SendTextMessage(apichat, 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) { func startPolling(delay int64) {
panicFn := func(err error) { panicFn := func(err error) {
broker.SendTextMessage(apichat, "Errore cercando di leggere l'RSS! @HAMCHA LEGGI LA CONSOLE!", nil) broker.SendTextMessage(apichat, "Errore cercando di leggere l'RSS! @HAMCHA LEGGI LA CONSOLE!", nil)
@ -63,24 +95,17 @@ func startPolling(delay int64) {
time.Sleep(time.Second) time.Sleep(time.Second)
} }
for { for {
resp, err := http.Get(mspaRSS) resp, err := http.Get(upd8url + "?" + strconv.FormatInt(time.Now().Unix(), 10))
if err != nil { if err != nil {
panicFn(err) panicFn(err)
} }
var out RSS data, err := ioutil.ReadAll(resp.Body)
err = xml.NewDecoder(resp.Body).Decode(&out)
if err != nil { if err != nil {
panicFn(err) panicFn(err)
} }
if len(out.Items) < 1 { isupd8, upd8data := getupd8(string(data))
panicFn(errors.New("0 items in the RSS Feed?")) if isupd8 {
} advert(upd8data)
if out.Items[0].Link != lastPage {
fmt.Println("Got new page: " + out.Items[0].Link)
if lastPage != "" {
advert(out.Items[0])
}
lastPage = out.Items[0].Link
} }
time.Sleep(time.Second * time.Duration(delay)) time.Sleep(time.Second * time.Duration(delay))
} }
@ -88,7 +113,7 @@ func startPolling(delay int64) {
func main() { func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
delay := flag.Int64("delau", 5*60, "How many seconds between each poll") delay := flag.Int64("delau", 10, "How many seconds between each poll")
chatID = flag.Int("chatid", -68373985, "Telegram Chat ID to count stats for") chatID = flag.Int("chatid", -68373985, "Telegram Chat ID to count stats for")
flag.Parse() flag.Parse()
lastPage = "" lastPage = ""