From d97a39febdd431c7058fb1054d304c9975279726 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 31 Mar 2016 10:10:38 +0100 Subject: [PATCH] MSPA: Use upd8.ninja instead of the RSS feed --- mspa/main.go | 93 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/mspa/main.go b/mspa/main.go index 65a7750..739a4c6 100644 --- a/mspa/main.go +++ b/mspa/main.go @@ -1,11 +1,11 @@ package main import ( - "encoding/xml" - "errors" "flag" "fmt" + "io/ioutil" "net/http" + "strconv" "strings" "time" @@ -23,36 +23,68 @@ var lastPage string var broker *tg.Broker var apichat *tg.APIChat -const mspaRSS = "http://mspaintadventures.com/rss/rss.xml" -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"` -} +const upd8url = "http://tjb0607.me/upd8/check.js" 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 apichat = update.Chat 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) { - date, _ := time.Parse(timelayout, page.Date) - dateStr := date.Format("15:04:05") - parts := strings.Split(page.Link, "p=") - pagen := strings.Trim(parts[1], "0") - text := "Nuova pagina uscita!\n\n#" + pagen + " - " + dateStr + "\n" + page.Title + "\n\nClicca qui per leggerla" +func advert(upd8 Upd8) { + pageint := strings.TrimLeft(upd8.Page, "0") + text := "Nuova pagina uscita!\n\n#" + pageint + "\n" + upd8.Title + "\n\nClicca qui per leggerla" 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) { panicFn := func(err error) { 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) } for { - resp, err := http.Get(mspaRSS) + resp, err := http.Get(upd8url + "?" + strconv.FormatInt(time.Now().Unix(), 10)) if err != nil { panicFn(err) } - var out RSS - err = xml.NewDecoder(resp.Body).Decode(&out) + data, err := ioutil.ReadAll(resp.Body) if err != nil { panicFn(err) } - if len(out.Items) < 1 { - panicFn(errors.New("0 items in the RSS Feed?")) - } - 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 + isupd8, upd8data := getupd8(string(data)) + if isupd8 { + advert(upd8data) } time.Sleep(time.Second * time.Duration(delay)) } @@ -88,7 +113,7 @@ func startPolling(delay int64) { func main() { 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") flag.Parse() lastPage = ""