Skip to content

Commit

Permalink
Fetch YouTube videos from YT API
Browse files Browse the repository at this point in the history
  • Loading branch information
max-programming committed Oct 19, 2022
1 parent e65aca2 commit 64513ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/pages/index.tsx
Expand Up @@ -8,9 +8,11 @@ import { Nav } from '@/components/Nav';
import { PostsHome } from '@/components/PostsHome';
import { SectionHeading } from '@/components/SectionHeading';
import { getPosts, Post } from '@/utils/fetchPosts';
import { getVideos, Video } from '@/utils/fetchVideos';

interface Props {
posts: Post[];
videos: Video[];
}

const Index = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
Expand Down Expand Up @@ -45,12 +47,14 @@ const Index = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
export const getStaticProps: GetStaticProps<Props> = async () => {
const SECONDS_IN_A_DAY = 86400;
const allPosts = await getPosts();
const videos = await getVideos();

const posts = allPosts.slice(0, 4);

return {
props: {
posts,
videos,
},
revalidate: SECONDS_IN_A_DAY,
};
Expand Down
45 changes: 45 additions & 0 deletions src/utils/fetchVideos.ts
@@ -0,0 +1,45 @@
const CHANNEL_ID = 'UC7LE4pbfb4e2voEASj3RscA';

interface Thumbnail {
url: string;
width: number;
height: number;
}

export interface Video {
id: string;
title: string;
description: string;
thumbnails: {
default: Thumbnail;
medium: Thumbnail;
high: Thumbnail;
};
}

export async function getVideos(): Promise<Array<Video>> {
const res = await fetch(
`https://www.googleapis.com/youtube/v3/search?key=${process.env.YOUTUBE_API_KEY}&channelId=${CHANNEL_ID}&part=snippet,id&order=date&maxResults=5`
);
const { items } = await res.json();
const videos: Array<Video> = items.map(({ id, snippet }: any) => {
return {
id: id.videoId,
title: snippet.title,
description: snippet.description,
thumbnails: snippet.thumbnails,
} as Video;
});
return videos;
}
// import Youtube from 'youtube.ts';

// const CHANNEL_URL = 'https://www.youtube.com/c/MaxProgramming';

// export default async function fetchVideos() {
// const yt = new Youtube(process.env.YOUTUBE_API_KEY!);
// const channel = await yt.channels.get(CHANNEL_URL);
// const videos = await yt.api.get()

// return channel;
// }

1 comment on commit 64513ab

@vercel
Copy link

@vercel vercel bot commented on 64513ab Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.