[google app script] google ๋ฒˆ์—ญ API ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

[google app script] google ๋ฒˆ์—ญ API ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

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

playlist ์—ฐ๋™์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์˜์–ด๋กœ ๊ฒ€์ƒ‰ํ•œ ์Šคํฌํ‹ฐ ํŒŒ์ด ์Œ์›์„ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•˜์—ฌ wiki ๋ฐฑ๊ณผ์—์„œ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ์š”. ์–ด์ฐจํ”ผ ๊ฐ€์ˆ˜ ๊ฒ€์ƒ‰์„ ํ•˜๋Š” ์ž…์žฅ์ด๋ผ ํฐ ๋ฒˆ์—ญ์ด ํ•„์š”ํ•˜์ง€ ์•Š์•„ Google cloud platform์— Cloud Translation Rest API๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด ๋ฒˆ์—ญ์„ ํ•˜์˜€๋‹ต๋‹ˆ๋‹ค.

์•„๋ž˜ ๋ณธ์ธ api ํ† ํฐ๊ฐ’๊ณผ ๊ฒ€์ƒ‰ ๋‹จ์–ด๋ฅผ ์˜์–ด๋กœ ๊ธฐ์žฌํ•˜๋ฉด ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญ๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. 

1. ์†Œ์Šค์ฝ”๋“œ

function translateWord()
{
  var apiToken  = "๋ณธ์ธ API token";
  var searchWord = "BTS"

  var url ="https://translation.googleapis.com/language/translate/v2?key=" + apiToken;

    var response = UrlFetchApp.fetch(url,
    { 
      method: "POST",
      headers:{
        "contentType": "application/json; charset=utf-8",
        "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36"
      },
      payload :{
        "q" : String(searchWord),
        "source" : "en",
        "target" : "ko",
        "format" : "text"
      },
      muteHttpExceptions: true

    });

  var json = response.getContentText();
  var data = JSON.parse(json);

  searchWord = data.data.translations[0].translatedText;

  return searchWord;
}

 

user-agent ๋ฐ muteHttpExceptions๋Š” ๊ผญ ์„ค์ •ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์Šต๊ด€์ด ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋‹ˆ ์ €๋„ ๋งค๋ฒˆ ๋นผ๋จน๊ณ  40x ์˜ค๋ฅ˜ ํ›„์— ์ˆ˜์ •ํ•˜๋Š”๋ฐ์š”. ์Šต๊ด€๋“ค์—ฌ์„œ ์˜ค๋ฅ˜ ํšŸ์ˆ˜๋ฅผ ์ค„์ž…์‹œ๋‹ค.

์ €๋Š” wiki ๋ฐฑ๊ณผ ๊ฒ€์ƒ‰๊นŒ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œ๊ธฐ๋ฉ๋‹ˆ๋‹ค. wiki ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ์ง€๋‚œ๋ฒˆ ํฌ์ŠคํŒ… ๊ธ€์— ๊ธฐ์žฌ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 

๋ฐ˜์‘ํ˜•
 

[google app script] wiki ์œ„ํ‚ค ๋ฐฑ๊ณผ API๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

ํ‹ฐ์Šคํ† ๋ฆฌ ๊ฒŒ์‹œ๊ธ€์„ ์ž๋™ ๋ฐœํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•ด ๋ณธ ์ž๋™ํ™” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ์Œ์•…์— ๋Œ€ํ•ด ๋งค์ผ ํฌ์ŠคํŒ…ํ•˜๋Š” ๊ฒƒ์„ ๊ณ„ํšํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋“ค ์œ„ํ‚ค๋ฐฑ๊ณผ์— ๋Œ€ํ•ด ์ž˜ ์•Œ๊ณ  ์žˆ๊ฒ ์ง€๋งŒ, ๊ตฌ๊ธ€์—์„œ ๊ฒ€์ƒ‰ ์‹œ ์ œ์ผ

odaily.tistory.com

 

2. ์ฐธ๊ณ ํ•  ๊ฐ€์ด๋“œ ๋งํฌ

https://cloud.google.com/translate/docs/reference/rest/v2/translate

 

3. ๋ฒˆ์—ญ ์–ธ์–ด ์ง€์› ์ฝ”๋“œ

https://cloud.google.com/translate/docs/languages

 

4. ํŠน์ • ๊ธ€์ž ์ˆ˜ ์ด์ƒ ๋ฒˆ์—ญ ์‹œ ์š”๊ธˆ ์œ ๋ฃŒ

https://cloud.google.com/translate/pricing

์ œ ํฌ์ŠคํŒ… ๋œ ๊ธ€์„ ๋ณด๋ฉด ๊ตฌ๊ธ€ API ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์—…๋ฌด ์ž๋™ํ™”์— ๋งŽ์ด ํ™œ์šฉํ•ด๋ณด์„ธ์š”. ์ง€๊ธˆ๊นŒ์ง€ google ๋ฒˆ์—ญ API ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. 

 

๋Œ“๊ธ€