catbase/plugins/nerdepedia/nerdepedia.go

136 lines
3.4 KiB
Go
Raw Normal View History

2019-01-19 00:48:12 +00:00
// © 2019 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package nerdepedia
import (
"bufio"
"fmt"
"html"
2019-01-19 00:48:12 +00:00
"net/http"
2021-02-07 20:10:24 +00:00
"regexp"
"strings"
2019-01-19 00:48:12 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
)
const (
descriptionPrefix = "<meta name=\"description\" content=\""
linkPrefix = "<link rel=\"canonical\" href=\""
closingTagSuffix = "\" />"
2019-01-19 00:48:12 +00:00
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var (
client HTTPClient
)
func init() {
client = &http.Client{}
}
2019-01-19 00:48:12 +00:00
type NerdepediaPlugin struct {
bot bot.Bot
config *config.Config
}
// NewNerdepediaPlugin creates a new NerdepediaPlugin with the Plugin interface
func New(b bot.Bot) *NerdepediaPlugin {
np := &NerdepediaPlugin{
bot: b,
config: b.Config(),
2019-01-19 00:48:12 +00:00
}
2021-02-07 20:10:24 +00:00
b.RegisterRegex(np, bot.Message, regexp.MustCompile(`.*`), np.message)
b.Register(np, bot.Help, np.help)
return np
2019-01-19 00:48:12 +00:00
}
func defaultSites() map[string]string {
starWars := "http://starwars.wikia.com/wiki/Special:Random"
starTrek := "http://memory-alpha.wikia.com/wiki/Special:Random"
dune := "http://dune.wikia.com/wiki/Special:Random"
lotr := "http://lotr.wikia.com/wiki/Special:Random"
pokemon := "http://pokemon.wikia.com/wiki/Special:Random"
return map[string]string{
"may the force be with you": starWars,
"help me obi-wan": starWars,
"beam me up scotty": starTrek,
"live long and prosper": starTrek,
"bless the maker": dune,
"i must not fear": dune,
"the spice must flow": dune,
"my precious": lotr,
"one ring to rule them all": lotr,
"one does not simply walk into mordor": lotr,
"pikachu i choose you": pokemon,
"gotta catch em all": pokemon,
}
}
2019-01-19 00:48:12 +00:00
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
2021-02-07 20:10:24 +00:00
func (p *NerdepediaPlugin) message(r bot.Request) bool {
c := r.Conn
message := r.Msg
2019-01-19 00:48:12 +00:00
lowerCase := strings.ToLower(message.Body)
query := ""
queries := p.config.GetMap("nerdepedia.sites", defaultSites())
for k, v := range queries {
if lowerCase == k {
query = v
break
}
2019-01-19 00:48:12 +00:00
}
if query != "" {
req, _ := http.NewRequest(http.MethodGet, query, nil)
resp, err := client.Do(req)
2019-01-19 00:48:12 +00:00
if err != nil {
return false
2019-01-19 00:48:12 +00:00
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
link := ""
description := ""
2019-01-19 00:48:12 +00:00
for scanner.Scan() {
line := scanner.Text()
if description == "" {
2019-01-19 13:38:19 +00:00
index := strings.Index(line, descriptionPrefix)
if index >= 0 {
description = html.UnescapeString(html.UnescapeString(strings.TrimSuffix(strings.TrimPrefix(line, descriptionPrefix), closingTagSuffix)))
}
}
if link == "" {
2019-01-19 13:38:19 +00:00
index := strings.Index(line, linkPrefix)
if index >= 0 {
2019-01-19 13:38:19 +00:00
link = strings.TrimSuffix(strings.TrimPrefix(line, linkPrefix), closingTagSuffix)
}
}
if description != "" && link != "" {
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, fmt.Sprintf("%s (%s)", description, link))
2019-01-19 00:48:12 +00:00
return true
}
}
}
return false
}
// Help responds to help requests. Every plugin must implement a help function.
func (p *NerdepediaPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool {
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, "nerd stuff")
return true
2019-01-19 00:48:12 +00:00
}