2020-03-13 14:16:32 +00:00
|
|
|
package impossible
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-03-13 14:23:00 +00:00
|
|
|
"math/rand"
|
2020-03-13 14:16:32 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-03-13 14:23:00 +00:00
|
|
|
"time"
|
2020-03-13 14:16:32 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Impossible struct {
|
2021-02-07 19:00:41 +00:00
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
handlers bot.HandlerTable
|
2020-03-13 14:16:32 +00:00
|
|
|
|
|
|
|
title string
|
|
|
|
content []string
|
|
|
|
updated time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *Impossible {
|
|
|
|
i := &Impossible{
|
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
title: "",
|
|
|
|
content: []string{},
|
|
|
|
updated: getTodaysMidnight().Add(time.Hour * -24),
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Register(i, bot.Help, i.help)
|
2021-02-07 19:00:41 +00:00
|
|
|
i.register()
|
2020-03-13 14:16:32 +00:00
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Impossible) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.b.Send(c, bot.Message, message.Channel, "You don't need to do anything. I'll take care of it. But guess what I'm thinking.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-07 19:00:41 +00:00
|
|
|
func (p *Impossible) tryRefresh(r bot.Request) (sent bool) {
|
2020-03-13 14:16:32 +00:00
|
|
|
if p.updated.Before(time.Now()) {
|
|
|
|
if p.title != "" {
|
2021-02-07 19:00:41 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
|
|
|
sent = true
|
2020-03-13 14:16:32 +00:00
|
|
|
}
|
2020-03-13 14:23:00 +00:00
|
|
|
for !p.refreshImpossible() {
|
|
|
|
}
|
2020-03-13 14:16:32 +00:00
|
|
|
|
|
|
|
}
|
2021-02-07 19:00:41 +00:00
|
|
|
return sent
|
|
|
|
}
|
2020-03-13 14:16:32 +00:00
|
|
|
|
2021-02-07 19:00:41 +00:00
|
|
|
func (p *Impossible) register() {
|
|
|
|
p.handlers = bot.HandlerTable{
|
|
|
|
{Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^hint|clue$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
if p.tryRefresh(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.content[rand.Intn(len(p.content))])
|
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^i friggin give up.?$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
if p.tryRefresh(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
|
|
|
for !p.refreshImpossible() {
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
if p.tryRefresh(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(strings.ToLower(r.Msg.Body), strings.ToLower(p.title)) {
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
|
|
|
for !p.refreshImpossible() {
|
|
|
|
}
|
2021-02-07 20:05:30 +00:00
|
|
|
return true
|
2021-02-07 19:00:41 +00:00
|
|
|
}
|
2021-02-07 20:05:30 +00:00
|
|
|
return false
|
2021-02-07 19:00:41 +00:00
|
|
|
}},
|
2020-03-13 14:16:32 +00:00
|
|
|
}
|
2021-02-07 19:00:41 +00:00
|
|
|
p.b.RegisterTable(p, p.handlers)
|
2020-03-13 14:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Impossible) refreshImpossible() bool {
|
|
|
|
p.updated = getTodaysMidnight()
|
|
|
|
resp, err := http.Get("https://en.wikipedia.org/wiki/Special:Random")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
titleRegex := regexp.MustCompile(`id="firstHeading"[^>]*(?P<Title>[^<]*)`)
|
2020-03-13 14:23:00 +00:00
|
|
|
results := titleRegex.FindStringSubmatch(string(body))
|
|
|
|
title := results[1][1:] //remove the leading <
|
2020-03-13 14:16:32 +00:00
|
|
|
|
2020-03-13 14:23:00 +00:00
|
|
|
if title == "" {
|
|
|
|
return false
|
|
|
|
}
|
2020-03-13 14:16:32 +00:00
|
|
|
|
|
|
|
p.title = title
|
|
|
|
p.content = []string{}
|
|
|
|
|
|
|
|
resp, err = http.Get("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&titles=" + url.PathEscape(title))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
var object map[string]interface{}
|
|
|
|
json.Unmarshal([]byte(body), &object)
|
|
|
|
|
|
|
|
pages := object["query"].(map[string]interface{})["pages"].(map[string]interface{})
|
|
|
|
for _, page := range pages {
|
|
|
|
descriptionText := page.(map[string]interface{})["extract"].(string)
|
|
|
|
sentences := strings.Split(strings.ReplaceAll(descriptionText, "\n", " "), ". ")
|
|
|
|
for _, sentence := range sentences {
|
|
|
|
trimmed := strings.ToLower(strings.TrimSpace(sentence))
|
|
|
|
if len(trimmed) == 0 || strings.HasPrefix(trimmed, "==") || len(strings.Split(trimmed, " ")) < 5 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
censored := strings.ReplaceAll(trimmed, strings.ToLower(title), "?????")
|
|
|
|
|
|
|
|
p.content = append(p.content, censored)
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 14:23:00 +00:00
|
|
|
return true
|
2020-03-13 14:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTodaysMidnight() time.Time {
|
2021-11-08 13:47:58 +00:00
|
|
|
y, m, d := time.Now().Date()
|
|
|
|
return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
|
2020-03-13 14:16:32 +00:00
|
|
|
}
|