[์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 4๋‹จ๊ณ„, ์œ ํŠœ๋ธŒ(Youtube) API ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋กœ ์˜์ƒ ๊ฐ€์ ธ์˜ค๊ธฐ , google app script
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

[์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 4๋‹จ๊ณ„, ์œ ํŠœ๋ธŒ(Youtube) API ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋กœ ์˜์ƒ ๊ฐ€์ ธ์˜ค๊ธฐ , google app script

by ์ผ์ƒ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค 2022. 4. 28.

์นด์นด์˜ค๋ทฐ ์ž๋™ ๋ฐœํ–‰์„ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ๋‹ค์Œ ๋‰ด์Šค N๊ฐœ, ๋„ค์ด๋ฒ„ ๋‰ด์Šค N๊ฐœ, ํ‹ฐ์Šคํ† ๋ฆฌ N๊ฐœ, ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ N๊ฐœ, ์œ ํŠœ๋ธŒ N๊ฐœ ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜์—ฌ ๋ฐœํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ƒ๊ฐํ•˜์˜€๋‹ค. ๋งค์ผ 00:00๋ฉด ์ˆ˜๋™์œผ๋กœ playlist ๋ณด๋“œ๋ฅผ ๋ฐœํ–‰ํ•˜์˜€๋Š”๋ฐ ์ด๊ฒƒ์„ ์ด์ œ ์ž๋™์œผ๋กœ ํ•  ์˜ˆ์ •์ด๋‹ค.

Google cloud console์— ์ ‘์†ํ•˜์—ฌ ์œ ํŠœ๋ธŒ api ์‚ฌ์šฉ์„ ํ—ˆ์šฉ

๋งํฌ : https://console.cloud.google.com/apis/library/youtube.googleapis.com?hl=ko 

 ๊ด€๋ฆฌ์— ๋“ค์–ด๊ฐ€์„œ ์ธ์ฆ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. api key๋ฅผ ๋ฐœ๊ธ‰ํ•œ๋‹ค๋Š” ์–˜๊ธฐ์ด๋‹ค. ์ œํ•œ ์‚ฌํ•ญ์„ ๊ฑธ์–ด๋‘๋ฉด ๋ณด์•ˆ์ƒ ์ข‹๋‹ค๊ณ  ํ•˜์—ฌ ๊ฑธ์—ˆ๋‹ค.

๋ฐœ๊ธ‰๋ฐ›์€ api key๋ฅผ ์•„๋ž˜ ์†Œ์Šค์—์„œ ์ ์šฉํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•
function getYoutube(playlist){
  var API_KEY = 'apikey';

  var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + playlist 
  + "&eventType=completed" //์ด๋ฏธ ์—…๋กœ๋“œ ๋œ 
  + "&order=date&type=video" //๋‚ ์งœ์ˆœ ์ •๋ ฌ ๋ฐ type์€ video
  + "&videoDefinition=high" //๊ณ ํ™”์งˆ
  + "&videoEmbeddable=true" //์‚ฌ์šฉ ํ—ˆ์šฉ
  + "&maxResults=2" //๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ˆ˜
  + "&regionCode=KR" //๊ตญ๊ฐ€ ์ฝ”๋“œ
  + "&videoDimension=2d" //3d ์˜์ƒ ์ œ์™ธ ํ•˜๊ธฐ ์œ„ํ•ด, 2d๋งŒ
  + "&videoDuration=long" //20๋ถ„ ์ด์ƒ์˜ ๊ณก
  + "&key="+ API_KEY ; //๋‚˜์˜ ๊ตฌ๊ธ€ API KEY

  var response = UrlFetchApp.fetch(url);

  var resData = response.getContentText();
  resData = JSON.parse(resData);

  
  var arrInfo = new Array();
  var arrTitle = new Array();
  var arrLink = new Array();
  var arrContent = new Array();
  var arrDate = new Array();

  for(var i = 0 ; i < 1; i ++ )
  {   
      arrTitle.push(replaceTitle(removeEmojis(resData.items[i].snippet.title)));
      arrLink.push("https://www.youtube.com/watch?v="+ resData.items[i].id.videoId);
      arrContent.push(replaceTitle(removeEmojis(resData.items[i].snippet.description)));
      arrDate.push(resData.items[i].snippet.publishedAt);
  }

  arrInfo.push(arrTitle);
  arrInfo.push(arrLink);
  arrInfo.push(arrContent);
  arrInfo.push(arrDate);


  var transposed = Array.from({ length: arrTitle.length }, () => new Array(2).fill(0)) 

  for (var o=0; o< arrInfo.length; o++) {
    for (var v=0; v<arrInfo[0].length; v++) { 
      [transposed[v][o]] = [arrInfo[o][v]];
  }
  }

  return transposed;
}

url์— ์žˆ๋Š” ์˜ต์…˜๊ฐ’ ์ค‘ type=video๋ฅผ ๊ฐ•์ œ๋กœ ๋„ฃ์ง€ ์•Š์œผ๋ฉด video ์˜ต์…˜์€ ์„ค์ •ํ•ด๋„ 403์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋‹ˆ ์ฃผ์˜ ๋ฐ”๋ž€๋‹ค.

  var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + playlist 
  + "&eventType=completed" //์ด๋ฏธ ์—…๋กœ๋“œ ๋œ 
  + "&order=date&type=video" //๋‚ ์งœ์ˆœ ์ •๋ ฌ ๋ฐ type์€ video
  + "&videoDefinition=high" //๊ณ ํ™”์งˆ
  + "&videoEmbeddable=true" //์‚ฌ์šฉ ํ—ˆ์šฉ
  + "&maxResults=2" //๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ˆ˜
  + "&regionCode=KR" //๊ตญ๊ฐ€ ์ฝ”๋“œ
  + "&videoDimension=2d" //3d ์˜์ƒ ์ œ์™ธ ํ•˜๊ธฐ ์œ„ํ•ด, 2d๋งŒ
  + "&videoDuration=long" //20๋ถ„ ์ด์ƒ์˜ ๊ณก
  + "&key="+ API_KEY ; //๋‚˜์˜ ๊ตฌ๊ธ€ API KEY

param ๊ด€๋ จํ•œ ์ •๋ณด๊ฐ€ ๋‚˜์™€์žˆ๋Š” ๊ฐ€์ด๋“œ ๋งํฌ๋ฅผ ์ „๋‹ฌ๋“œ๋ฆฐ๋‹ค.

 

API Reference  |  YouTube Data API  |  Google Developers

API Reference YouTube Data API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด YouTube ์›น์‚ฌ์ดํŠธ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ์ž์˜ ์›น์‚ฌ์ดํŠธ ๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ชฉ๋ก์—์„œ๋Š” API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ

developers.google.com

 

keyword๋ฅผ ๊ทธ๋•Œ ๊ทธ๋•Œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ํ‚ค์›Œ๋“œ ์„ค์ • ๋ฐ ํ•ด๋‹น ํ‚ค์›Œ๋“œ๋ณ„ ๋ฆฌ์ŠคํŠธ๋ฅผ 2๊ฐœ์”ฉ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•ด๋‘์—ˆ๋‹ค.
์„ ํƒ์ ์œผ๋กœ ๋ณด๋“œ ๋ฐœํ–‰ ํ•  ์˜ˆ์ •์ด๋‹ค.

 

์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•ด์„œ, ์ข‹์€ ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์œ ํŠœ๋ธŒ(Youtube) API ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ํŽธ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

*์ฐธ๊ณ ๋กœ ์ˆ˜์ •, ์—…๋กœ๋“œ, ์‚ญ์ œ ๋“ฑ์€ OAuth ์ธ์ฆ์ •๋ณด๋„ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต ์‹œ ํ˜ธ์ถœ ์ œํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ณผ ๋  ๊ฒฝ์šฐ #ERROR๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 

*5์›” ์ถ”๊ฐ€ ๊ณ ๋ฏผ ์‚ฌํ•ญ : ์ƒ๊ฐ๋ณด๋‹ค ์ข‹์€ playlist๊ฐ€ ๊ฒ€์ƒ‰์ด ์•ˆ๋˜์„œ ํŠน์ • ์ฑ„๋„ ๊ธฐ์ค€์œผ๋กœ ๋ฆฌ์ŠคํŠธ์—… ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

๋Œ“๊ธ€