基于 AutoJs实现淘宝领喵币和蚂蚁森林能量自动化

909 阅读6分钟
原文链接: mp.weixin.qq.com

概要

  一年一度双11又到了,一起来凑个热闹。淘宝需要各种签到、逛店铺领喵币,一个店铺15S,领完所有喵币,至少十几二十分钟就没了,作为一名程序员,是时候解放双手了。

脚本自动领取喵币

1、技术背景

1.1、AutoJs

GitHub:https://github.com/hyb1996/Auto.js

AutoJs简介(以下内容摘录至GitHub)

  • 一个支持无障碍服务的Android平台上的JavaScript IDE,其发展目标是JsBox和Workflow。
  • 同时有VS Code插件可提供基础的在桌面开发的功能。
  • 由无障碍服务实现的简单易用的自动操作函数。
  • Auto.js主要以自动化、工作流为目标。
  • Auto.js执行大部分任务不需要root权限。只有需要精确坐标点击、滑动的相关函数才需要root权限。

1.2、其他准备工作

  • 开发工具:VsCode,其实文本编辑器都可以,只不过VsCode可以在线调试;
  • 下载VsCode插件:Auto.js-VSCodeExt、Debugger for Chrome、Chinese (Simplified) Language Pack for Visual Studio Code。

2、脚本编写

详细的API参见官网,本文就不赘述了。做个简单的东西并不复杂,我就花了一下午的时间,完成学习以及做了淘宝领喵币脚本、蚂蚁森林收能量脚本。

2.1、淘宝领喵币脚本

  如果找不到“去浏览”按钮,就会提示“今日浏览店铺领取喵币已达上”,可能是网速太慢导致加载时间较长,可以把等待时间设置长一点,或者提前先启动淘宝APP,让其在后台运行。

var height = device.height;var width = device.width;goScan(1);// 去浏览function goScan(k) {    toastLog("Build By @zxiaofan,有问题请前往订阅号【zxiaofan】留言");    toastLog("打开淘宝");    launch("com.taobao.taobao");    sleep(4000);    toastLog("打开淘宝ing...");    sleep(4000);    scrollUp();    // 进入领取喵币主页    click(800, 1650);    sleep(4000);    toastLog("开始领喵币");    getMiaoBi();    sleep(1000);    click("领喵币");    sleep(2000);    var count = 0;    toastLog("去浏览 店铺");    while (count < 100) {        sleep(1000);        var 去浏览 = text("去浏览").findOnce();        // log("[去浏览]:" + 去浏览);        if (去浏览 != null) {            count++;            toastLog("[去浏览:]" + count);            click("去浏览");            sleep(1000);            scrollDown();            sleep(5000);            toastLog("[等待滑动15秒领取喵币 ing...]");            sleep(15000);            var 首页 = text("首页").findOnce();            if (null == 首页) {                back();            } else {                click("首页");                goScan(1);            }        } else {            sleep(1000);            scrollUp();            toastLog("今日浏览店铺领取喵币已达上限");            break;        }    }    sleep(1000);    // 签到    var 签到 = text("签到").findOnce();    if (签到 != null) {        toastLog("[签到 ing...]");        click("签到");        sleep(4000);        back();    }    var 去签到 = text("去签到").findOnce();    if (去签到 != null) {        toastLog("[去签到 ing...]");        click("去签到");        sleep(4000);        // var 去完成 = text("去完成").findOnce();        // click("去完成");        // sleep(4000);        toastLog("[农场签到啦啦 ]");        // 不知道怎么操作,把屏幕挨个点击一遍        // if (textContains("的农场").exists()) {        //     //判断当前界面        //     sleep(1000);        // textContains("去完成").waitFor();        // sleep(1000);        // textContains("去完成").findOne().click();        // text("签到").waitFor();        // var i = text("签到").findOne().bounds();        // click(i.centerX(), i.centerY());;        toastLog("农场手动签到吧");        // click(width / 13 * 8.5, height / 7 * 4);        sleep(4000);        back();        back();        // }        toastLog("[END]");        // back();    }    toastLog("Build By @zxiaofan,有问题请前往订阅号【zxiaofan】留言");    sleep(2000);    exit();}function getMiaoBi() {    sleep(1000);    toastLog("[领取已有喵币]");    // 上线1 = desc("上线1").findOnce();    // 上线1.parent().click();    // click("TB1wUKChoD1gK0jSZFGXXbd3FXa-133-18.png_270x270Q50s50.jpg_");    // 354,1267,734,1372    scrollUp();    click(500, 1300);}

2.2、蚂蚁森林收能量脚本

