From aeb149f5ad2c46369e7a7600db8720c5827322fa Mon Sep 17 00:00:00 2001 From: Gabriele Ficarelli Date: Sat, 4 Jul 2015 02:55:45 +0100 Subject: [PATCH 1/4] Added youtube search to commands --- commands/commands.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/commands/commands.go b/commands/commands.go index f279daf..db73a32 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -5,10 +5,15 @@ import ( "log" "strings" "time" + "flag" + "net/http" "github.com/jinzhu/gorm" _ "github.com/mattn/go-sqlite3" "github.com/whyrusleeping/hellabot" + + "code.google.com/p/google-api-go-client/googleapi/transport" + "code.google.com/p/google-api-go-client/youtube/v3" ) type Commands struct { @@ -22,6 +27,8 @@ var ( c Commands ) +const ytApiKey = "DEVELOPER KEY HERE" + func init() { c = Commands{} c.commands = map[string]interface{}{ @@ -29,6 +36,7 @@ func init() { "seen": lastSeen, "tell": tell, "ack": ack, + "yt": yt, } } @@ -88,6 +96,49 @@ func lastSeen(irc *hbot.IrcCon, msg *hbot.Message) bool { return true } +func yt(irc *hbot.IrcCon, msg *hbot.Message) bool { + log.Printf("Running yt command") + splitted := strings.SplitN(msg.Content, " ", 2) + if len(splitted) < 2 { + irc.Channels[msg.To].Say(fmt.Sprintf("Youtube search: %syt ", c.Identifier)) + return true + } else { + var query = flag.String("query", splitted[1], "Query string") + var maxResults = flag.Int64("max-results", 1, "Max results") + flag.Parse() + client := &http.Client{ Transport: &transport.APIKey{Key: ytApiKey}, } + service, err := youtube.New(client) + if err != nil { log.Printff("Error creating new YouTube client: %v", err); return true } + + // API call + call := service.Search.List("id,snippet"). + Q(*query). + MaxResults(*maxResults) + response, err := call.Do() + if err != nil { log.Printf("Error making search API call: %v", err); return true } + + // Parse response + var found bool = false + for _, item := range response.Items { + if item.Id.Kind == "youtube#video" { + var returnMsg string = item.Snippet.Title + " - https://youtu.be/" + item.Id.VideoId + found = true + log.Printf(returnMsg) + continue + } + } + + // Send to chat + if !found { + irc.Channels[msg.To].Say(fmt.Sprintf("No videos found!")) + return true + } else { + irc.Channels[msg.To].Say(fmt.Sprintf("[YouTube] %s", returnMsg)) + } + } + return true +} + func help(irc *hbot.IrcCon, msg *hbot.Message) bool { log.Printf("Running help command") keys := []string{} From 86194944c2a1cddaefd2bf24fef009971a8ac212 Mon Sep 17 00:00:00 2001 From: Gabriele Ficarelli Date: Wed, 8 Jul 2015 16:28:20 +0100 Subject: [PATCH 2/4] API key in global config, YT client initialized only at startup --- commands/commands.go | 24 ++++++++++++------------ main.go | 3 ++- tapiro.cfg | 1 + 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index db73a32..40e3ba9 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -21,14 +21,13 @@ type Commands struct { commands map[string]interface{} db *gorm.DB self string + ytapikey string } var ( c Commands ) -const ytApiKey = "DEVELOPER KEY HERE" - func init() { c = Commands{} c.commands = map[string]interface{}{ @@ -36,7 +35,7 @@ func init() { "seen": lastSeen, "tell": tell, "ack": ack, - "yt": yt, + "youtube": youtube, } } @@ -96,7 +95,7 @@ func lastSeen(irc *hbot.IrcCon, msg *hbot.Message) bool { return true } -func yt(irc *hbot.IrcCon, msg *hbot.Message) bool { +func youtube(irc *hbot.IrcCon, msg *hbot.Message) bool { log.Printf("Running yt command") splitted := strings.SplitN(msg.Content, " ", 2) if len(splitted) < 2 { @@ -106,12 +105,9 @@ func yt(irc *hbot.IrcCon, msg *hbot.Message) bool { var query = flag.String("query", splitted[1], "Query string") var maxResults = flag.Int64("max-results", 1, "Max results") flag.Parse() - client := &http.Client{ Transport: &transport.APIKey{Key: ytApiKey}, } - service, err := youtube.New(client) - if err != nil { log.Printff("Error creating new YouTube client: %v", err); return true } // API call - call := service.Search.List("id,snippet"). + call := ytservice.Search.List("id,snippet"). Q(*query). MaxResults(*maxResults) response, err := call.Do() @@ -124,16 +120,14 @@ func yt(irc *hbot.IrcCon, msg *hbot.Message) bool { var returnMsg string = item.Snippet.Title + " - https://youtu.be/" + item.Id.VideoId found = true log.Printf(returnMsg) + irc.Channels[msg.To].Say(fmt.Sprintf("[YouTube] %s", returnMsg)) continue } } - // Send to chat if !found { irc.Channels[msg.To].Say(fmt.Sprintf("No videos found!")) return true - } else { - irc.Channels[msg.To].Say(fmt.Sprintf("[YouTube] %s", returnMsg)) } } return true @@ -149,7 +143,7 @@ func help(irc *hbot.IrcCon, msg *hbot.Message) bool { return true } -func Configure(identifier string, dblocation string, self string, bot *hbot.IrcCon) (err error) { +func Configure(identifier string, dblocation string, ytapikey string, self string, bot *hbot.IrcCon) (err error) { c.Identifier = identifier c.self = self log.Printf("Command module running with command identifier %s", identifier) @@ -160,6 +154,12 @@ func Configure(identifier string, dblocation string, self string, bot *hbot.IrcC c.db = &db c.db.AutoMigrate(&LastSeen{}) c.db.AutoMigrate(&TellMessage{}) + c.ytapikey = ytapikey + ytclient := &http.Client{ Transport: &transport.APIKey{Key: c.ytapikey}, } + ytservice, err := youtube.New(ytclient) + if err != nil { + return err + } lastSeenTrigger := &hbot.Trigger{ func(mes *hbot.Message) bool { if mes.Name != self && mes.Command == "PRIVMSG" { diff --git a/main.go b/main.go index 2f5e558..4453605 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ var ( commandsConf = flag.NewFlagSet("commands", flag.ExitOnError) commandsIdentifier = commandsConf.String("identifier", "_", "Command prefix identifier") commandsDb = commandsConf.String("db", "_", "Db location") + commandsYtapikey = commandsConf.String("ytapikey", "_", "Youtube developer API key") ) var config globalconf.GlobalConf @@ -110,7 +111,7 @@ func main() { if err != nil { log.Fatalf("Configuration error: %s", err) } - err = commands.Configure(*commandsIdentifier, *commandsDb, *ircNickname, bot) + err = commands.Configure(*commandsIdentifier, *commandsDb, *commandsYtapikey, *ircNickname, bot) if err != nil { log.Fatalf("Configuration error: %s", err) } diff --git a/tapiro.cfg b/tapiro.cfg index f3d0f9e..b87dab5 100644 --- a/tapiro.cfg +++ b/tapiro.cfg @@ -25,3 +25,4 @@ channel = "" [commands] identifier = "!" db = "" +ytapikey = "" From 08d09092587eb2d0f45c92d38cc141fc818799c1 Mon Sep 17 00:00:00 2001 From: Gabriele Ficarelli Date: Wed, 8 Jul 2015 16:48:05 +0100 Subject: [PATCH 3/4] Added ytservice as global variable --- commands/commands.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/commands.go b/commands/commands.go index 40e3ba9..53acd20 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -26,6 +26,7 @@ type Commands struct { var ( c Commands + ytservice *youtube.Service ) func init() { @@ -156,7 +157,7 @@ func Configure(identifier string, dblocation string, ytapikey string, self strin c.db.AutoMigrate(&TellMessage{}) c.ytapikey = ytapikey ytclient := &http.Client{ Transport: &transport.APIKey{Key: c.ytapikey}, } - ytservice, err := youtube.New(ytclient) + ytservice, err = youtube.New(ytclient) if err != nil { return err } From df081563a4fdea35428c1e5e60fdfa3a789ca07f Mon Sep 17 00:00:00 2001 From: Gabriele Ficarelli Date: Wed, 8 Jul 2015 19:41:20 +0100 Subject: [PATCH 4/4] Minor logging/output text updates --- commands/commands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 53acd20..8e0f8fb 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -97,10 +97,10 @@ func lastSeen(irc *hbot.IrcCon, msg *hbot.Message) bool { } func youtube(irc *hbot.IrcCon, msg *hbot.Message) bool { - log.Printf("Running yt command") + log.Printf("Running youtube command") splitted := strings.SplitN(msg.Content, " ", 2) if len(splitted) < 2 { - irc.Channels[msg.To].Say(fmt.Sprintf("Youtube search: %syt ", c.Identifier)) + irc.Channels[msg.To].Say(fmt.Sprintf("Youtube search: %syoutube ", c.Identifier)) return true } else { var query = flag.String("query", splitted[1], "Query string")