preface

The thing is, recently I know a little sister has the habit of watching the weather forecast every morning. In my opinion, many people get up first thing is to check wechat news, in this case, I am reluctant to send a weather forecast to the little sister every morning.

The first few days, I was using a very primitive method, to get the weather forecast screenshots, and then manually sent to the little sister. After a few days I thought: wrong ah, how I say is also a program ape, how can use so low way.

It reminds me of an open source Python library I saw earlier — Wxpy, a very powerful wechat API call class library that just meets my current needs.

Task decomposition

  • Call wechat API to send a simple message
  • Get screenshots of the weather forecast for the day
  • Setting a Scheduled Task

Call wechat API to send a simple message

This program is mainly used through the WXPY library, refer to its official website documents, we need to do the following preparations:

1.Python environment (I am using Python 2.7 on MAC)

Install the WXpy library

pip install -U wxpy -i "https://pypi.doubanio.com/simple/"(Douban source used by domestic users)Copy the code

Get the chat object and send the message

Let’s try to get hold of the file transfer Assistant and practice with it

from wxpy import *File_helper = bot.file_helper# File_helper.send ('Hello')
Copy the code

Obtain specified wechat friends through nicknames

Friend = bot.friends().search(' zhiming ')friend.send(u'Hello Zhiming '
Copy the code

Get screenshots of the weather forecast for the day

There are many APIS on the domestic weather network. I found a simple and easy to use weather query website — China Weather

My requirement is to intercept the content circled in the red box of the web page and convert it into pictures. A search is really to find such a artifact — Webkit2png, official website address

webkit2png

This is a command line tool that converts web content into images on Linux and can be installed directly on MAC via BREW

brew install webkit2png
Copy the code

There is a small problem here. Webkit2png must use HTTPS links by default. You need to manually modify the webkit2PNG program, please refer to the link reference link

webkit2png http://www.google.com/
Copy the code

The Google home page image can be found in the directory where the command line is currently executed

What if I want to capture a part of the page and convert it to an image? Webkit2png is totally ok

Using chrome’s debugging tool, you can easily find the selector corresponding to the page element in the specified area

Try to take a screenshot of the page through the selector. Enter the following command on the command line to see the corresponding screenshot of the page

webkit2png --ignore-ssl-check --selector=#today http://www.weather.com.cn/weather1d/101020100.shtml\#search
Copy the code

Setting a Scheduled Task

I’m using BlockingScheduler library to set up a timed job. It’s very easy to use

if __name__ == '__main__':    from apscheduler.schedulers.blocking import BlockingScheduler    sched = BlockingScheduler()
    # set save job Sched. Add_job (keep_online, 'interval', seconds=10) Use the cron expression to specify the sending period as 6 am every day from Monday to Friday: 30 sched.add_job(weather_notification, 'cron', day_of_week='1-5', hour=6, minute=30) sched.start()
Copy the code

Here’s a little trick. Since the first time you start wechat chatbot, you need to scan the code to log in, so WXpy provides a caching function, which means you can keep wechat online for a long time without having to scan the code repeatedly

Bot = bot (console_qr=2, cache_path=True, qr_path='wepy.pkl')
Copy the code

At the end

After knowing the truth, little sister decided to ignore me, because she didn’t know whether she was chatting with the robot or me, so these functions should be used carefully, moved at the same time, she also joked: No wonder the number of single app apes is so high.