var height = device.height;var width = device.width;var countmax = 100;ant(1);// 去浏览function ant(k) {    toast("开始收取蚂蚁森林能量");    toastLog("Build By @zxiaofan,有问题请前往订阅号【zxiaofan】留言");    click("蚂蚁森林");    //注册音量下按下退出脚本监听    registEvent();    sleep(3000);    // 先把自己的能量收了    // 排除区域:250,420,830,1608    toast("收取自己的蚂蚁森林能量");    getEnergy(1);    // toast("查看截图权限");    toastLog("查看截图权限");    var reqScreen = requestScreenCapture();    log("reqScreen:" + reqScreen);    if (!reqScreen) {        sleep(1000);        toast("请求截图失败");        log("请求截图失败");        exit();    }    sleep(3000);    toastLog("下滑");    scrollDown();    var screen = captureScreen();    if (screen == null) {        toastLog("截图失败");        exit();    }    toastLog("截图成功");    sleep(500);    var shou = images.load("/sdcard/antshou.jpg");    var love = images.load("/sdcard/antshou.jpg");    if (null == shou) {        shou = images.load("http://tc.zxiaofan.com/tc/a/icon/anthand.jpg");    }    sleep(1000);    if (null == love) {        love = images.load("http://tc.zxiaofan.com/tc/a/icon/antlove.jpg");    }    sleep(1000);    images.save(shou, "/sdcard/antshou.jpg");    images.save(love, "/sdcard/antlove.jpg");    if (shou == null || love == null) {        toastLog("加载图片失败");        exit();    }    var count = 0;    toastLog("[屏幕分辨率]width:" + width + ";height:" + height);    while (count < countmax) {        count++;        screen = captureScreen();        var point = findImage(screen, shou, {            region: [width - 200, 200, 200, height - 200],            threshold: 0.7        });        sleep(500);        if (!point) {            point = findImage(screen, love, {                region: [width - 200, 200, 200, height - 200],                threshold: 0.7            });        }        if (point) {            toastLog("找到可收取能量:" + point);            click(point.x - 10, point.y + 10);            getEnergy(2);      //收能量            sleep(500);            back();            sleep(1000);        } else {            toastLog("没有找到能量:" + count);            sleep(1000);            scrollDown();            toastLog("查看更多好友");            click("查看更多好友");            sleep(1000);            var 没有更多了 = text("没有更多了").findOnce();            if (没有更多了 != null) {                toastLog("所有好友的蚂蚁森林均已查看完毕");                sleep(1000);                count = countmax;            }        }    }    toastLog("脚本运行结束");    toastLog("Build By @zxiaofan,有问题请前往订阅号【zxiaofan】留言");    exit();}function getEnergy(k) {    sleep(3000);    //浇水 sleep(3000);    /*     if (text("浇水").exists()) {        //判断有没有浇水         var Water = text("浇水").findOne(600).bounds();         click(Water.centerX(), Water.centerY());         sleep(1000);     }    */    // if (textStartsWith("收集能量").exists()) {//判断是否存在能量球    //     textStartsWith("收集能量").find().forEach(function (e) {    //         var pos = e.bounds();   //能量球坐标范围    //         click(pos.centerX(), pos.centerY() - 70);//点击能量球坐标范围中心点    //         sleep(500);    //     })    // }  else {    //     toast("没有可收集的能量");    // }    //将能量球存在的区域都点一遍,间隔是能量球的半径    var countmax = 900;    var rowmax = 900;    if (k == 1) {        countmax = 750;        rowmax = 750;    }    for (var row = 640; row < rowmax; row += 100) {        for (var col = 140; col < countmax; col += 100) {            click(col, row);            sleep(50);        }    }}/** * 设置按键监听 当脚本执行时候按音量减 退出脚本 */function registEvent() {    //启用按键监听    events.observeKey();    //监听音量上键按下    events.onKeyDown("KEYCODE_VOLUME_DOWN", function (event) {        toastLog("脚本手动退出");        exit();    });}

3、成品

  有能力的同学可以自行使用上面的脚本完成自动化操作;  如果需要现成的APP的同学(直接安装可用),请前往公众号【zxiaofan】留言“自动领喵币”,后台将自动回复APP下载地址。回复“蚂蚁森林能量”,后台自动回复最新脚本文件。

4、使用注意事项

  • AutoJs需要获取权限:“获取手机号码、IMEI、IMSI权限”、“读写手机存储”、“显示悬浮窗”;
  • 权限如果不提供,APP直接无法启动;
  • 关于IMSI权限,AutoJs作者的解释是 用于显示启动页广告;
  • 使用AutoJs必须开启“无障碍服务”权限。

5、后记

  其实AutoJs想象空间很大,比如各种褥羊毛、APP自动化测试。  当然,对于尔等程序猿而言:  领喵币不是重点,重点是不能浪费宝贵的时间在领喵币上。

学习,思考;                          

          

                           沉淀,成长。