2016-03-29 14:29:42 +00:00
package main
import (
"flag"
"fmt"
2016-03-31 09:10:38 +00:00
"io/ioutil"
2016-03-29 14:29:42 +00:00
"net/http"
2016-03-31 09:10:38 +00:00
"strconv"
2016-03-29 14:29:42 +00:00
"strings"
"time"
"github.com/hamcha/clessy/tg"
)
func assert ( err error ) {
if err != nil {
panic ( err )
}
}
var chatID * int
var lastPage string
var broker * tg . Broker
var apichat * tg . APIChat
2016-03-31 09:10:38 +00:00
const upd8url = "http://tjb0607.me/upd8/check.js"
2016-03-29 14:29:42 +00:00
func process ( b * tg . Broker , update tg . APIMessage ) {
2016-03-31 09:10:38 +00:00
if update . Chat . ChatID == * chatID && * ( update . Text ) == "!starths" {
2016-03-29 14:29:42 +00:00
broker = b
apichat = update . Chat
broker . SendTextMessage ( apichat , "Ok! Aspetto updates.." , & update . MessageID )
}
2016-03-31 09:10:38 +00:00
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 )
}
}
2016-03-29 14:29:42 +00:00
}
2016-03-31 09:10:38 +00:00
func advert ( upd8 Upd8 ) {
pageint := strings . TrimLeft ( upd8 . Page , "0" )
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>"
2016-03-29 14:29:42 +00:00
broker . SendTextMessage ( apichat , text , nil )
}
2016-03-31 09:10:38 +00:00
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
}
2016-03-29 14:29:42 +00:00
func startPolling ( delay int64 ) {
panicFn := func ( err error ) {
broker . SendTextMessage ( apichat , "Errore cercando di leggere l'RSS! @HAMCHA LEGGI LA CONSOLE!" , nil )
panic ( err )
}
for broker == nil {
// Wait for broker
time . Sleep ( time . Second )
}
for {
2016-03-31 09:10:38 +00:00
resp , err := http . Get ( upd8url + "?" + strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 ) )
2016-03-29 14:29:42 +00:00
if err != nil {
panicFn ( err )
}
2016-03-31 09:10:38 +00:00
data , err := ioutil . ReadAll ( resp . Body )
2016-03-29 14:29:42 +00:00
if err != nil {
panicFn ( err )
}
2016-03-31 09:10:38 +00:00
isupd8 , upd8data := getupd8 ( string ( data ) )
if isupd8 {
advert ( upd8data )
2016-03-29 14:29:42 +00:00
}
time . Sleep ( time . Second * time . Duration ( delay ) )
}
}
func main ( ) {
brokerAddr := flag . String ( "broker" , "localhost:7314" , "Broker address:port" )
2016-03-31 09:10:38 +00:00
delay := flag . Int64 ( "delau" , 10 , "How many seconds between each poll" )
2016-03-29 14:29:42 +00:00
chatID = flag . Int ( "chatid" , - 68373985 , "Telegram Chat ID to count stats for" )
flag . Parse ( )
lastPage = ""
go startPolling ( * delay )
err := tg . CreateBrokerClient ( * brokerAddr , process )
assert ( err )
}