Created
May 6, 2016 02:33
-
-
Save ericholiveira/4e6fe70e071eea7ff50f6ecb4e03478f to your computer and use it in GitHub Desktop.
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
var Studio = require('studio'); | |
var request = require('request'); | |
var YOUTUBE_KEY = 'SOME_KEY'; | |
var YOUTUBE_REGEX = /https:\/\/www.youtube.com\/watch\?v=(\w*)/; | |
var youtubeModule = Studio.module('Youtube'); | |
var promisifiedRequest = Studio.promise.promisify(request); | |
youtubeModule(function fetch(url){ | |
var match = YOUTUBE_REGEX.exec(url); | |
if(match && match[1]){ | |
var id = match[1]; | |
return promisifiedRequest('https://www.googleapis.com/youtube/v3/videos?id='+id+'&part=snippet&key='+YOUTUBE_KEY).then(function(youtubeResponse){ | |
var body = JSON.parse(youtubeResponse.body); | |
var itemsResult = body.items; | |
if(itemsResult && itemsResult.length>0){ | |
var info = itemsResult[0]; | |
return { | |
title:info.snippet.title, | |
description:info.snippet.description | |
}; | |
} | |
}); | |
} | |
return null; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment