The best solution for App side automation, completely free your hands!

Posted by griemar on Tue, 23 Nov 2021 19:37:37 +0100

1. Preface

Hello, I'm Ango!

I wrote an article before, which proposed a scheme to automatically send mass News Morning Post to wechat group every day

How to use Python crawler to send mass News Morning Post to wechat? (detailed)

However, for many people, first of all, writing an App requires some mobile terminal development experience, and then writing another barrier free service application, which is difficult

This article will introduce another scheme, that is, using AutoJS introduced in the previous article to automatically send news morning post to wechat group

2. Reptiles and services

For the convenience of demonstration, baidu hot search is used as the data source of News Morning Post,

Use requests + beautiful soup to crawl the 15 data with the highest heat according to the heat

import requests
from bs4 import BeautifulSoup

def baidu_top_tipic():
    """Baidu hot search"""
    requests_page = requests.get('')
    soup = BeautifulSoup(requests_page.text, "lxml")

    # query
    soup_text = soup.find_all("div", class_='c-single-text-ellipsis', text=True)

    top_list = []
    for index, text in enumerate(soup_text):
        top_list.append((str(index + 1) + "," + text.string.strip()))

    # Take the 15 data with the highest heat
    return '\n'.join(top_list[:15])

Then, use the FastAPI   Write an API to get the morning news and deploy it to the ECS (take CentOS as an example here)

import uvicorn
from fastapi import FastAPI
from every_news import *

# pip3 install uvicorn
# pip3 install fastapi

# instantiation 
app = FastAPI()

# Daily News
async def rsc_api():
    msg = get_news()
    return {
        "code": 200,
        "msg": msg

if __name__ == '__main__':'news_api:app', host="",
                port=6789, reload=True, debug=True)

Finally, run the following command to make the service run in the background

# Command line background running
# Log directory: / news_api.log
nohup python3 /xag/ >  /news_api.log 2>&1 &

3. Automatically send group chat

Write AutoJS script in VS Code

First, define a method to send messages to group chat

PS: use   click()   Coordinate click operation is only applicable to Android 7.0+

//API call to get news data
var url = "http://host:6789/news";

//Send group chat name
var group_name = "Group chat name";

//Send information to wechat group
function send_wx_msg(group_name, send_msg) {
    //If you sleep, wake up the device
    //Note: to ensure low power consumption, sleep is set (no operation for 10s)

    //Open wechat

    //Click to enter the chat interface
    var chat_element_bounds = text(group_name).findOne().bounds();
    //Support Android 7.0+
    click(chat_element_bounds.centerX(), chat_element_bounds.centerY());
    //send message
    text("send out").click()
    log("Sending succeeded!")
    //Return to mobile desktop

Then, start a new thread in the main thread, call the API interface, and send the data after obtaining the data

threads.start(function () {
    //Get news
    http.get(url, {}, function (res, err) {
        if (err) {
            log("i 'm sorry! Failed to get news today...")
        log("Today's news is a success!")
        let html = res.body.string();
        let msg = JSON.parse(html).msg;
        send_wx_msg(group_name, msg)

Then, use VS Code to import the source code into the mobile device

Finally, select source file - right click - more  -  Scheduled task, just set the scheduled task

4. Finally

In this way, the function of sending morning news to the designated group every morning can be realized

Of course, if multiple group chat sending is involved, you only need to use AutoJS to query multiple target group chat objects + page sliding, traverse and send information

In addition, due to the instability of barrier free service, it can be set in the setting   The priority of AutoJS application service to ensure the stable operation of the program

If you think the article is good, please   Like, share and leave messages   Next, because this will be the strongest driving force for me to continue to output more high-quality articles!

Recommended reading

Mobile automation AutoJS Quick Start Guide (Part 1)

5 minutes, use intranet penetration to quickly realize remote desktop

Talk about how Jmeter executes Python scripts concurrently

Talk about the best solution for PC Automation - Pywinauto

Talk about the best solution for PC Automation - WinAppDriver

Topics: Python