kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit 86b55aa572a4753f0b400a3e12edf1e2f92fc1a3
parent 40b636f3a31144d31b7daea6f269eebaa1ea9093
Author: dsp <dsp@2f30.org>
Date:   Tue, 29 Apr 2014 14:39:17 +0100

reimplemented ResolveYoutubeTitle with native golang parsing, now titles are written to the urlDB upon insertion.

Diffstat:
Msrc/kunt/kunt.go | 68+++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 45 insertions(+), 23 deletions(-)

diff --git a/src/kunt/kunt.go b/src/kunt/kunt.go @@ -5,7 +5,6 @@ package main import ( - "bytes" "flag" "fmt" "games" @@ -18,6 +17,7 @@ import ( "net/http" "os" "os/exec" + "regexp" "strings" "syscall" "time" @@ -28,23 +28,6 @@ type kuntCtx struct { ircCtx *irc.Context } -func resolveYoutubeTitle(link string) (title string, ret bool) { - var out bytes.Buffer - cmd := exec.Command("./resolve", link) - cmd.Stdout = &out - err := cmd.Run() - if err != nil { - if _, ok := err.(*exec.ExitError); ok { - title = "" - ret = false - return - } - } - title = out.String() - ret = true - return -} - func cmdKunt(msg irc.Message) { replies := []string{ "dn niotho maaaan", @@ -196,22 +179,55 @@ func cmdRandUrl(msg irc.Message) { r := fmt.Sprintf("%s%02d[%d]%s %s", "\003", '5', idx, "\003", string(url)) kunt.ircCtx.Privmsg(msg.Args[0], r) - title, ok := resolveYoutubeTitle(string(url)) - if ok { - r = fmt.Sprintf("[YouTube] %s", title) - kunt.ircCtx.Privmsg(msg.Args[0], r) +} + +func resolveYoutubeTitle(url string) (title string, ok bool) { + title = "" + ok = false + if len(ytregex.FindAllString(url, -1)) > 0 { + title = "Unresolved title" + ok = true + resp, err := http.Get(url) + if err == nil { + defer resp.Body.Close() + contents, err := ioutil.ReadAll(resp.Body) + if err == nil { + titles := titleregex.FindAllString(string(contents), 1) + if len(titles) > 0 { + //this relates to the way titleregex is + title = string(titles[0][35 : len(titles[0])-2]) + } else { + log.Printf(" [addurl] could not find the title regexp in youtube HTTP GET response\n") + } + } else { + log.Printf(" [addurl] could not read response body with ioutil\n") + } + } else { + log.Printf(" [addurl] could not connect to youtube url for title fetching \n") + } + //print [Youtube] tag in purple + title = fmt.Sprintf(" %s%02d[%s]%s %s", + "\003", '6', "Youtube", "\003", string(title)) } + return } func cmdAddUrl(msg irc.Message) { text := msg.Args[1] + title := "" if len(strings.Fields(text)) < 2 { kunt.ircCtx.Privmsg(msg.Args[0], "Missing parameter for !addurl") return } text = text[8:] text = strings.TrimSpace(text) - text += "\n" + // Check for yt link + title, ok := resolveYoutubeTitle(text) + if ok { + text += title + "\n" + } else { + text += "\n" + } buf := []byte(text) if urlDb.CountMatches(buf) > 0 { kunt.ircCtx.Privmsg(msg.Args[0], @@ -353,6 +369,8 @@ var urlDb *mapfs.Mapfs var kunt kuntCtx var geng games.GameEnger var botname string +var ytregex *regexp.Regexp +var titleregex *regexp.Regexp func main() { kunt.stime = time.Now() @@ -383,6 +401,10 @@ func main() { log.Fatal(err) } + // Compile the yt regexp + ytregex = regexp.MustCompile("(((www.)?youtube.com/.*(.v=|&v=).+)|(youtu.be/.+))") + titleregex = regexp.MustCompile("<meta property=\"og:title\" content=\".*\">") + // Check for duplicate entries iter := mapfs.MakeMapIter(quoteDb) for {