Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch videos from a YouTube Channel #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion meteor/app/private/Private.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import React from 'react';
import React, {useState, useEffect} from 'react';
import { useNavigate } from 'react-router-dom';

import { RoutePaths } from '../general/RoutePaths';

export const Private = () => {
const navigate = useNavigate();
const [videos, setVideos] = useState([]);

const goHome = () => {
navigate(RoutePaths.HOME);
};

const listVideosFromChannel = () => {
Meteor.call('youtube.listVideosFromChannel', { channelId: 'UCpEyIZJ6SAbsLwyfF9BTSiA' }, (err, res) => {
setVideos(res);
console.log(res)
});
}

useEffect(() => {
listVideosFromChannel();
}, []);

return (
<div className="flex flex-grow flex-col items-center">
<h2 className="mt-48 text-3xl font-extrabold tracking-tight text-gray-900 md:text-4xl">
Expand All @@ -23,6 +35,22 @@ export const Private = () => {
</a>
</div>
</h2>

{videos.map((video) => (
<div key={video.videoId} className="mt-5">
<a
href={video.link}
target="_blank"
rel="noreferrer"
className="text-base font-medium text-indigo-700 hover:text-indigo-600"
>
{video.title}
</a>
<p>{video.description}</p>
<img src={video.thumbnails.default.url} alt={video.title} />
<p>{video.likeStatus.isLiked ? 'Liked' : video.likeStatus.isUnliked ? 'Unliked' : '-'}</p>
</div>
))}
</div>
);
};
77 changes: 77 additions & 0 deletions meteor/app/youtube/youtubeMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Meteor } from 'meteor/meteor';

const { google } = require('googleapis');
const OAuth2 = google.auth.OAuth2;

async function getGoogleAPI({ userId }) {
const user = await Meteor.users.findOneAsync(userId);

const oauth2Client = new OAuth2(
Meteor.settings.google.clientId,
Meteor.settings.google.secret,
Meteor.settings.public.appUrl
);
const accessToken = user.services.google.accessToken;
oauth2Client.setCredentials({
access_token: accessToken,
});
return google.youtube({
version: 'v3',
auth: oauth2Client,
});
}
const getLikeStatus = async ({ googleAPI, videoId }) => {
try {
const videoLikes = await googleAPI.videos.getRating({
id: videoId,
// onBehalfOfContentOwner: userId // Replace with user's ID
});
const rating = videoLikes.data.items[0].rating;

if (rating === 'none') {
return {};
}
return { isLiked: rating === 'like', isUnliked: rating === 'dislike' };
} catch (e) {
console.error(`getLikeStatus error`, e);
return null;
}
};

const listVideosFromChannel = async function ({ channelId }) {
const { userId } = this;
if (!userId) {
throw new Meteor.Error('not-authorized');
}
const googleAPI = await getGoogleAPI({ userId });
const { data } = await googleAPI.search.list({
part: 'snippet',
channelId,
maxResults: 10,
});

const videos = data.items
.filter((item) => !!item.id.videoId)
.map(
({ id: { videoId }, snippet: { title, description, thumbnails } }) => ({
link: `https://www.youtube.com/watch?v=${videoId}`,
videoId,
title,
description,
thumbnails,
})
);
const videosToReturn = await Promise.allSettled(
videos.map(async (video) => {
video.likeStatus = await getLikeStatus({
googleAPI,
videoId: video.videoId,
});
return video;
})
);
return videosToReturn.map((video) => video.value);
};
Meteor.methods({
'youtube.listVideosFromChannel': listVideosFromChannel,
});
201 changes: 98 additions & 103 deletions meteor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions meteor/private/env/dev/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"public": {
"appUrl": "http://localhost:3000",
"appInfo": {
"name": "Meteor Template by quave"
}
Expand Down
2 changes: 2 additions & 0 deletions meteor/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Accounts } from 'meteor/accounts-base';

import '../app/youtube/youtubeSetup';

import '../app/youtube/youtubeMethods';

import '../app/clicks/clicksMethods';
import '../app/clicks/clicksPublishes';

Expand Down