Use Python to automatically collect the energy of the ant forest at a fixed time, no longer have to worry about forgetting to collect it

1 Overview

When it comes to Ant Forest, everyone should know, have you ever been charged by a friend because you forgot to collect energy?

If you are not a heavy user of Ant Forest, the energy charged by others may be nothing to you.

But if you are a heavy user of Ant Forest and encounter energy being stolen...

In this article, let’s take a look at how to use Python + Appium to automatically collect ant forest energy at regular intervals.

2. Environment

The main environment of this article is as follows:

  • Win7

  • Mi 5s

  • Python3.7

  • Appium1.5

  • Alipay 10.2.6.7010

If you are not familiar with environment setup , you can take a look: Python + Appium Automated Operation of WeChat Introduction and I used Python to find out all the people who deleted my WeChat and automatically delete them .

3. Realization

The basic idea of ​​function realization is:

  • Open Alipay and enter the ant forest to collect your own energy

  • After collecting your own energy, click to 找能量enter the friend's ant forest to collect your friend's energy, and so on

Next we look at the main code implementation.

The parameter configuration code is implemented as follows:

desired_caps = {
    "platformName": "Android", # 系统
    "platformVersion": "8.0.0", # 系统版本号
    "deviceName": "m5s", # 设备名
    "appPackage": "com.eg.android.AlipayGphone", # 包名
    "appActivity": "AlipayLogin", # app 启动时主 Activity
    'noReset': True # 保留 session 信息,可以避免重新登录
}

Usually everyone will put Ant Forest on the Alipay homepage. At this time, after opening Alipay, click on the Ant Forest option to enter.

The code is implemented as follows:

driver.find_elements_by_id('com.alipay.android.phone.openplatform:id/home_app_view')[10].click()

After entering our ant forest, we start to collect our own energy, because the new version of Alipay can't locate the energy ball element, so we need to click on the area where the energy ball may appear. The code for collecting energy is implemented as follows:

# 收取能量
def collect_energy(driver):
    print('开始收取能量')
    # 获取手机屏幕宽高
    width = int(driver.get_window_size()['width'])
    height = int(driver.get_window_size()['height'])
    # 能量球可能出现的区域坐标
    start_x = 110
    end_x = 940
    start_y = 460
    end_y = 880
    for i in range(start_y, end_y, 80):
        for j in range(start_x, end_x, 80):
            tap_x1 = int((int(j) / width) * width)
            tap_y1 = int((int(i) / height) * height)
            # 点击指定坐标
            driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], 1000)
    print('能量收取完毕')

After collecting your energy, click to 找能量enter the friend Ant Forest to continue collecting energy, the code implementation is as follows:

# 找能量
def search_energy(driver):
    print('找能量,收取好友能量')
    time.sleep(3)
    # 点击找能量
    driver.tap([(1000, 1520), (1080, 1580)], 1000)
    time.sleep(3)
    # 收取好友能量
    collect_energy(driver)
    time.sleep(3)
    # 收取完毕继续找能量
    search_energy(driver)

After the energy collection function is realized, we can use the timed task to realize the timed collection. Let's look at the realization of the timed task.

Achieve timing tasks we use apschedulercomponents need to perform before using the pip install apschedulerequipment it.

The code implementation of the timing task is as follows:

scheduler = BlockingScheduler()
# collect_main:定时执行的方法
scheduler.add_job(collect_main, 'cron', hour=20, minute=23, second=20)
try:
    scheduler.start()
except (KeyboardInterrupt, SystemExit):
    pass

At this point, we use Python + Appium to realize timed and automated collection of ant forest energy.

The source code is obtained by replying to 201116 in the background of the public number Python small two .

< END >

Originality is not easy, please share, watch, praise

Guess you like

Origin blog.csdn.net/ityard/article/details/109733747