使用 uiautomator2 实现批量歌曲下载
前言
在移动设备上批量下载歌曲可以节省大量的时间和精力。本文介绍如何利用 uiautomator2
库实现自动化歌曲下载功能。
环境配置
在开始之前,需要配置以下环境:
- Python 3.x
uiautomator2
库- Android 设备(启用开发者模式和 USB 调试)
安装依赖
pip install uiautomator2
脚本详解
以下是完整的脚本,实现了从应用“ListenDown”下载指定歌曲列表。
coding: utf-8
import uiautomator2 as u2
import time
import os
DOWNLOAD_LOG = “downloaded_songs.txt”
d = u2.connect()
d.app_clear(“com.listendown.music.plus”)
读取已下载歌曲
if os.path.exists(DOWNLOAD_LOG):
with open(DOWNLOAD_LOG, “r”, encoding=“utf-8”) as f:
downloaded_songs = set(line.strip() for line in f)
print(“[INFO] 已下载歌曲列表读取成功”)
else:
downloaded_songs = set()
print(“[INFO] 未找到下载记录,初始化下载列表”)
def search_and_download(song_name):
print(f"[INFO] 开始处理歌曲: {song_name}“)
if song_name in downloaded_songs:
print(f”[跳过] 已下载✅: {song_name}“)
return
try:
# 打开APP
d.app_start(“com.all.inclusive”)
print(”[INFO] 应用启动成功")
time.sleep(2)
d.click(0.637, 0.476)
print(“[INFO] 搜音乐成功”)
time.sleep(2)
d.click(0.635, 0.085)
print(“[INFO] 点击搜索框成功”)
time.sleep(2)
d.click(0.381, 0.426)
print(“[INFO] 输入框点击成功”)
time.sleep(1)
input_box = d.xpath(‘//[@resource-id=“com.all.inclusive:id/textInputLayout”]/android.widget.FrameLayout[1]‘)
input_box.click_exists()
print(“[INFO] 搜索框定位成功”)
time.sleep(1)
input_box.set_text(song_name)
print(f"[INFO] 输入歌曲名称: {song_name}“)
time.sleep(1)
d.click(0.467, 0.519)
print(”[INFO] 开始搜索")
time.sleep(2)
d.click(0.927, 0.231)
print(“[INFO] 选择第一首歌曲”)
time.sleep(1)
print(“[INFO] 点击下载按钮”)
if d.xpath(’//[contains(@text, “高品”)]’).exists:
d.click(0.191, 0.518)
time.sleep(1)
d.click(0.751, 0.655)
downloaded_songs.add(song_name)
with open(DOWNLOAD_LOG, “a”, encoding=“utf-8”) as f:
f.write(f"{song_name}\n")
print(f"[成功] 下载完成✅: {song_name}“)
else:
print(”[警告] 没有找到高品❌")
print(“[INFO] 返回主页成功”)
except Exception as e:
print(f"[错误] {song_name} 下载失败❌ - {e}")
song_list = [
“《修炼爱情》林俊杰”, “《知足》五月天”, “《温柔》五月天”,
“《突然好想你》五月天”, “《盛夏光年》五月天”, “《爱情呼叫转移》林俊杰”, “《会呼吸的痛》梁静茹”, “《开始懂了》孙燕姿”,
“《普通朋友》陶喆”, “《找自己》陶喆”, “《爱很简单》陶喆”, “《阴天》莫文蔚”, “《Monsters》Katie Sky”,
“《Here with You》Asher Book”, “《Faded》Alan Walker”, “《Better Now》Post Malone”, “《Way Back》Vicetone & Cozi Zuehlsdorff”,
“《Wolves》Selena Gomez & Marshmello”, “《Dancing with Your Ghost》Sasha Sloan”, “《Something Just Like This》The Chainsmokers & Coldplay”, “《Love Is Gone》Dylan Matthew”, “《Numb》Linkin Park”,
“《An Angel》Kelly Family”, “《Seasons in the Sun》Westlife”, “《Eversleeping》Xandria”, “《Waiting for You》Dept & Ashley Alisha”, “《Perfect》Ed Sheeran”,
“《Dreaming My Dreams》The Cranberries”, “《Don’t Cry》Guns N’ Roses”, “《Only Time》Enya”, “《My Heart Will Go On》Celine Dion”, “《Come Away with Me》Norah Jones”,
“《Someone Like You》Adele”, “《Rubia》周深”, “《Born Again》Doja Cat & RAYE”, “《Love Is Gone》SLANDER & Dylan Matthew”, “《Numb》Linkin Park”
]
for song in song_list:
search_and_download(song)
print(f"[INFO] {song} 执行成功")
time.sleep(3)
脚本源码2
# coding: utf-8
import uiautomator2 as u2
import time
import os
DOWNLOAD_LOG = "downloaded_songs.txt"
d = u2.connect()
d.app_clear("com.listendown.music.plus")
# 读取已下载歌曲
if os.path.exists(DOWNLOAD_LOG):
with open(DOWNLOAD_LOG, "r", encoding="utf-8") as f:
downloaded_songs = set(line.strip() for line in f)
print("[INFO] 已下载歌曲列表读取成功")
else:
downloaded_songs = set()
print("[INFO] 未找到下载记录,初始化下载列表")
def search_and_download(song_name):
print(f"[INFO] 开始处理歌曲: {
song_name}")
if song_name in downloaded_songs:
print(f"[跳过] 已下载✅: {
song_name}")
return
try:
# 打开APP
d.app_start("com.all.inclusive")
print("[INFO] 应用启动成功")
time.sleep(2)
d(resourceId="com.all.inclusive:id/tvDescription", text="各大平台的音乐集合").click()
time.sleep(2)
d(resourceId="com.all.inclusive:id/search").click()
print("[INFO] 点击搜索框成功")
time