Day 35 - Intermediate+ Keys, Authentication & Environment Variables: Send SMS

本章由 Dot 分享,筆記連結網址於此。由 Sky 取得同意後,整理於下方,方便大家參考與討論。

https://www.notion.so/Day-35-1e34056d9f2e49c092cc287599530dd1

時間:2021年5月23日 20:00~20:30
與會人員:Dot, Wayne, 玉米, Shadow, Yeh, Sky
分享:Dot

學習目標

API Keys, Authentication, Environment Variables and Sending SMS

上課筆記

#309 介紹今日目標

#310 介紹為什麼需要 API Keys

#311 測試你申請的 API Keys

api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}

https://www.iso.org/obp/ui/#iso:code:3166:TW

Standard: ISO 3166 — Codes for the representation of names of countries and their subdivisions
Committee:ISO/TC 46 ICS:01.140.30

#312 檢查接下來 12 小時是否會下雨

#313 利用 SMS API 傳送訊息

#314 利用 python anywhere 週期執行程式

#315 系統環境變數與小結

課程練習

import requests

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

weather_params = {
    "lat": 25.056561995080813,
    "lon": 121.48609015286014,
    "exclude": "current,minutely,hourly",
    "appid": api_key,
}

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

輸出 JSON

{
    "lat": 25.0566,
    "lon": 121.4861,
    "timezone": "Asia/Taipei",
    "timezone_offset": 28800,
    "daily": [
        {
            "dt": 1621738800,
            "sunrise": 1621717594,
            "sunset": 1621766118,
            "moonrise": 1621753680,
            "moonset": 1621708560,
            "moon_phase": 0.37,
            "temp": {
                "day": 305.84,
                "min": 298.91,
                "max": 306.56,
                "night": 301.71,
                "eve": 304.27,
                "morn": 298.98
            },
            "feels_like": {
                "day": 311.22,
                "night": 305.78,
                "eve": 313.02,
                "morn": 299.94
            },
            "pressure": 1009,
            "humidity": 58,
            "dew_point": 295.57,
            "wind_speed": 3.69,
            "wind_deg": 313,
            "wind_gust": 3.82,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 80,
            "pop": 0.48,
            "rain": 0.47,
            "uvi": 12.25
        },
        {
            "dt": 1621825200,
            "sunrise": 1621803975,
            "sunset": 1621852549,
            "moonrise": 1621843980,
            "moonset": 1621797240,
            "moon_phase": 0.41,
            "temp": {
                "day": 305.91,
                "min": 297.73,
                "max": 305.91,
                "night": 297.73,
                "eve": 298.86,
                "morn": 300.34
            },
            "feels_like": {
                "day": 311.39,
                "night": 298.54,
                "eve": 299.78,
                "morn": 303.53
            },
            "pressure": 1011,
            "humidity": 58,
            "dew_point": 295.77,
            "wind_speed": 4.79,
            "wind_deg": 54,
            "wind_gust": 6.01,
            "weather": [
                {
                    "id": 501,
                    "main": "Rain",
                    "description": "moderate rain",
                    "icon": "10d"
                }
            ],
            "clouds": 38,
            "pop": 0.96,
            "rain": 13.17,
            "uvi": 12.5
        },
        {
            "dt": 1621911600,
            "sunrise": 1621890357,
            "sunset": 1621938979,
            "moonrise": 1621934460,
            "moonset": 1621886100,
            "moon_phase": 0.45,
            "temp": {
                "day": 300.43,
                "min": 297.62,
                "max": 300.6,
                "night": 298.66,
                "eve": 299.38,
                "morn": 297.81
            },
            "feels_like": {
                "day": 303.2,
                "night": 299.54,
                "eve": 299.38,
                "morn": 298.63
            },
            "pressure": 1012,
            "humidity": 77,
            "dew_point": 295.34,
            "wind_speed": 4.12,
            "wind_deg": 49,
            "wind_gust": 5.44,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 94,
            "pop": 0.44,
            "rain": 1.07,
            "uvi": 12.09
        },
        {
            "dt": 1621998000,
            "sunrise": 1621976740,
            "sunset": 1622025409,
            "moonrise": 1622025060,
            "moonset": 1621975260,
            "moon_phase": 0.5,
            "temp": {
                "day": 304.85,
                "min": 298.36,
                "max": 304.85,
                "night": 300.58,
                "eve": 302.37,
                "morn": 298.36
            },
            "feels_like": {
                "day": 309.82,
                "night": 303.99,
                "eve": 306.56,
                "morn": 299.18
            },
            "pressure": 1010,
            "humidity": 61,
            "dew_point": 295.53,
            "wind_speed": 2.68,
            "wind_deg": 255,
            "wind_gust": 4.01,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 57,
            "pop": 0.37,
            "rain": 0.73,
            "uvi": 11.97
        },
        {
            "dt": 1622084400,
            "sunrise": 1622063124,
            "sunset": 1622111839,
            "moonrise": 1622115720,
            "moonset": 1622064720,
            "moon_phase": 0.53,
            "temp": {
                "day": 307.52,
                "min": 299.21,
                "max": 308.31,
                "night": 300.32,
                "eve": 305.22,
                "morn": 299.21
            },
            "feels_like": {
                "day": 310.38,
                "night": 303.48,
                "eve": 308.14,
                "morn": 299.21
            },
            "pressure": 1008,
            "humidity": 44,
            "dew_point": 292.52,
            "wind_speed": 6.73,
            "wind_deg": 270,
            "wind_gust": 8.71,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 100,
            "pop": 0.34,
            "rain": 0.29,
            "uvi": 12.11
        },
        {
            "dt": 1622170800,
            "sunrise": 1622149509,
            "sunset": 1622198268,
            "moonrise": 1622206200,
            "moonset": 1622154540,
            "moon_phase": 0.57,
            "temp": {
                "day": 305.8,
                "min": 298.76,
                "max": 305.8,
                "night": 298.76,
                "eve": 300.91,
                "morn": 299.58
            },
            "feels_like": {
                "day": 309.88,
                "night": 299.75,
                "eve": 304.66,
                "morn": 299.58
            },
            "pressure": 1010,
            "humidity": 54,
            "dew_point": 294.29,
            "wind_speed": 5.88,
            "wind_deg": 294,
            "wind_gust": 7.61,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 100,
            "pop": 0.9,
            "rain": 6.86,
            "uvi": 13
        },
        {
            "dt": 1622257200,
            "sunrise": 1622235895,
            "sunset": 1622284697,
            "moonrise": 1622296320,
            "moonset": 1622244720,
            "moon_phase": 0.6,
            "temp": {
                "day": 295.05,
                "min": 294.44,
                "max": 297.85,
                "night": 294.71,
                "eve": 294.59,
                "morn": 296.47
            },
            "feels_like": {
                "day": 295.67,
                "night": 295.38,
                "eve": 295.24,
                "morn": 297.36
            },
            "pressure": 1013,
            "humidity": 91,
            "dew_point": 292.72,
            "wind_speed": 5.17,
            "wind_deg": 63,
            "wind_gust": 8.37,
            "weather": [
                {
                    "id": 502,
                    "main": "Rain",
                    "description": "heavy intensity rain",
                    "icon": "10d"
                }
            ],
            "clouds": 100,
            "pop": 1,
            "rain": 57.76,
            "uvi": 13
        },
        {
            "dt": 1622343600,
            "sunrise": 1622322283,
            "sunset": 1622371126,
            "moonrise": 1622385960,
            "moonset": 1622335020,
            "moon_phase": 0.64,
            "temp": {
                "day": 295.43,
                "min": 294.72,
                "max": 296.69,
                "night": 296.48,
                "eve": 296.69,
                "morn": 295.12
            },
            "feels_like": {
                "day": 296.06,
                "night": 297.3,
                "eve": 297.37,
                "morn": 295.85
            },
            "pressure": 1010,
            "humidity": 90,
            "dew_point": 292.96,
            "wind_speed": 4.8,
            "wind_deg": 66,
            "wind_gust": 8,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 100,
            "pop": 1,
            "rain": 5.11,
            "uvi": 13
        }
    ]
}

