diff --git a/README.md b/README.md index d6f8aa4..26c2792 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,24 @@ Then add **hubot-youtube** to your `external-scripts.json`: ] ``` +### Obtain a [Google Developer Console](https://console.developers.google.com) token + +Enable the "YouTube Data API v3" permission from the API menu. + +![Enable v3](https://cloud.githubusercontent.com/assets/80459/7863722/8161df38-0523-11e5-931a-5c2bf6d8105b.png) + +Create a "Public" token rather than the OAuth credentials for this particular implementation. + +![Get Public Token](https://cloud.githubusercontent.com/assets/80459/7600553/f2fa44c2-f8d1-11e4-8edf-009c0e3f04f1.png) + +Copy your token to the `HUBOT_YOUTUBE_API_KEY` environment variable. + +``` +export HUBOT_YOUTUBE_API_KEY= +``` + +_[Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) about how to generate Google credentials._ + ## Sample Interaction ``` diff --git a/src/youtube.coffee b/src/youtube.coffee index 702c4e2..98f550d 100644 --- a/src/youtube.coffee +++ b/src/youtube.coffee @@ -1,28 +1,31 @@ # Description: -# Messing around with the YouTube API. +# YouTube video search +# +# Configuration: +# HUBOT_YOUTUBE_API_KEY - Obtained from https://console.developers.google.com # # Commands: # hubot youtube me - Searches YouTube for the query and returns the video embed link. module.exports = (robot) -> - robot.respond /(youtube|yt)( me)? (.*)/i, (msg) -> - query = msg.match[3] - robot.http("http://gdata.youtube.com/feeds/api/videos") + robot.respond /(?:youtube|yt)(?: me)? (.*)/i, (msg) -> + unless process.env.HUBOT_YOUTUBE_API_KEY + return msg.send "You must configure the HUBOT_YOUTUBE_API_KEY environment variable" + query = msg.match[1] + robot.http("https://www.googleapis.com/youtube/v3/search") .query({ - orderBy: "relevance" - 'max-results': 15 - alt: 'json' + order: 'relevance' + part: 'snippet' + type: 'video' + maxResults: 15 q: query + key: process.env.HUBOT_YOUTUBE_API_KEY }) .get() (err, res, body) -> videos = JSON.parse(body) - videos = videos.feed.entry + videos = videos.items - unless videos? - msg.send "No video results for \"#{query}\"" - return + unless videos? && videos.length > 0 + return msg.send "No video results for \"#{query}\"" video = msg.random videos - video.link.forEach (link) -> - if link.rel is "alternate" and link.type is "text/html" - msg.send link.href - + msg.send "https://www.youtube.com/watch?v=#{video.id.videoId}" diff --git a/test/youtube-test.coffee b/test/youtube-test.coffee index edced38..ca59552 100644 --- a/test/youtube-test.coffee +++ b/test/youtube-test.coffee @@ -13,7 +13,4 @@ describe 'youtube', -> require('../src/youtube')(@robot) it 'registers a respond listener', -> - expect(@robot.respond).to.have.been.calledWith(/hello/) - - it 'registers a hear listener', -> - expect(@robot.hear).to.have.been.calledWith(/orly/) + expect(@robot.respond).to.have.been.calledWith(/(?:youtube|yt)(?: me)? (.*)/i) \ No newline at end of file