1. An overview of the
Speaking of ant forest, have you ever been picked up by a friend because you forgot to pick up energy?
If you’re not a heavy ant Forest user, being charged energy by someone else probably doesn’t matter to you.
But if you’re a heavy user of Ant Forest and your energy gets stolen…
In this article we will look at how to use Python + Appium to automate the collection of ant forest energy.
2. Environmental
The main environment of this paper is as follows:
- Win7
- Millet 5 s
- Python3.7
- Appium1.5
- Pay treasure 10.2.6.7010
If you are not familiar with the environment setup, you can see: Python + Appium automatic operation wechat entry and I used Python to find out all the people who deleted my wechat and automatically deleted them.
3. The implementation
The basic idea of function realization is as follows:
-
Open Alipay into the ant forest, collect their own energy
-
After collecting your own energy, click “Find energy” to enter your friend’s ant forest and collect your friend’s energy, and so on
Let’s take a look at the main code implementation.
The parameter configuration code is as follows:
desired_caps = {
"platformName": "Android".# system
"platformVersion": "8.0.0".# System version number
"deviceName": "m5s".# device name
"appPackage": "com.eg.android.AlipayGphone".# the package name
"appActivity": "AlipayLogin".# App starts the main Activity
'noReset': True Save session information to avoid re-login
}
Copy the code
Ant Forest is usually placed on the home page of Alipay. At this time, we can enter it by directly clicking the ant Forest option after opening Alipay.
The code implementation is as follows:
driver.find_elements_by_id('com.alipay.android.phone.openplatform:id/home_app_view') [10].click()
Copy the code
After entering my own ant forest, I began to collect my own energy. Since the new version of Alipay cannot locate the element of energy ball, we need to click in the area where the energy ball may appear. The code for collecting energy is as follows:
# Charge energy
def collect_energy(driver) :
print('Start taking energy')
Get the phone screen width and height
width = int(driver.get_window_size()['width'])
height = int(driver.get_window_size()['height'])
# Coordinates of the region where the energy sphere may appear
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)
# Click on the specified coordinates
driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], 1000)
print('All power collected')
Copy the code
After collecting their own energy, click “Find energy” to enter their friend ant forest to continue collecting energy. The code is as follows:
# to find energy
def search_energy(driver) :
print('Find energy, collect energy from friends')
time.sleep(3)
# Click to find energy
driver.tap([(1000.1520), (1080.1580)].1000)
time.sleep(3)
# Collect friend energy
collect_energy(driver)
time.sleep(3)
# Continue to look for energy after collecting
search_energy(driver)
Copy the code
After the energy collection function is realized, we can use the scheduled task to realize the scheduled collection. Let’s take a look at the implementation of the scheduled task.
PIP install apscheduler is used to implement scheduled tasks.
The code of the scheduled task is as follows:
scheduler = BlockingScheduler()
# collect_main: a timed execution method
scheduler.add_job(collect_main, 'cron', hour=20, minute=23, second=20)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
Copy the code
At this point, we use Python + Appium to achieve timed automatic collection of ant forest energy work is complete.
The source code is available in Python 201116.