-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.service.js
57 lines (48 loc) · 1.5 KB
/
youtube.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const google = require('googleapis').google;
const credentials = require('./zuhaib-367615-e7443966834b.json');
const scopes = ['https://www.googleapis.com/auth/youtube'];
const auth = new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key,
scopes
);
const youtube = google.youtube({ version: 'v3', auth });
const youtube_parser= (url) =>{
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
let match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
const youtubeVideoDeatils = async (url)=>{
let obj = {}
const id = youtube_parser(url)
const res = await youtube.videos.list(
{
"part": [
"snippet"
],
"id": [
id
]
}
)
obj.Videotitle = res.data.items[0].snippet.title
obj.channelTitle = res.data.items[0].snippet.channelTitle
obj.channelId = res.data.items[0].snippet.channelId
const {data} = await youtube.channels.list({
"part": [
"statistics"
],
"id": [
res.data.items[0].snippet.channelId
]
})
obj.viewCount = data.items[0].statistics.viewCount;
obj.subscriberCount = data.items[0].statistics.subscriberCount;
obj.videoCount = data.items[0].statistics.videoCount;
return obj;
}
module.exports = {
youtube_parser,
youtubeVideoDeatils
}