進階練習

找出12小時內是否會下雨

import requests
import datetime

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

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

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

for hour in first_twelve_hourly_data:
    if hour["weather"][0]["id"] < 700:
        print(f"{datetime.datetime.fromtimestamp(hour['dt']).isoformat()}: "
              f"Rain - Bring an umbrella")

利用 telegram bot api 傳送下雨資訊

import requests

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

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

chat_id = "756199869"
token = "請填入你申請到的token"
TG_Endpoint = "https://api.telegram.org/bot" + token + "/"
tg_params = {
    "method": "sendMessage",
    "chat_id": chat_id,
    "text": "",
}

hours_num = 24
response = requests.get(OWM_Endpoint, params=weather_params)
response.raise_for_status()
weather_data = response.json()
first_twelve_hourly_data = weather_data["hourly"][:hours_num]

for hour in first_twelve_hourly_data:
    if hour["weather"][0]["id"] < 700:
        tg_params["text"] = f"未來 {hours_num} 小時內會下雨,請記得帶傘。"
        requests.post(TG_Endpoint, params=tg_params)
        break

參考資料

[Telegram] Telegram(五) 取得 Chat ID – 樺的筆記:
http://blog.3dgowl.com/telegram-telegram五-取得-chat-id/

請問一下相同的概念,是不是也能運用在line 上面,
利用Line 的 API來實現 用Line 通知會不會下雨?
這部分想請教有沒有先進有測試過的心得呢?

之前有前輩分享過了喔,而且還剛好同一堂課。請參考這篇介紹,謝謝!

這篇分享的真棒! 等等好好來跟著實作~
感覺從最近這幾堂課開始,多了許多可以實際運用的場景,對於我這種沒正式學過程式的人,找到了堅持下去的動機~ ^^
感謝 sky大 跟 ChrisWei大

1個讚

昨天一步一步跟著做,也完成了用Line 發天氣預報,直接發到我跟我老婆的群組中,之後每天都會發天氣提醒,真棒!!! 這樣實際應用在生活場景的感覺真好~ 感謝老師及各位先進的分享。

1個讚