From d16895bb183cfca8e716010141bbe86e58b2bc38 Mon Sep 17 00:00:00 2001 From: Stephen Yeargin Date: Tue, 12 May 2015 18:27:57 -0500 Subject: [PATCH 1/5] Switch to API Token-based search Fixes #3 Prior to this PR, the YouTube Hubot package would return a `devicesupport` link indicating that the search functionality was broken because it was using an outdated API. This moves the search to V3 of the YouTube API and restores functionality. It will require the addition of `YOUTUBE_API_KEY` environment variable. --- README.md | 6 ++++++ src/youtube.coffee | 32 +++++++++++++++++--------------- test/youtube-test.coffee | 5 +---- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index d6f8aa4..d2164aa 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,12 @@ Then add **hubot-youtube** to your `external-scripts.json`: ] ``` +Obtain a [Google Developer Console](https://console.developers.google.com) token. You will want the "Public" token rather than the OAuth credentials for this particular implementation. [Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) + +``` +export YOUTUBE_API_KEY= +``` + ## Sample Interaction ``` diff --git a/src/youtube.coffee b/src/youtube.coffee index 702c4e2..128cdaf 100644 --- a/src/youtube.coffee +++ b/src/youtube.coffee @@ -1,28 +1,30 @@ # Description: -# Messing around with the YouTube API. +# YouTube video search +# +# Configuration: +# 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.YOUTUBE_API_KEY + return msg.send "You must configure the 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' + maxResults: 15 q: query + key: process.env.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 From 736c4776896c67961266c1a5732294556de387e9 Mon Sep 17 00:00:00 2001 From: Stephen Yeargin Date: Thu, 14 May 2015 12:36:33 -0500 Subject: [PATCH 2/5] Filter to only videos (no playlists) --- src/youtube.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/youtube.coffee b/src/youtube.coffee index 128cdaf..994140b 100644 --- a/src/youtube.coffee +++ b/src/youtube.coffee @@ -15,6 +15,7 @@ module.exports = (robot) -> .query({ order: 'relevance' part: 'snippet' + type: 'video' maxResults: 15 q: query key: process.env.YOUTUBE_API_KEY From 9349fed6bb77758d8a01d48026de04763f2220b0 Mon Sep 17 00:00:00 2001 From: Stephen Yeargin Date: Thu, 28 May 2015 10:27:38 -0500 Subject: [PATCH 3/5] Clarify README --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d2164aa..a043552 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,24 @@ Then add **hubot-youtube** to your `external-scripts.json`: ] ``` -Obtain a [Google Developer Console](https://console.developers.google.com) token. You will want the "Public" token rather than the OAuth credentials for this particular implementation. [Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) +### 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 environment `YOUTUBE_API_KEY` variable. ``` export YOUTUBE_API_KEY= ``` +_[Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) about how to generate Google credentials._ + ## Sample Interaction ``` From 5df0eebe4cd70c59e3df3582b257374203c1339c Mon Sep 17 00:00:00 2001 From: Stephen Yeargin Date: Thu, 28 May 2015 10:28:44 -0500 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a043552..aeeed21 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Create a "Public" token rather than the OAuth credentials for this particular im ![Get Public Token](https://cloud.githubusercontent.com/assets/80459/7600553/f2fa44c2-f8d1-11e4-8edf-009c0e3f04f1.png) -Copy your token to the environment `YOUTUBE_API_KEY` variable. +Copy your token to the `YOUTUBE_API_KEY` environment variable. ``` export YOUTUBE_API_KEY= From dfd7a9c055549d76a849e71b0873301f1de50549 Mon Sep 17 00:00:00 2001 From: Stephen Yeargin Date: Sat, 6 Jun 2015 13:04:09 -0500 Subject: [PATCH 5/5] s/YOUTUBE_API_KEY/HUBOT_YOUTUBE_API_KEY/ --- README.md | 4 ++-- src/youtube.coffee | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aeeed21..26c2792 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,10 @@ Create a "Public" token rather than the OAuth credentials for this particular im ![Get Public Token](https://cloud.githubusercontent.com/assets/80459/7600553/f2fa44c2-f8d1-11e4-8edf-009c0e3f04f1.png) -Copy your token to the `YOUTUBE_API_KEY` environment variable. +Copy your token to the `HUBOT_YOUTUBE_API_KEY` environment variable. ``` -export YOUTUBE_API_KEY= +export HUBOT_YOUTUBE_API_KEY= ``` _[Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) about how to generate Google credentials._ diff --git a/src/youtube.coffee b/src/youtube.coffee index 994140b..98f550d 100644 --- a/src/youtube.coffee +++ b/src/youtube.coffee @@ -2,14 +2,14 @@ # YouTube video search # # Configuration: -# YOUTUBE_API_KEY - Obtained from https://console.developers.google.com +# 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) -> - unless process.env.YOUTUBE_API_KEY - return msg.send "You must configure the YOUTUBE_API_KEY environment variable" + 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({ @@ -18,7 +18,7 @@ module.exports = (robot) -> type: 'video' maxResults: 15 q: query - key: process.env.YOUTUBE_API_KEY + key: process.env.HUBOT_YOUTUBE_API_KEY }) .get() (err, res, body) -> videos = JSON.parse(body)