Skip to content

Commit

Permalink
Merge pull request #4 from stephenyeargin/bug/3-broken-youtube-search
Browse files Browse the repository at this point in the history
Switch to API Token-based search
  • Loading branch information
technicalpickles committed Jun 6, 2015
2 parents 0b0e57a + dfd7a9c commit 61658df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your token>
```

_[Learn more](https://developers.google.com/console/help/new/?hl=en_US#generatingdevkeys) about how to generate Google credentials._

## Sample Interaction

```
Expand Down
33 changes: 18 additions & 15 deletions src/youtube.coffee
Original file line number Diff line number Diff line change
@@ -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 <query> - 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}"
5 changes: 1 addition & 4 deletions test/youtube-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 61658df

Please sign in to comment.