Advertisement
elliottchong

lib/youtube.ts

Aug 20th, 2023
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. export async function searchYouTube(searchQuery: string) {
  2. searchQuery = searchQuery.replaceAll(" ", "+");
  3. console.count("youtube search");
  4. const response = await fetch(
  5. `https://www.googleapis.com/youtube/v3/search?key=${process.env.YOUTUBE_API}&q=${searchQuery}&videoDuration=medium&videoEmbeddable=true&type=video&maxResults=5`,
  6. {
  7. method: "GET",
  8. }
  9. );
  10. const json = await response.json();
  11. if (!json) {
  12. console.log("youtube fail");
  13. return null;
  14. }
  15. if (json.items[0] == undefined) {
  16. console.log("youtube fail");
  17. return null;
  18. }
  19. return json.items[0].id.videoId;
  20. }
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement