๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT๐Ÿ’ก/Python

[Python] ์‚ฌ์šฉ์ž ์œ„์น˜๊ธฐ๋ฐ˜ ๋‚ ์”จ ์ •๋ณด ๋ฉ”์ผ๋กœ ๋ณด๋‚ด๊ธฐ(geopy, OpenWeatherMap, smtplib)

by hk713 2022. 3. 17.

์‚ฌ์šฉ์ž ์œ„์น˜๊ธฐ๋ฐ˜์˜ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฉ”์ผ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.

 

๋จผ์ €, ์ฝ”๋“œ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€ ๋‹จ๊ณ„๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

1๏ธโƒฃ ์‚ฌ์šฉ์ž์—๊ฒŒ "ํ•™๊ต๋ช…"์„ ์ž…๋ ฅ๋ฐ›๊ณ , geopy๋ฅผ ์‚ฌ์šฉํ•ด ์œ„๋„/๊ฒฝ๋„๋ฅผ ๊ตฌํ•œ๋‹ค

2๏ธโƒฃ OpenWeatherMap API๋ฅผ ์‚ฌ์šฉํ•ด ์œ„์น˜์— ๋Œ€ํ•œ ๋‚ ์”จ ์ •๋ณด๋ฅผ ์–ป๋Š”๋‹ค

3๏ธโƒฃ ๋„ค์ด๋ฒ„ SMTP ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ๋ฉ”์ผ์„ ๋ณด๋‚ธ๋‹ค

 

geopy๋Š” ์ „ ์„ธ๊ณ„์˜ ์ฃผ์†Œ, ๋„์‹œ, ๊ตญ๊ฐ€ ๋ฐ ๋žœ๋“œ๋งˆํฌ์˜ ์ขŒํ‘œ๋ฅผ ์ฐพ์•„์ฃผ๋Š” python ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค.

https://geopy.readthedocs.io/en/stable/index.html?highlight=user_agent 

 

Welcome to GeoPy’s documentation! — GeoPy 2.2.0 documentation

geopy is a Python client for several popular geocoding web services. geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources. geo

geopy.readthedocs.io

์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” pip ์„ค์น˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค!

pip install geopy

 

๊ทธ๋ฆฌ๊ณ  ๋‚ ์”จ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด์„œ 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๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด requests์™€ json ๋ชจ๋“ˆ์ด ํ•„์š”ํ•˜๋‹ค. json์€ ํŒŒ์ด์ฌ ๋‚ด์žฅ๋ชจ๋“ˆ์ด๋ฏ€๋กœ requests๋งŒ pip ์„ค์น˜๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

pip install requests

 

๋งˆ์ง€๋ง‰์œผ๋กœ ํŒŒ์ด์ฌ์œผ๋กœ ๋ฉ”์ผ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” SMTP ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

(์—ฌ๊ธฐ์„œ๋Š” ๋„ค์ด๋ฒ„ ๋ฉ”์ผ์„ ์‚ฌ์šฉํ–ˆ๋‹ค)

"๋„ค์ด๋ฒ„ ๋ฉ”์ผ" ํ™”๋ฉด์— ๋“ค์–ด๊ฐ€ ์™ผ์ชฝ ๋ฉ”๋‰ด๋ฐ”์— ์žˆ๋Š” ์ž‘์€ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๋ˆ„๋ฅด๋ฉด "ํ™˜๊ฒฝ ์„ค์ •" ํ™”๋ฉด์ด ๋ณด์ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์„ค์ •์„ ๋ฐ”๊ฟ” ์ค€๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ํ•˜๋ฉด ์ค€๋น„๋Š” ๋๋‚œ๋‹ค!


์™„์„ฑ ์ฝ”๋“œ

from geopy.geocoders import Nominatim
import requests
import json
import smtplib
from email.message import EmailMessage

# ํ•™๊ต๋ช… ์ž…๋ ฅ๋ฐ›๊ธฐ
school = input("ํ•™๊ต๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”: ")

