-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from boostcampwm2023/feat/greeneye_api
Clova GreenEye API 요청
- Loading branch information
Showing
8 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '비밀'; | ||
|
||
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); | ||
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) => { | ||
const timestamps = Array.from( | ||
{ length: 4 }, | ||
() => `${Math.floor(Math.random() * 101)}%`, | ||
); | ||
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); | ||
}; | ||
|
||
const checkHarmful = (results) => { | ||
return results.some((result) => result.normal.confidence < 0.5); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters