(暂时不支持图片)
最近手机从一加换到小米了。因为平时喜欢写备忘录,一加便签里记了很多条信息。但是手机搬家只能搬一个没有签名的阉割版一加便签过来,原来记的笔记都没了。网上一搜解决办法全是莫名其妙的啥波一敬业签广告,要么就是自己一条一条复制,十分令人苦恼。
想到几百条便签一条一条手动不知道要复制多久,于是编程小白的我研究了一两天,自己写了段代码,希望能帮到大家。
利用的是adb模拟手机操作的方法,有点类似selenium爬虫,简单有效,缺点就是会慢一点,但是适用性强,别的高级办法咱就不懂了,因为安卓这一块我是完全小白呜呜呜
前期准备
电脑下载adb工具和python
版本不要太老就行
adb下载地址:
https://dl.google.com/android/repository/platform-tools-latest-windows.zip
python不需要用到第三方库,随便下个python就行
两个都记得要配置环境变量,具体方式可以网上自己搜搜,教程很多
手机下载adb keyboard
ADBKeyBoard/ADBKeyboard.apk at master · senzhk/ADBKeyBoard (github.com)
安装就完事了
下面正式开始操作
第一步,手机usb连接电脑,手机开发者选项打开usb调试,并允许usb调试模拟点击(小米手机上叫usb调试安全设置)
然后在cmd里输入adb devices,如果下面有检测到device,那第一步就成功了
第二步,在原来的一加手机里打开手机搬家,选择备份,备份便签
之后会提示你备份保存在什么地方了,应该是叫backup啥的,自己找找
里面有一个oneplus.note,或者叫Note的对应一加便签的文件夹,打开有很多几百k的没有后缀的文件(其实就是便签里的图片)还有最关键的OnePlusNote.xml文件,这个文件里就存着所有的便签内容,最需要的也就是这个文件,把这个文件导入电脑
第三步,上代码
import re
import os
import time
from shutil import copyfile
import xml.dom.minidom
import base64
if not os.path.exists('OnePlusNote.txt'):
copyfile('OnePlusNote.xml', 'OnePlusNote_tmp.xml')
os.rename('OnePlusNote_tmp.xml','OnePlusNote.txt')
with open('OnePlusNote.txt','r' ,encoding='UTF-8',errors='ignore') as f:
data = f.read()
print(len(data))
response = data
word=response.encode('UTF-8', 'ignore').decode('UTF-8')
word=word.replace('','')
word=word.replace('','')
word=word.replace('','')
#word=word.replace(' ','\n')
#word=re.sub(r'[\U0001F000-\U0001ffff]', lambda m: unicodedata.name(m.group()), word)
#word=re.sub(r'[\u200b,\u2661,\xa0]', lambda m: unicodedata.name(m.group()), word)
if not os.path.exists('OnePlusNote1.xml'):
file_pathh=r'OnePlusNote1.txt'
with open(file_pathh, 'w', encoding='UTF-8') as file:
file.write(word)
os.rename(file_pathh,file_pathh[:-3]+'xml')
dom = xml.dom.minidom.parse('OnePlusNote1.xml')
root = dom.documentElement
re=root.getElementsByTagName("noteRecord")
title=[]
text=[]
top=[]
for i in range(len(re)):
title.append(re[i].getAttribute('title'))
tt=(re[i].getAttribute('rich_content'))
text.append(tt.replace(' ','\n'))#内容
top.append(re[i].getAttribute('top'))#1顶置 0不顶置
#print(i)
title.reverse()
text.reverse()
top.reverse()
t0=[]
t1=[]
c0=[]
c1=[]
for i in range(len(title)):
if top[i]=='1':
t0.append(title[i])
c0.append(text[i])
else:
t1.append(title[i])
c1.append(text[i])
title=t1+t0
text=c1+c0
'''
print(title)
for ti in text:
print(ti)
print('---')
'''
os.system("adb devices")
out = os.popen("adb devices").read()
#input tap 后面的就是手机几个需要自动点击的点,最左上角为0 0,先x轴再y轴
#我的手机是k50u,请根据自己手机屏幕分辨率来自行修改参数
#time.sleep 决定等待的时间,可根据手机性能自己修改
for i in range(len(title)):
os.system("adb shell input tap 1044 2400")
time.sleep(0.5)
os.system("adb shell input tap 100 320")
os.system("adb shell am broadcast -a ADB_INPUT_B64 --es msg %s" %(str(base64.b64encode(title[i].encode('utf-8')))[1:]))
os.system("adb shell input tap 100 1800")
os.system("adb shell am broadcast -a ADB_INPUT_B64 --es msg %s" %(str(base64.b64encode(text[i].encode('utf-8')))[1:]))
os.system("adb shell input tap 1111 176")
os.system("adb shell input tap 108 170")
time.sleep(0.5)
按照自己手机的情况修改最后的几个参数,默认是红米k50u的配置
手机打开小米便签或者其他什么便签,进到这个界面就可以了,然后记得切换输入法为adb keyboard,不然无法输入中文!
懒得搞ide这些的话,代码可以粘贴到记事本,改完参数保存为txt文件,再把后缀改为.py,双击运行,大功告成,等个几分钟便签就一条一条导入完了,我300多条导入了差不多七八分钟,如果对手机性能有信心可以把time.sleep调小一点,临时关闭手机动画,这样可以加快时间
运行完成会有两个新文件,一个是OnePlusNote.txt,一个是OnePlusNote1.xml。两个都可以直接打开看到自己所有的便签内容(原来的xml文件不能直接打开,因为有一些非法字符&#啥的,后面我做了更改替换)emoji都看得到
暂时不支持导入图片,虽然原理其实也是差不多的,但是说实话这种方式本来就已经不太优美了,再一张一张push图片,选择导入就更臃肿而且慢了,我在找更好的办法,如果老哥们有更好的通过adb直接输入图片的方法,请告诉我
如果遇到什么问题程序出错可以拿错误代码问我,我尽量回答