# ์œ„๋„(lat), ๊ฒฝ๋„(long) ๊ตฌํ•˜๊ธฐ
app = Nominatim(user_agent='test')  # user_agent ๊ฐ’์€ ์•„๋ฌด๊ฑฐ๋‚˜ ์จ๋„ ๋œ๋‹ค
location = app.geocode(school)
lat = location.latitude
lng = location.longitude


# ๋‚ ์”จ ์ •๋ณด ๊ตฌํ•˜๊ธฐ
apikey = "๋ฐœ๊ธ‰๋ฐ›์€ API KEY"
lang = "kr"
api = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lng}&appid={apikey}&lang={lang}&units=metric"

response = requests.get(api)
data = json.loads(response.text)
content = f"""{school}์˜ ๋‚ ์”จ๋Š” {data["weather"][0]["main"]}({data["weather"][0]["description"]}) ์ž…๋‹ˆ๋‹ค.
ํ˜„์žฌ ์˜จ๋„๋Š” {data["main"]["temp"]} ์ด๊ณ , ์ฒด๊ฐ ์˜จ๋„๋Š” {data["main"]["feels_like"]} ์ž…๋‹ˆ๋‹ค.
๊ด€์ฐฐ๋œ ์ตœ๊ณ  ๊ธฐ์˜จ์€ {data["main"]["temp_max"]} ์ด๋ฉฐ ์ตœ์ € ๊ธฐ์˜จ์€ {data["main"]["temp_min"]} ์ž…๋‹ˆ๋‹ค:)

** ๊ฒ€์ƒ‰ํ•œ {school}์˜ ์œ„์น˜๋Š” {data["name"]} ์ด๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. 
"""


# ๋ฉ”์ผ ๋งŒ๋“ค๊ธฐ
mail = EmailMessage()

# ๋ณธ๋ฌธ ์ž‘์„ฑ
mail.set_content(content)

# MIME Header ์„ค์ • - ๋ฉ”์ผ ์ œ๋ชฉ, ๋ฐœ์‹ ์ธ, ์ˆ˜์‹ ์ธ
mail["Subject"] = f"์š”์ฒญํ•˜์‹  {school}์˜ ๋‚ ์”จ ์ •๋ณด์ž…๋‹ˆ๋‹ค"
mail["From"] = "๋ฐœ์‹ ์ž ๊ณ„์ • - xxxx@naver.com"
mail["To"] = "์ˆ˜์‹ ์ž ๊ณ„์ •"


# SMTP ์„ค์ • - ๋„ค์ด๋ฒ„
SMTP_SERVER = "smtp.naver.com"
SMTP_PORT = 465

# ๋ณด์•ˆ์—ฐ๊ฒฐ(SSL)ํ•ด์„œ SMTP ์„œ๋ฒ„ ์—ฐ๊ฒฐ
smtp = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)

# smtp ๊ณ„์ • ๋กœ๊ทธ์ธ
smtp.login("๋„ค์ด๋ฒ„ID","๋„ค์ด๋ฒ„PW")

# ๋ฉ”์ผ ๋ฐœ์†ก
smtp.send_message(mail)
smtp.quit()

์‹คํ–‰๊ฒฐ๊ณผ

์•„๋‚˜์ฝ˜๋‹ค ํ™˜๊ฒฝ์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ž…๋ ฅ์ฐฝ์ด ๋œฌ๋‹ค! 

์˜ˆ์‹œ๋กœ "๋ช…์ง€๋Œ€ํ•™๊ต" ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด...

 

Out: (221, b'2.0.0 Closing connection cAY5eqbzSuKe57mdSBW78Q - nsmtp')

์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต์ด๋‹ค!

 

์ˆ˜์‹ ์ž ๋ฉ”์ผํ•จ์„ ์‚ดํŽด๋ณด๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค๐Ÿ˜

((๋„ค์ด๋ฒ„์—์„œ gmail๊ณ„์ •์œผ๋กœ ๋ฉ”์ผ์„ ๋ณด๋ƒˆ๋‹ค))

๋Œ“๊ธ€