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

[feat]더미 추가 #223

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
168 changes: 108 additions & 60 deletions src/controllers/summary.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { BaseError } from "../../config/error.js";
import { response } from "../../config/response.js";
import { status } from "../../config/response.status.js";
import axios from 'axios';
import { isScript } from '../models/video.dao.js';
import { viewVideo } from '../services/video.service.js';


export const summary = async (req, res) => {
Expand All @@ -15,84 +17,130 @@ export const summary = async (req, res) => {
//사용자에게 입력받을 video ID 변수
const videoId=req.body.videoId;

const isScriptData="https://www.youtube.com/embed/"+videoId;
const isScriptResult=await isScript(isScriptData);
console.log("찾기여부",isScriptResult[0]);
if (!(isScriptResult[0] && Array.isArray(isScriptResult[0]) && isScriptResult[0].length >0)) {
const responseData = await axios.get(`https://www.googleapis.com/youtube/v3/videos?id=${videoId}&key=${process.env.YOUTUBE_API_KEY}&part=snippet`);
console.log( responseData.data.items[0]?.snippet);


const responseData =
await axios.get(`https://www.googleapis.com/youtube/v3/videos?id=${videoId}&key=${process.env.YOUTUBE_API_KEY}
&part=snippet`);
console.log( responseData.data.items[0]?.snippet);

const videoTitle=responseData.data.items[0]?.snippet.title;
const youtubeDate=responseData.data.items[0]?.snippet.publishedAt;


// Object Storage에서 스크립트 파일 이름 가져오기
const scriptFileName = await getScriptFileName(process.env.OBJECT_STORAGE_BUCKET_SUMMARY_NAME, videoId);
const videoTitle=responseData.data.items[0]?.snippet.title;
const youtubeDate=responseData.data.items[0]?.snippet.publishedAt;


if (scriptFileName) {
// 스크립트 파일이 이미 존재하면 해당 데이터 사용
const jsonData = await readFileFromObjectStorage(process.env.OBJECT_STORAGE_BUCKET_SUMMARY_NAME, scriptFileName);
// Object Storage에서 스크립트 파일 이름 가져오기
const scriptFileName = await getScriptFileName(process.env.OBJECT_STORAGE_BUCKET_SUMMARY_NAME, videoId);

const scriptText = jsonData.text;
const timeStampData=[];
for(let i=0;i<jsonData.segments.length;i++){
timeStampData.push({
"start_time":jsonData.segments[i].start/1000,
"end_time":jsonData.segments[i].end/1000,
"text":jsonData.segments[i].text
})
}
console.log("시간데이터 있는 데이터",timeStampData);
// const tempResult=await fineTunningData(JSON.stringify(timeStampData));
console.log(scriptText);
if (scriptFileName) {
// 스크립트 파일이 이미 존재하면 해당 데이터 사용
const jsonData = await readFileFromObjectStorage(process.env.OBJECT_STORAGE_BUCKET_SUMMARY_NAME, scriptFileName);

const summaryResult = await getSummary(scriptText);
console.log("요약데이터",summaryResult);
// const startSummaryIndex = summaryResult.indexOf('{'); // 첫 번째 '{'의 인덱스 찾기
// console.log("찾은 인덱스",startSummaryIndex);
// const trimmedSummaryResponse = summaryResult.substring(startSummaryIndex);
const summaryData=JSON.parse(summaryResult);
console.log("summary json 데이터",summaryData);
//gpt 데이터
const gptResponse = await chatGPTCall(scriptText);
console.log("gpt받아온 데이터",gptResponse);
const startIndex = gptResponse.indexOf('{'); // 첫 번째 '{'의 인덱스 찾기
const trimmedResponse = gptResponse.substring(startIndex);
const gptData=JSON.parse(trimmedResponse);
const scriptText = jsonData.text;
const timeStampData=[];
for(let i=0;i<jsonData.segments.length;i++){
timeStampData.push({
"start_time":jsonData.segments[i].start/1000,
"end_time":jsonData.segments[i].end/1000,
"text":jsonData.segments[i].text
})
}
//console.log("시간데이터 있는 데이터",timeStampData);
// const tempResult=await fineTunningData(JSON.stringify(timeStampData));
console.log(scriptText);


const summaryResult = await getSummary(scriptText);
//console.log("요약데이터",summaryResult);
// const startSummaryIndex = summaryResult.indexOf('{'); // 첫 번째 '{'의 인덱스 찾기
// console.log("찾은 인덱스",startSummaryIndex);
// const trimmedSummaryResponse = summaryResult.substring(startSummaryIndex);
const summaryData=JSON.parse(summaryResult);
console.log("summary json 데이터",summaryData);
//gpt 데이터
const gptResponse = await chatGPTCall(scriptText);
console.log("gpt받아온 데이터",gptResponse);
const startIndex = gptResponse.indexOf('{'); // 첫 번째 '{'의 인덱스 찾기
const trimmedResponse = gptResponse.substring(startIndex);
const gptData=JSON.parse(trimmedResponse);

// //유튜브 제목 요약
// const titleData=await getTitle(videoTitle);


//맵핑
const data=await timeStampMapping(gptData,timeStampData);
console.log("돌아온 데이터",data);
console.log(summaryData.video_name);
const tagData=await splitTag(gptData.tag);

// //유튜브 제목 요약
// const titleData=await getTitle(videoTitle);


//맵핑
const data=await timeStampMapping(gptData,timeStampData);
console.log("돌아온 데이터",data);
console.log(summaryData.video_name);
const tagData=await splitTag(gptData.tag);


//데이터 가공
//데이터 가공
const finalData={
"title":videoTitle,
"youtube_created_at":youtubeDate,
"link":"https://www.youtube.com/embed/"+videoId,
"description":summaryData.video_name[0].name,
"subheading":data,
"summary":summaryData.Summary,
"tag":tagData
}
res.send(response(status.SUCCESS,{
message: 'File processed successfully using existing data',
finalData
}));


} else {
// 스크립트 파일이 없으면 오류 호출
res.status(404).json({ message: 'Script file not found' });
}
}else{
const data= {
"userID":"41",
"videoID":isScriptResult[0][0].id,
"version":"original"
};
const result= await viewVideo(data);
const subHeadingData=[]
const summaryData=[]
const tagData=[]
console.log(result);
for(let i=0; i<result.subHeading.length;i++){
subHeadingData.push({
"name":result.subHeading[i].name,
"start_time":result.subHeading[i].start_time,
"end_time":result.subHeading[i].end_time,
"content":result.subHeading[i].content
})
}
for(let i=0; i<result.summary.length;i++){
summaryData.push({
"content":result.summary[i].content,

})
}
for(let i=0; i<result.tag.length;i++){
tagData.push({
"name":result.tag[i].name,
})
}
const finalData={
"title":videoTitle,
"youtube_created_at":youtubeDate,
"title":result.title,
"youtube_created_at":result.youtube_created_at,
"link":"https://www.youtube.com/embed/"+videoId,
"description":summaryData.video_name[0].name,
"subheading":data,
"summary":summaryData.Summary,
"description":result.description,
"subheading":subHeadingData,
"summary":summaryData,
"tag":tagData
}
res.send(response(status.SUCCESS,{
message: 'File processed successfully using existing data',
finalData
}));


} else {
// 스크립트 파일이 없으면 오류 호출
res.status(404).json({ message: 'Script file not found' });
}


} catch (error) {
res.status(500).json({ message: 'Error in processing video', error: error.toString() });
console.log(error);
Expand Down
13 changes: 12 additions & 1 deletion src/models/video.dao.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pool } from "../../config/db.connect.js";
import { BaseError } from "../../config/error.js";
import { status } from "../../config/response.status.js";
import {getVideoSql,getEntireVideoSql,getSubHeadingSql,getSummarySql,getCategorySql,getTagSql,insertVideoOriginSql,insertVideoRevisionSql,connectSubheading,connectSummary,connectTag,connectVideoTag,deleteVideoTagSql,deleteTagSql,deleteSubheadingSql,deleteSummarySql,deleteVideoSql,updateVideoSql,updateSummarySql,updateSubheadingSql,setTimeSql, entireTagSql, addCopyRevisonVideoSql, findTagSql, getTagIdSql} from "../models/video.sql.js"
import {getVideoSql,getEntireVideoSql,getSubHeadingSql,getSummarySql,getCategorySql,getTagSql,insertVideoOriginSql,insertVideoRevisionSql,connectSubheading,connectSummary,connectTag,connectVideoTag,deleteVideoTagSql,deleteTagSql,deleteSubheadingSql,deleteSummarySql,deleteVideoSql,updateVideoSql,updateSummarySql,updateSubheadingSql,setTimeSql, entireTagSql, addCopyRevisonVideoSql, findTagSql, getTagIdSql, findUrlSq} from "../models/video.sql.js"
import {getSimpleVideoWithVideoSql,updateCategorySql,getUnReadDummyVideoSql,getCategoryNameSql,getRecentVideoSql,insertDummyVideoSql,removeSummarySql} from "../models/video.sql.js";
import {addCopyVideoSql,copySubheadingSql,copySummarySql,copyTagSql} from "../models/video.sql.js"

Expand Down Expand Up @@ -66,6 +66,17 @@ export const addCopyVideo= async(data)=>{
throw new BaseError(status.PARAMETER_IS_WRONG);
}
}
export const isScript=async(url)=>{
try {
const conn =await pool.getConnection();
const isState=await pool.query(findUrlSq,[url]);

conn.release();
return isState;
} catch (error) {

}
}
export const getSimpleVideo=async (req)=>{
try{
const conn =await pool.getConnection();
Expand Down
1 change: 1 addition & 0 deletions src/models/video.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const connectSummary="insert into summary(content, video_id, version_id)
export const connectTag="INSERT INTO tag(name) VALUES (?);";
export const connectVideoTag="insert into video_tag(video_id, tag_id, version_id) VALUES (?,?,?);";
export const findTagSql="select EXISTS (select * from tag where name=? limit 1) as success;"
export const findUrlSq="select distinct id from video where link=? and user_id=41 ;"
export const getTagIdSql="select id from tag where name=?;";
//video delete 관련 sql

Expand Down
1 change: 1 addition & 0 deletions src/services/video.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const viewVideo=async(data)=>{
const getTagData = await getTag(data);
const setTimeData=await setReadTime(data,time);
let categoryData={};
console.log("받아온 비디오정보",getVideoData[0]);
if(getVideoData[0].category_id){
categoryData=await getCategoryName(data.userID,getVideoData[0].category_id);
}else{
Expand Down
Loading