From cf6e2a9c1ce0abd3d35d3c1b6f675ef339fdb406 Mon Sep 17 00:00:00 2001 From: skkiesel Date: Fri, 18 Jan 2019 19:58:31 -0500 Subject: [PATCH] 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 } }