Youtube music and video downloader

YouTubeAPI.js 906B

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