์ฌ์ฉ์ ์์น๊ธฐ๋ฐ์ ๋ ์จ ์ ๋ณด๋ฅผ ๋ฉ์ผ๋ก ๋ณด๋ด์ฃผ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์๋ค.
๋จผ์ , ์ฝ๋๋ ํฌ๊ฒ 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๋ฅผ ์ฌ์ฉํ๋ค.
ะก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๊ณ์ ์ผ๋ก ๋ฉ์ผ์ ๋ณด๋๋ค))
๋๊ธ