Youtube music and video downloader

YouTubeAPI.js 945B

1234567891011121314151617181920212223242526272829
  1. import { Meteor } from 'meteor/meteor';
  2. import 'whatwg-fetch';
  3. import querystring from 'querystring';
  4. let KEY = '';
  5. Meteor.call('security.key', (err, res) => KEY = res.key);
  6. const ENDPOINT = 'https://www.googleapis.com/youtube/v3';
  7. const checkStatus = response => {
  8. if (response.status >= 200 && response.status < 300) return response;
  9. let error = new Error(response.statusText);
  10. error.response = response;
  11. throw error
  12. };
  13. const parseJson = response => response.json();
  14. const YouTubeAPIQuery = ({ action, params}) =>
  15. fetch(`${ENDPOINT}/${action}?${querystring.stringify(params)}`)
  16. .then(checkStatus)
  17. .then(parseJson);
  18. const YouTubeSearch = query => YouTubeAPIQuery({ action: 'search',
  19. params: { part: 'snippet', type: 'video', key: KEY, q: query }});
  20. const YouTubeVideoInfo = id => YouTubeAPIQuery({ action: 'videos',
  21. params: { part: 'statistics,contentDetails', key: KEY, id: id }});
  22. export { YouTubeSearch, YouTubeVideoInfo };