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"
2018-04-03 10:07:06 +00:00
"github.com/hamcha/tg"
2016-03-29 14:29:42 +00:00
)
func assert ( err error ) {
if err != nil {
panic ( err )
}
}
var lastPage string
var broker * tg . Broker
2016-03-31 09:23:44 +00:00
var apichat map [ int ] * tg . APIChat
const devid = 75510391
2016-03-29 14:29:42 +00:00
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:23:44 +00:00
if broker == nil {
2016-03-29 14:29:42 +00:00
broker = b
}
2016-03-31 09:23:44 +00:00
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 )
}
2016-03-31 09:24:13 +00:00
/ *
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 )
}
2016-03-31 09:10:38 +00:00
}
2016-03-31 09:24:13 +00:00
* /
2016-03-29 14:29:42 +00:00
}
2016-03-31 09:10:38 +00:00
func advert ( upd8 Upd8 ) {
2016-03-31 09:23:44 +00:00
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 )
}
}
2016-03-29 14:29:42 +00:00
}
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 ) {
2016-03-31 09:23:44 +00:00
broker . SendTextMessage ( & tg . APIChat { ChatID : devid } , "Errore cercando di leggere l'update feed! @HAMCHA LEGGI LA CONSOLE!" , nil )
2016-03-29 14:29:42 +00:00
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
flag . Parse ( )
lastPage = ""
2016-03-31 09:23:44 +00:00
apichat = make ( map [ int ] * tg . APIChat )
2016-03-29 14:29:42 +00:00
go startPolling ( * delay )
2017-04-21 15:20:49 +00:00
_ , err := tg . CreateBrokerClient ( * brokerAddr , process )
2016-03-29 14:29:42 +00:00
assert ( err )
}