Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added youtube search to commands #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ 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 {
Identifier string
commands map[string]interface{}
db *gorm.DB
self string
ytapikey string
}

var (
c Commands
ytservice *youtube.Service
)

func init() {
Expand All @@ -29,6 +36,7 @@ func init() {
"seen": lastSeen,
"tell": tell,
"ack": ack,
"youtube": youtube,
}
}

Expand Down Expand Up @@ -88,6 +96,44 @@ func lastSeen(irc *hbot.IrcCon, msg *hbot.Message) bool {
return true
}

func youtube(irc *hbot.IrcCon, msg *hbot.Message) bool {
log.Printf("Running youtube command")
splitted := strings.SplitN(msg.Content, " ", 2)
if len(splitted) < 2 {
irc.Channels[msg.To].Say(fmt.Sprintf("Youtube search: %syoutube <query>", 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()

// API call
call := ytservice.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)
irc.Channels[msg.To].Say(fmt.Sprintf("[YouTube] %s", returnMsg))
continue
}
}

if !found {
irc.Channels[msg.To].Say(fmt.Sprintf("No videos found!"))
return true
}
}
return true
}

func help(irc *hbot.IrcCon, msg *hbot.Message) bool {
log.Printf("Running help command")
keys := []string{}
Expand All @@ -98,7 +144,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)
Expand All @@ -109,6 +155,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" {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions tapiro.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ channel = ""
[commands]
identifier = "!"
db = ""
ytapikey = ""