How to send weather reminders to the people you like every day

"  How to send a daily weather reminder to the person you like "

 

 

First of all, let’s think about it. Your girlfriend asks you to send her a weather message every day, and it can’t be repeated. If you can persist for a year, then she will agree to marry you. But after you think about it, you are very lazy, and you may not be able to hold it for a year. This is anxious. What can I do, what if I forget it.

 

 

01— Thinking

 

 

What is the demand here?

 

First of all, your girlfriend asks you to send a weather message to her, and you have to send it every day. You must persist for a year.

 

No problem, we just used the method of sending text messages before, so let's use it.

What about the weather news?

We searched the weather interface of the relevant weather channel to see if we could find it. Don't tell me, we found it.

Well, send the text message and get the weather, then what are we going to do? Of course it is looking for materials.

 

02— Material preparation

 

material:

1、pip install twilio

2. A weather forecast interface

3. Girlfriend's mobile phone number

4. Register a twilio account.

For the fourth step, you need to apply for a mobile phone number. Let’s give you a website: https://www.twilio.com/

I won’t say anything about the operation, after all, there are already many on Baidu.

 

 

03— Start programming

Then we will enter the programming link. First, let’s look at how to send a text message to a mobile phone.

The code for sending SMS is as follows:

def send_message(text):
    # 下面认证信息的值在你的 twilio 账户里可以找到
    account_sid = "ACecad97aaec8f2e1fbd95b1390ef2acf"
    auth_token = "b5a5a863223b6c3dad4f1e488975dd4"
    client = Client(account_sid, auth_token)
    client.messages.create( to="+86",  # 区号+你的手机号码
                            from_="+150",  # 你的 twilio 电话号码
                            body=text)
    print("发送成功")

Look at it this way, is it very simple? This SMS code. That's right how simple it is.

 

 

The rest is to fix the weather, then let's see.

We need the weather interface and get the information returned by the interface.

Here I provide you with an interface:

http://t.weather.sojson.com/api/weather/city/101280101

 

Main format: http://t.weather.sojson.com/api/weather/city/+city_id

 

There is a file dedicated to city_id, which will be packed and taken away later.

 

How do we get information?

 

We use the module that drops the interface, here we use requests.

code show as below:

def get_weather():#获取天气
    url = "http://t.weather.sojson.com/api/weather/city/101280101"
    html = requests.get(url)
    x = html.json()
    forecast="""时间:{0},{1}
{2},{3},{4},风力{5},{6}
温馨提示:{7}
""".format(x["data"]["forecast"][0]["ymd"], x["data"]["forecast"][0]["week"],
                    x["data"]["forecast"][0]["high"], x["data"]["forecast"][0]["low"],
                    x["data"]["forecast"][0]["fx"], x["data"]["forecast"][0]["fl"],
                    x["data"]["forecast"][0]["type"], x["data"]["forecast"][0]["notice"])
    data="""{0}:{1}
湿度:{2}
pm25:{3}
pm10:{4}
温度:{5}
质量:{6}
提醒:{7}
{8}""".format(x["time"], x["cityInfo"]["city"], x["data"]["shidu"],
            x["data"]["pm25"], x["data"]["pm10"], x["data"]["quality"],
            x["data"]["wendu"], x["data"]["ganmao"], forecast)#短信格式
    print(data)

 

Okay, get it done, and get the weather successfully. Take a look at the output:

 

2019-06-24 14:13:21:广州市
湿度:97%
pm25:8.0
pm10:11.0
温度:优
质量:23
提醒:各类人群可自由活动
时间:2019-06-24,星期一
高温 30.0℃,低温 25.0℃,无持续风向,风力<3级,大雨
温馨提示:出门最好穿雨衣,勿挡视线

Seeing this full of warm reminders, my girlfriend is very happy!

 

Then come and send it at 8:00 every morning.

 

Look at the complete code:

 

import  requests
import time
from twilio.rest import Client
​
def send_message(text):
    # 下面认证信息的值在你的 twilio 账户里可以找到
    account_sid = "ACecad97aaec8fe1dfbd95b1390ef2acf"
    auth_token = "b5a5a8632223b6c3dd4f1e488975dd4"
    client = Client(account_sid, auth_token)
    client.messages.create( to="+86",  # 区号+你的手机号码
                            from_="+133",  # 你的 twilio 电话号码
                            body=text)
    print("发送成功")
​
def get_weather():
    url = "http://t.weather.sojson.com/api/weather/city/101280101"
    html = requests.get(url)
    x = html.json()
    forecast="""时间:{0},{1}
{2},{3},{4},风力{5},{6}
温馨提示:{7}
""".format(x["data"]["forecast"][0]["ymd"], x["data"]["forecast"][0]["week"],
                    x["data"]["forecast"][0]["high"], x["data"]["forecast"][0]["low"],
                    x["data"]["forecast"][0]["fx"], x["data"]["forecast"][0]["fl"],
                    x["data"]["forecast"][0]["type"], x["data"]["forecast"][0]["notice"])
    data="""{0}:{1}
湿度:{2}
pm25:{3}
pm10:{4}
温度:{5}
质量:{6}
提醒:{7}
{8}""".format(x["time"], x["cityInfo"]["city"], x["data"]["shidu"],
            x["data"]["pm25"], x["data"]["pm10"], x["data"]["quality"],
            x["data"]["wendu"], x["data"]["ganmao"], forecast)
    send_message(data)#调用发送短信的函数
​
if __name__=="__main__":
    while true:
        time.sleep(1)#防止cpu处理太多,加一个一秒的停顿
        if time.strftime("%H:%M", time.localtime())=="08:00":
              get_weather()

 

SMS successfully sent;

 

 

A bunch of hidden confession codes

Python implements blessing bullet frame

Teach you how to use 21 lines of code to develop desktop applications

Official account backstage reply: SMS reminder to   get the interface and source code. Like to remember to continue to follow us​.

 

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/94355434