๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL๐Ÿ”ฅ/๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ_AI School 5๊ธฐ

[๋ฉ‹์‚ฌ] AI SCHOOL 5๊ธฐ_ Day 3

by hk713 2022. 3. 16.

API

 

API(Application Programming Interface)๋ฅผ ์ง์—ญํ•˜๋ฉด "์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธํ„ฐํŽ˜์ด์Šค" ์ด๋‹ค.

API๋ฅผ ์„ค๋ช…ํ•  ๋•Œ, ์‹๋‹น๋น„์œ ๋ฅผ ๋งŽ์ด ๋“ค๊ณค ํ•œ๋‹ค. 

์†๋‹˜(ํ”„๋กœ๊ทธ๋žจ)์ด ๋ฉ”๋‰ด(๋ช…๋ น)๋ฅผ ์ฃผ๋ฌธํ•˜๋ฉด, ์ ์›(API)์€ ๋ฉ”๋‰ด(๋ช…๋ น)์„ ๋ฐ›์•„ ์š”๋ฆฌ์‚ฌ(์‘์šฉ ํ”„๋กœ๊ทธ๋žจ)์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

์š”๋ฆฌ์‚ฌ(์‘์šฉ ํ”„๋กœ๊ทธ๋žจ)๊ฐ€ ์š”๋ฆฌ(๋ช…๋ น์˜ ๊ฒฐ๊ณผ๊ฐ’)๋ฅผ ์™„์„ฑํ•˜๋ฉด ์ ์›(API)์€ ์š”๋ฆฌ(๋ช…๋ น์˜ ๊ฒฐ๊ณผ๊ฐ’)๋ฅผ ์†๋‹˜(ํ”„๋กœ๊ทธ๋žจ)์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

๐Ÿ™‹‍โ™€๏ธProgram -- ๐Ÿค–API -- ๐Ÿ–ฅ Program

โ–ต ์ด๋Ÿฐ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค

 

OpenWeatherMap 

openWeatherMap์—์„œ ๋‚ ์”จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

https://openweathermap.org/

 

ะกurrent weather and forecast - OpenWeatherMap

Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on the global and local weather models, satellites, radars and a vast network of weather stations. how to obtain APIs (subscriptions w

openweathermap.org

์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด ํšŒ์›๊ฐ€์ž… ํ›„ API Key๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์•„์•ผ ํ•œ๋‹ค. 

API Key๋Š” ํ•ด๋‹น API์˜ ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. 

์ด API๋Š” ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ json ํ˜•์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

json

javascript object notation์˜ ์•ฝ์ž๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• ์ค‘์—์„œ ๊ฐ์ฒด(object)๋ผ๋Š” ๋ฌธ๋ฒ•์„ ๋”ฐ๋ฅด๋Š” ๋ฌธ์ž๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ ํฌ๋งท์ด๋‹ค.

json์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋งท์ด๋‹ค! ๋ชจ์–‘์ด Dictionary๋ž‘ ๋น„์Šทํ•œ๋ฐ type์„ ์‚ดํŽด๋ณด๋ฉด <str>์ด๋‹ค.

๋”ฐ๋ผ์„œ ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” json ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

โžก๏ธ json.loads(<str>) ์„ ํ†ตํ•ด <str>์„ <dict>๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Googletrans

Googletrans๋Š” ๊ตฌ๊ธ€ ๋ฒˆ์—ญ API(Google Translate API)๋ฅผ ๊ตฌํ˜„ํ•œ ๋ฌด๋ฃŒ ๋ฌด์ œํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. 

https://py-googletrans.readthedocs.io/en/latest/

 

Googletrans: Free and Unlimited Google translate API for Python — Googletrans 3.0.0 documentation

Googletrans: Free and Unlimited Google translate API for Python Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate. Feature

py-googletrans.readthedocs.io

pip install googletrans

ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์„ค์น˜ ํ›„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. ((๊ณต์‹๋ฌธ์„œ ์‚ฌ์ดํŠธ ํ™œ์šฉ))

.detect() : ์–ธ์–ด๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค

.translate() : ๋ฒˆ์—ญํ•œ๋‹ค


IMAP

IMAP(Internet Messaging Access Protocol)๋Š” "์„œ๋ฒ„์—์„œ ๋ฉ”์ผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ"์ด๋‹ค.

IMAP์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”์‹œ์ง€๊ฐ€ ์›๊ฒฉ ์„œ๋ฒ„์— ์ €์žฅ๋ผ ์–ด๋””์„œ๋‚˜ ๋ชจ๋“  ์žฅ์น˜์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

SMTP

SMTP(Simple Mail Transfer Protocol)๋Š” "๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ์•ฝ์†"์ด๋‹ค. 

์ฆ‰, SMTP์„œ๋ฒ„๋ฅผ ์ด์šฉํ•ด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ณณ์œผ๋กœ ๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ™‹‍โ™€๏ธ Email client A - - SMTP - - > Email Server A ๐Ÿ–ฅ - - SMTP - - > ๐Ÿ’ป Email Server B - - IMAP - - > Email client B๐Ÿ™‹‍โ™‚๏ธ

MIME

MIME(Multipurpose Internet Mail Extensions)๋Š” ์ด๋ฉ”์ผ์— ์ฒจ๋ถ€ํ•  ํŒŒ์ผ(์ด๋ฏธ์ง€๋‚˜ ์˜ค๋””์˜ค, ๋น„๋””์˜ค ๋“ฑ)์„ ํ…์ŠคํŠธ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

ํŒŒ์ด์ฌ์—์„  emil.message ๋ชจ๋“ˆ์—์„œ .EmailMessage ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ด๊ฑฐ๋Š” ๋‚ด์žฅํ•จ์ˆ˜๋‹ค!

ํŒŒ์ผ ๋ชจ๋“œ(binary)

  • rb : read binary
  • wb : write binary
  • ab : append binary

binary๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฌธ์ž๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. jpg ๊ฐ™์€ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด๋‚˜ mp4 ๊ฐ™์€ ์˜์ƒ ํŒŒ์ผ ๋“ฑ์„ ์ปดํ“จํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.


์ •๊ทœํ‘œํ˜„์‹

^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$

^ : ์‹œ์ž‘

$ : ๋

[a-zA-Z0-9.+_-] : a๋ถ€ํ„ฐ z๊นŒ์ง€, A๋ถ€ํ„ฐ Z๊นŒ์ง€, 0๋ถ€ํ„ฐ 9๊นŒ์ง€, . , + , _ , - ๊ฐ€ 1ํšŒ ์ด์ƒ ๋ฐ˜๋ณต๋œ๋‹ค.

\. : ์ผ๋ฐ˜ ๋ฌธ์ž . ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ

[a-zA-Z]{2,3} : a๋ถ€ํ„ฐ z๊นŒ์ง€, A๋ถ€ํ„ฐ Z๊นŒ์ง€๊ฐ€ ์ตœ์†Œ 2ํšŒ, ์ตœ๋Œ€ 3๋ฒˆ  ๋ฐ˜๋ณต๋œ๋‹ค

 

 

โš ๏ธ ํ•ด๋‹น ๊ธ€์€ ๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ AI SCHOOL ๊ต์œก ๋‚ด์šฉ์„ ๋ณต์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค

 

๋Œ“๊ธ€