forked from atilaahmettaner/youtube-subtitle-summarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
261 lines (230 loc) · 8.57 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import express from "express";
import bodyParser from "body-parser";
import scraper from "youtube-captions-scraper";
import dotenv from "dotenv"
import fetch from "node-fetch";
import analytics from "express-google-analytics"
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.set("view engine", "ejs");
import {Configuration, OpenAIApi} from "openai";
import { google } from 'googleapis';
const apiKey="AIzaSyAL3i_PgYjgVBr7jViU4ffxfhwfsYPSuRo"
const youtube = google.youtube({
version: 'v3',
auth: apiKey
});
async function fetchComments(videoId) {
const { data } = await youtube.commentThreads.list({
part: 'snippet,replies',
videoId,
key: apiKey,
maxResults: 100
});
let comments = data.items.map(item => item.snippet.topLevelComment.snippet.textDisplay);
while (data.nextPageToken) {
const { data: newData } = await youtube.commentThreads.list({
part: 'snippet,replies',
videoId,
key: apiKey,
maxResults: 100,
pageToken: data.nextPageToken
});
comments = comments.concat(newData.items.map(item => item.snippet.topLevelComment.snippet.textDisplay));
data.nextPageToken = newData.nextPageToken;
}
return comments;
}
function filterCommentsByTime(comments) {
const regex = /(\d{1,2}:\d{2})/g;
const matches = comments.map((comment) => {
const match = comment.match(regex);
return {
time: match ? match[0] : null,
text: comment,
};
});
return matches.filter((comment) => comment.time !== null);
}
const configuration = new Configuration({
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
app.use(analytics(process.env.GOOGLE_ANALYTICS_TRACKING_ID));
function removeStartingSentences(paragraph) {
let sentences = paragraph.split(". ");
let startIndex = 0;
for (let i = 0; i < sentences.length; i++) {
if (sentences[i].charAt(0) === sentences[i].charAt(0).toUpperCase()) {
startIndex = i;
break;
}
}
return sentences.slice(startIndex).join(". ");
}
async function getAiResponse(topic) {
try {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: topic,
max_tokens: 2024,
n: 1,
stop: null,
temperature: 0.7
});
return removeStartingSentences(completion.data.choices[0].text)
} catch (e) {
console.error(e)
}
}
let result = ""
let longResult = ""
let summ = ""
let aiSummary = ""
let aiSummarys = ""
let title = ""
app.use(bodyParser.urlencoded({extended: true}), express.static("public"));
app.get("/", (req, res) => {
res.render("home", {input: null});
});
let myData = []
let secondstoMin = []
app.post("/summarize", async (req, res) => {
try {
let algo = {}
let myData = []
let errorMessage;
const link = req.body.input;
const lang = req.body["selectedLanguage"];
const input = getYouTubeId(link)
let newData = []
if (input === "error") {
console.log("err")
} else {
/*https://yt.lemnoslife.com/noKey/videos?part=snippet&id=XqVC-lBeG2U*/
fetch(`https://yt.lemnoslife.com/videos?part=mostReplayed&id=${input}`)
.then(response => response.json())
.then(async data => {
newData = data
if (!data.items || data.items.length === 0) {
const errorMessage = 'Hatalı bir YouTube bağlantısı girdiniz!';
algo.errorMessage = errorMessage;
return res.render('index', {algo});
}
function millisToMinutesAndSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
const heatMarkers = newData.items[0];
if (!heatMarkers['mostReplayed'] || !heatMarkers['mostReplayed']['markers'] || heatMarkers['mostReplayed']['markers'].length === undefined) {
myData.push('0:00')
} else {
for (let i = 0; i < heatMarkers['mostReplayed']['markers'].length; i++) {
if (heatMarkers['mostReplayed']['markers'][i]['intensityScoreNormalized'] > 0.8) {
console.log(heatMarkers['mostReplayed']['markers'][i]['startMillis'])
myData.push(millisToMinutesAndSeconds(heatMarkers['mostReplayed']['markers'][i]['startMillis']))
}
}
}
console.log(myData)
const comments = await fetchComments(`${input}`);
const timeStamps = filterCommentsByTime(comments);
console.log(timeStamps.map((comment) => comment.time));
const secondsData = convertToSeconds(myData)
secondstoMin = secondsData.map(Number)
summ = await subConv(`${input}`, secondsData, `${lang}`)
title = await getTitle(input)
const text = summ.longResult;
aiSummarys = await langDet(lang, summ.result)
algo = {
timeStamps: timeStamps,
aiSummary: aiSummarys,
title: title,
summ: summ.result,
longSub: text,
input: input,
myData: myData,
secondsData: secondsData,
secondstoMin: secondstoMin
};
res.render("index", {algo: algo});
})
}
} catch (error) {
console.log('error:', error.message);
const errorMessage = 'error';
return res.render('index', {message: errorMessage});
}
});
function convertToSeconds(array) {
let result = [];
for (let item of array) {
let parts = item.split(':').map(Number);
let minutes = parts [0];
let seconds = parts [1];
let totalSeconds = minutes * 60 + seconds;
result.push(totalSeconds.toFixed(3));
}
return result;
}
app.listen(port, () => {
console.log(`app is running http://localhost:${port} `);
});
const subConv = async (id, myData, lang) => {
try {
return await scraper.getSubtitles({
videoID: id,
lang: lang
}).then(function (captions) {
const filteredCaptions = captions.filter(caption => {
const start = parseFloat(caption.start);
return myData.some(time => time >= start - 15 && time <= start + 15);
});
const mostReplayed = filteredCaptions.map(caption => caption.text);
const allCaption = captions.map(caption => caption.text);
result = mostReplayed.join(" ");
longResult = allCaption.join(" ");
return {result, longResult};
})
} catch (error) {
console.error(error);
return {result: `${lang} subtitle not found`, longResult: `${lang} subtitle not found`};
}
};
function getYouTubeId(link) {
let id;
if (link.includes("youtube.com/watch")) {
id = link.split("v=")[1];
const ampersandPosition = id.indexOf("&");
if (ampersandPosition !== -1) {
id = id.substring(0, ampersandPosition);
}
} else if (link.includes("youtu.be/")) {
id = link.split("youtu.be/")[1];
const slashPosition = id.indexOf("/");
if (slashPosition !== -1) {
id = id.substring(0, slashPosition);
}
} else {
id = "error"
}
return id;
}
async function langDet(lang, res) {
if (lang === "tr") {
aiSummary = await getAiResponse(`Bu video transkripsiyonunu özetleyebilir misin:${res}.`)
} else if (lang === "en") {
aiSummary = await getAiResponse(`summarize the transcription of this video?:${res}.`)
} else if (lang === "es") {
aiSummary = await getAiResponse(`Hazme un resumen de la transcripción de este video?:${res}.`)
}
return aiSummary
}
async function getTitle(id) {
const response = await fetch(`https://yt.lemnoslife.com/noKey/videos?part=snippet&id=${id}`);
const data = await response.json();
const title = data.items[0].snippet.title;
return title;
}