-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from stephenyeargin/bug/3-broken-youtube-search
Switch to API Token-based search
- Loading branch information
Showing
3 changed files
with
37 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters