Skip to content

Commit

Permalink
feat: 람다 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
zsr12383 committed Dec 2, 2023
1 parent 2a5ea3f commit d7ea042
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ dist/
tmp/
.idea/
.env
lambda/
50 changes: 50 additions & 0 deletions lambda/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from 'axios';
const PREFIX_URL = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&key=";
const COUNT = 3;

async function getYoutubeComment(videoId) {
const url = PREFIX_URL + process.env.API_KEY + "&videoId=" + videoId + "&maxResults=100";
let { data } = await axios.get(url);
if(!data) {
return [];
}
let i = 1;
const ret = data.items.map((ele) => ele.snippet.topLevelComment.snippet);
while (data.nextPageToken && i < COUNT) {
const res = await axios.get(url + "&pageToken=" + data.nextPageToken);
data = res.data;
data.items.forEach((ele) => ret.push(ele.snippet.topLevelComment.snippet));
i++;
}
return ret;
}

export const handler = async(event) => {
const {queryStringParameters} = event;
let ret = [];

if(!queryStringParameters) {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "https://www.youtube.com",
"Access-Control-Allow-Methods": "OPTIONS,GET"
},
body: JSON.stringify(ret),
};
return response;
}

ret = await getYoutubeComment(queryStringParameters.videoId);
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "https://www.youtube.com",
"Access-Control-Allow-Methods": "OPTIONS,GET"
},
body: JSON.stringify(ret),
};
return response;
};

0 comments on commit d7ea042

Please sign in to comment.