Day 035

AWESOMee·2022년 3월 7일
0

Udemy Python Bootcamp

목록 보기
35/64
post-thumbnail

Udemy Python Bootcamp Day 035

Using API Keys and Get Weather

import requests

OWM_Endpoint = "https://api.openweathermap.org/data/2.5/onecall"
api_key = "******************"

weather_params = {
    "lat": **.***,
    "lon": ***.****,
    "appid": api_key
}

response = requests.get(OWM_Endpoint, params=weather_params)
data = response.json()
print(data)

slice() Function

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)
print(a[x])

#output
('a', 'b')

Check if it Will Rain in the Next 12 Hours

weather_data = response.json()
weather_slice = weather_data["hourly"][:12]

will_rain = False

for hour_data in weather_slice:
    condition_code = hour_data["weather"][0]["id"]
    if int(condition_code) < 700:
        will_rain = True

if will_rain:
    print("Bring an Umbrella")

# weather_data = weather_data["hourly"][0]["weather"][0]["id"]

Sending SMS via the Twilio API

if will_rain:
    client = Client(account_sid, auth_token)
    message = client.messages \
        .create(
        body="It's going to rain today. Remember to bring an  ☔",
        from_='+1937872****',
        to=MY_NUMBER
    )
    print(message.status)

메세지를 보내다니요.... 이제 신기해하는 것도 물림..

"python everywhere"에서 계속 작동하도록 등록하는 건
저번 생일메일 보내기부터 뭐가 문젠지 작동을 안함...
command python3 main.py 입력해도 자꾸 재입력하도록 뜸..
anyway, "python everywhere"에 입력하면 정해진 시간에 메일/SMS 전송 가능함!

import requests
from twilio.rest import Client

OWM_Endpoint = "https://api.openweathermap.org/data/2.5/onecall"
api_key = "******************"
account_sid = "******************"
auth_token = "******************"
MY_NUMBER = '+******************'

weather_params = {
    "lat": ******,
    "lon": ******,
    "exclude": "current,minutely,daily",
    "appid": api_key
}

response = requests.get(OWM_Endpoint, params=weather_params)
response.raise_for_status()
weather_data = response.json()
weather_slice = weather_data["hourly"][:12]

will_rain = False

for hour_data in weather_slice:
    condition_code = hour_data["weather"][0]["id"]
    if int(condition_code) < 700:
        will_rain = True

if will_rain:
    client = Client(account_sid, auth_token)
    message = client.messages \
        .create(
        body="It's going to rain today. Remember to bring an  ☔",
        from_='+193787******',
        to=MY_NUMBER
    )
    print(message.status)

Environment Variables

VIRTUAL_ENV=/User/me/PycharmProjects/rain_alert/venv

These variables have values which are strigs which can be used in our applications or our code.
Now inside "python everywhere" we can also type env and we can see slightly defferent environment variables which are relative to the "Python anywhere" environment.

There's two major use cases.

One is for convenience. Normally when we deploy a large application, the process is quite complicated. And once we've done it, we kind of don't want to mess around with the code base and update the code files.
Instead, we could have these environment variables, which we can change. So certain variables that are being used in our code base could be set as environment variables and we can modify those variables without having to touch the code.

A sencond reason is for security. So when we're developing software, we might be uploading our code based somewhere. And it's usually not a good idea to have things like your authentication keys or our API keys to be stored in the same place as the rest of our code.

That's where environment variables come in.
So, environment variables essentially allow us to seperate out where we store our keys, our secret stuff, and various other variables away from where our code base is located.

profile
개발을 배우는 듯 하면서도

0개의 댓글