Last active
April 7, 2016 17:25
-
-
Save bookercodes/f52d70ecdc92376bdb90af10072c1072 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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, | |
} | |
}) | |
}) | |
}) | |
} | |
} |
This file contains hidden or 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
// 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