-
Notifications
You must be signed in to change notification settings - Fork 1
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
Clova GreenEye API 요청 #241
Changes from all commits
2d110fb
03e42ba
67e61c1
9ced021
d759b1d
b302533
a6f4656
ba5c87f
b59dc19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { ErrorCode } from 'src/exceptions/enum/exception.enum'; | ||
import { BaseException } from './base.exception'; | ||
|
||
export class GreenEyeActionFailException extends BaseException { | ||
constructor() { | ||
super(ErrorCode.GreenEyeActionFail, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { ErrorCode } from 'src/exceptions/enum/exception.enum'; | ||
import { BaseException } from './base.exception'; | ||
|
||
export class GreenEyeApiFailException extends BaseException { | ||
constructor() { | ||
super(ErrorCode.GreenEyeApiFail, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const ffmpeg = require('fluent-ffmpeg'); | ||
const fs = require('fs'); | ||
const axios = require('axios'); | ||
|
||
const apiUrl = '비밀'; | ||
5tarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async function main(params) { | ||
const results = []; | ||
const { videoUrl, object_name, greenEyeSecret, accessToken } = params; | ||
await createScreanshots(videoUrl); | ||
const files = Array.from( | ||
{ length: 4 }, | ||
(_, k) => `${object_name}_${k + 1}.png`, | ||
); | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const file of files) { | ||
const data = fs.readFileSync(file); | ||
const imageData = data.toString('base64'); | ||
// eslint-disable-next-line no-await-in-loop | ||
const result = await greenEye(file, file, imageData, greenEyeSecret); | ||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for문에서는 await를 하지 않는게 권장이군여 |
||
results.push(result); | ||
fs.unlinkSync(file); | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 1000); | ||
}); | ||
} | ||
console.log(results); | ||
if (checkHarmful(results)) { | ||
axios.delete(`http://223.130.136.106/videos/${object_name}`, { | ||
headers: { Authorization: `Bearer ${accessToken}` }, | ||
}); | ||
} | ||
} | ||
|
||
const createScreanshots = async (videoUrl) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수 내에서 await를 안하면 async를 안붙여도 되는데 이 함수처럼 promise를 리턴하는 경우 명시성을 위해 일부러 붙여주기도 하더라고요! 그런 점을 고려하신건지 궁금해요~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흠 아마 깊게 고려하진 않았던 것 같아요 |
||
const timestamps = Array.from( | ||
{ length: 4 }, | ||
() => `${Math.floor(Math.random() * 101)}%`, | ||
); | ||
5tarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(timestamps); | ||
return new Promise((resolve) => { | ||
ffmpeg(videoUrl) | ||
.screenshots({ | ||
timestamps, | ||
filename: `%b.png`, | ||
folder: './', | ||
}) | ||
.on('end', () => { | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
|
||
const greenEye = (requestId, imageName, imageData, greenEyeSecret) => { | ||
const headers = { | ||
'X-GREEN-EYE-SECRET': greenEyeSecret, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
const data = { | ||
version: 'V1', | ||
requestId, | ||
timestamp: Number(new Date()), | ||
images: [ | ||
{ | ||
name: imageName, | ||
data: imageData, | ||
}, | ||
], | ||
}; | ||
|
||
return axios | ||
.post(apiUrl, data, { | ||
headers, | ||
}) | ||
.then((response) => response.data.images.pop().result); | ||
5tarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
const checkHarmful = (results) => { | ||
return results.some((result) => result.normal.confidence < 0.5); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import axios from 'axios'; | ||
import { GreenEyeApiFailException } from 'src/exceptions/greeneye-api-fail.exception'; | ||
|
||
export const greenEye = ( | ||
requestId: string, | ||
imageName: string, | ||
imageUrl: string, | ||
) => { | ||
const apiUrl = process.env.GREENEYE_API_URL; | ||
|
||
const headers = { | ||
'X-GREEN-EYE-SECRET': process.env.GREENEYE_SECRET, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
const data = { | ||
version: 'V1', | ||
requestId, | ||
timestamp: Number(new Date()), | ||
images: [ | ||
{ | ||
name: imageName, | ||
url: imageUrl, | ||
}, | ||
], | ||
}; | ||
|
||
return axios | ||
.post(apiUrl, data, { | ||
headers, | ||
}) | ||
.then((response) => response.data.images.pop().result) | ||
.catch(() => { | ||
throw new GreenEyeApiFailException(); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 이건 token 추가하려고 이렇게 했었어요 ㅎㅎ