From 6e39e6ec2b2d186db2ab7fabf9587b6aafe87699 Mon Sep 17 00:00:00 2001 From: skkiesel Date: Fri, 18 Jan 2019 19:48:12 -0500 Subject: [PATCH 1/2] Up your nerd game, nerd. --- main.go | 2 + plugins/nerdepedia/nerdepedia.go | 84 +++++++++++++++++++++++++++ plugins/nerdepedia/nerdepeida_test.go | 35 +++++++++++ 3 files changed, 121 insertions(+) create mode 100644 plugins/nerdepedia/nerdepedia.go create mode 100644 plugins/nerdepedia/nerdepeida_test.go diff --git a/main.go b/main.go index 0395309..3b1a22f 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "github.com/velour/catbase/plugins/first" "github.com/velour/catbase/plugins/inventory" "github.com/velour/catbase/plugins/leftpad" + "github.com/velour/catbase/plugins/nerdepedia" "github.com/velour/catbase/plugins/picker" "github.com/velour/catbase/plugins/reaction" "github.com/velour/catbase/plugins/reminder" @@ -80,6 +81,7 @@ func main() { b.AddHandler("sisyphus", sisyphus.New(b)) b.AddHandler("tell", tell.New(b)) b.AddHandler("couldashouldawoulda", couldashouldawoulda.New(b)) + b.AddHandler("nedepedia", nerdepedia.New(b)) // catches anything left, will always return true b.AddHandler("factoid", fact.New(b)) b.AddHandler("db", db.New(b)) diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go new file mode 100644 index 0000000..8252157 --- /dev/null +++ b/plugins/nerdepedia/nerdepedia.go @@ -0,0 +1,84 @@ +// © 2019 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package nerdepedia + +import ( + "strings" + "net/http" + "bufio" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/config" +) + +const ( + prefix = "= 0 { + p.bot.SendMessage(message.Channel, strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\" />")) + return true + } + } + } + return false +} + +// Help responds to help requests. Every plugin must implement a help function. +func (p *NerdepediaPlugin) Help(channel string, parts []string) { + p.bot.SendMessage(channel, "star wars/trek stuff") +} + +// Empty event handler because this plugin does not do anything on event recv +func (p *NerdepediaPlugin) Event(kind string, message msg.Message) bool { + return false +} + +// Handler for bot's own messages +func (p *NerdepediaPlugin) BotMessage(message msg.Message) bool { + return false +} + +// Register any web URLs desired +func (p *NerdepediaPlugin) RegisterWeb() *string { + return nil +} + +func (p *NerdepediaPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/nerdepedia/nerdepeida_test.go b/plugins/nerdepedia/nerdepeida_test.go new file mode 100644 index 0000000..04d3ad0 --- /dev/null +++ b/plugins/nerdepedia/nerdepeida_test.go @@ -0,0 +1,35 @@ +// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package nerdepedia + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/bot/user" +) + +func makeMessage(payload string) msg.Message { + isCmd := strings.HasPrefix(payload, "!") + if isCmd { + payload = payload[1:] + } + return msg.Message{ + User: &user.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + } +} + +func TestObiWan(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("help me obi-wan")) + assert.Len(t, mb.Messages, 1) + assert.True(t, res) +} From cf6e2a9c1ce0abd3d35d3c1b6f675ef339fdb406 Mon Sep 17 00:00:00 2001 From: skkiesel Date: Fri, 18 Jan 2019 19:58:31 -0500 Subject: [PATCH 2/2] Don't panic. Unescape description. Add link. Remove 'make it so' --- plugins/nerdepedia/nerdepedia.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index 8252157..cb7140e 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -3,7 +3,9 @@ package nerdepedia import ( + "fmt" "strings" + "html" "net/http" "bufio" @@ -13,7 +15,10 @@ import ( ) const ( - prefix = "" ) type NerdepediaPlugin struct { @@ -37,23 +42,37 @@ func (p *NerdepediaPlugin) Message(message msg.Message) bool { query := "" if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" { query = "http://starwars.wikia.com/wiki/Special:Random" - } else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" || lowerCase == "make it so" { + } else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" { query = "http://memory-alpha.wikia.com/wiki/Special:Random" } if query != "" { resp, err := http.Get(query) if err != nil { - panic(err) + return false } defer resp.Body.Close() scanner := bufio.NewScanner(resp.Body) + link := "" + description := "" for scanner.Scan() { line := scanner.Text() - index := strings.Index(line, prefix) - if index >= 0 { - p.bot.SendMessage(message.Channel, strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\" />")) + if description == "" { + index := strings.Index(line, linkPrefix) + if index >= 0 { + description = html.UnescapeString(strings.TrimSuffix(strings.TrimPrefix(line, linkPrefix), closingTagSuffix)) + } + } + if link == "" { + index := strings.Index(line, descriptionPrefix) + if index >= 0 { + link = strings.TrimSuffix(strings.TrimPrefix(line, descriptionPrefix), closingTagSuffix) + } + } + + if description != "" && link != "" { + p.bot.SendMessage(message.Channel, fmt.Sprintf("%s (%s)", description, link)) return true } }