Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active April 7, 2016 17:25
Show Gist options
  • Save bookercodes/f52d70ecdc92376bdb90af10072c1072 to your computer and use it in GitHub Desktop.
Save bookercodes/f52d70ecdc92376bdb90af10072c1072 to your computer and use it in GitHub Desktop.
// npm i moment youtube-api youtube-url
import {extractId} from 'youtube-url'
import youtubeApiClient from 'youtube-api'
import moment from 'moment'
export default class YouTube {
constructor(youtubeApiKey: string) {
youtubeApiClient.authenticate({
type: 'key',
key: this.apiKey
})
}
fetchVideoDetails(youtubeUrl) {
const videoId = extractId(youtubeUrl)
return new Promise((resolve, reject) => {
youtubeApiClient.videos.list({
id: videoId,
part: 'snippet,contentDetails'
}, function(err, details) {
const screencast = details.items.shift();
if (!screencast) {
reject({
name: 'YoutubeVideoDoesNotExist'
})
return
}
resolve({
id: videoId,
title: screencast.snippet.title,
description: screencast.snippet.description,
durationInSeconds: moment.duration(screencast.contentDetails.duration).asSeconds(),
channel: {
id: screencast.snippet.channelId,
title: screencast.snippet.channelTitle,
}
})
})
})
}
}
// npm i moment youtube-api youtube-url
import {extractId} from 'youtube-url'
import youtubeApiClient from 'youtube-api'
import moment from 'moment'
export function authenticate(key: string): any {
youtubeApiClient.authenticate({
type: 'key',
key
})
return function fetchVideoDetails(youtubeUrl) {
const videoId = extractId(youtubeUrl)
return new Promise((resolve, reject) => {
youtubeApiClient.videos.list({
id: videoId,
part: 'snippet,contentDetails'
}, function(err, details) {
const screencast = details.items.shift();
if (!screencast) {
reject({
name: 'YoutubeVideoDoesNotExist'
})
return
}
resolve({
id: videoId,
title: screencast.snippet.title,
description: screencast.snippet.description,
durationInSeconds: moment.duration(screencast.contentDetails.duration).asSeconds(),
channel: {
id: screencast.snippet.channelId,
title: screencast.snippet.channelTitle,
}
})
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment