Skip to content

Commit aae57be

Browse files
author
MiloNguyen95
committed
Basic video fetch
1 parent 5bab5e1 commit aae57be

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{ kind: 'youtube#playlistItem',
2+
etag: '"p4VTdlkQv3HQeTEaXgvLePAydmU/lBwcYm3PmDCF7c0jJvf9quujp1s"',
3+
id: 'VVVBMnRGazJOZHpPektwU0pkZjI0Y0FnLmU0T2lkZTlCc0g4',
4+
snippet: { publishedAt: '2020-01-04T16: 05: 16.000Z',
5+
channelId: 'UCA2tFk2NdzOzKpSJdf24cAg',
6+
title: 'Radix Sort Algorithm | Thuật toán sắp xếp theo cơ số',
7+
description:
8+
'Hế lô hế lô, Ông dev đây!\n\nTrong phần này mình sẽ giới thiệu và giải thích cơ chế hoạt động của Radix Sort Algorithm - thuật toán sắp xếp theo cơ số.\n\n--- Để xem những video về lập trình và gaming ---\nNhấn vào đây để theo dõi kênh mình nhé: https: //duyngd.com/Subscribe\n\n--- Blog của mình ---\nhttps://duyngd.com\n\n--- Facebook page của mình ---\nhttps://www.facebook.com/ongdevvuitinh\n\n--- Ủng hộ Ông Dev ---\nhttps://unghotoi.com/ongdev\n\nCảm ơn các bạn đã quan tâm theo dõi\n#ôngdev #algorithm #radixsort',
9+
thumbnails: [Object
10+
],
11+
channelTitle: 'Ông Dev',
12+
playlistId: 'UUA2tFk2NdzOzKpSJdf24cAg',
13+
position: 0,
14+
resourceId: [Object
15+
]
16+
}
17+
}

src/models/video.model.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mongoose from 'mongoose';
2+
// eslint-disable-next-line no-unused-vars
23
import PlaylistModel from './playlist.model';
34

45
const { Schema } = mongoose;
@@ -10,31 +11,23 @@ const VideoSchema = Schema({
1011
},
1112
title: String,
1213
description: String,
13-
tags: [
14-
{
15-
type: String,
16-
},
17-
],
18-
status: {
19-
privacyStatus: String,
20-
publishAt: Date,
21-
},
2214
statistics: {
2315
viewCount: Number,
2416
likeCount: Number,
2517
dislikeCount: Number,
2618
commentCount: Number,
2719
},
28-
thumbnails: {
20+
thumbnails: [{
2921
url: String,
3022
width: Number,
3123
height: Number,
24+
}],
25+
position: {
26+
type: Number,
27+
unique: true,
3228
},
33-
comments: [
34-
// Do not work on it for now
35-
],
36-
playlists: [{ type: PlaylistModel.id, ref: 'PlaylistModel' }],
37-
}, { _id: false });
29+
playlists: [{ type: String, ref: 'PlaylistModel' }],
30+
});
3831

3932
const VideoModel = mongoose.model('Videos', VideoSchema, 'videos');
4033

src/tasks/video.background.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cron from 'node-cron';
22
import dotenv from 'dotenv';
33

44
import YoutubeApi from '../configs/yt.config';
5+
import VideoModel from '../models/video.model';
56

67
dotenv.config();
78

@@ -19,9 +20,26 @@ YoutubeBackgroundTasks.getAllAvailableVideosFromYoutube = async () => {
1920
}
2021
};
2122

23+
YoutubeBackgroundTasks
24+
.saveAllVideosToDatabase = async (videos) => videos && videos.forEach(async (video) => {
25+
const {
26+
title, description, thumbnails, position,
27+
} = video.snippet;
28+
await VideoModel.deleteMany({});
29+
const newVideo = new VideoModel({
30+
id: video.id,
31+
title,
32+
description,
33+
thumbnails,
34+
position,
35+
});
36+
newVideo.save();
37+
});
38+
2239
// Request every 10 mins
23-
YoutubeBackgroundTasks.autoUpdateYoutubeVideos = cron.schedule('*/10 * * * *', () => {
24-
console.log('running a task every 10 minutes');
40+
YoutubeBackgroundTasks.autoUpdateYoutubeVideos = cron.schedule('*/10 * * * *', async () => {
41+
const videos = await YoutubeBackgroundTasks.getAllAvailableVideosFromYoutube();
42+
await YoutubeBackgroundTasks.saveAllVideosToDatabase(videos);
2543
});
2644

2745
export default YoutubeBackgroundTasks;

0 commit comments

Comments
 (0)