Youtube music and video downloader

YouTubeAPI.js 918B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import 'whatwg-fetch';
  2. import querystring from 'querystring';
  3. _ = lodash;
  4. const KEY = 'AIzaSyDh641dLzxYexCASA3TdYMlc6zzS793ppQ';
  5. const ENDPOINT = 'https://www.googleapis.com/youtube/v3';
  6. const checkStatus = (response) => {
  7. if (response.status >= 200 && response.status < 300) {
  8. return response
  9. } else {
  10. let error = new Error(response.statusText);
  11. error.response = response;
  12. throw error
  13. }
  14. };
  15. const parseJson = response => response.json();
  16. export function YouTubeSearch(query) {
  17. const params = {
  18. part: 'snippet',
  19. type: 'video',
  20. key: KEY,
  21. q: query
  22. };
  23. return fetch(`${ENDPOINT}/search?${querystring.stringify(params)}`)
  24. .then(checkStatus)
  25. .then(parseJson);
  26. }
  27. export function YouTubeVideoInfo(id) {
  28. const params = {
  29. part: 'statistics,contentDetails',
  30. key: KEY,
  31. id: id
  32. };
  33. return fetch(`${ENDPOINT}/videos?${querystring.stringify(params)}`)
  34. .then(checkStatus)
  35. .then(parseJson);
  36. }