-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaddVideoToAstra.js
More file actions
45 lines (37 loc) · 1.1 KB
/
addVideoToAstra.js
File metadata and controls
45 lines (37 loc) · 1.1 KB
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
const mongoose = require("mongoose");
const { getYoutubeTranscript } = require('./getYoutubeTranscript')
const { generateEmbedding } = require("./generateEmbedding");
const { getYoutubeVideoInfo } = require("./getYoutubeVideoInfo");
const addVideoToAstra = async (url) => {
try {
const Video = mongoose.model("Video");
const videoUrl = url
console.log(`videoUrl`, videoUrl)
const existingVideo = await Video.findOne({ url: videoUrl });
if (existingVideo) {
console.log("Video already exists in the database");
return {
addedToAstra: false,
...existingVideo.toJSON()
}
} else {
let transcript = await getYoutubeTranscript(videoUrl)
let vector = await generateEmbedding(transcript)
let videoInfo = await getYoutubeVideoInfo(videoUrl)
let addedVideo = await Video.create({
...videoInfo,
url: videoUrl,
transcript,
$vector: vector
})
console.log("Video inserted into the database");
return {
addedToAstra: true,
...addedVideo.toJSON()
}
}
} catch (e) {
console.error(e);
}
};
module.exports = { addVideoToAstra }