基於AutoJs實現淘寶領喵幣和螞蟻森林能量自動化

自動收能量優化版來襲:

【自動收取螞蟻森林能量】:
界面化操作,日誌窗口;
基於控件座標,適用於所有分辨率手機;
支持自動給好友澆水啦。

需新版的小夥伴公衆號zxiaofan隨時留言關鍵字“螞蟻森林”獲取。
 自動收能量領喵幣【二合一版本】

概要

  一年一度雙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/antlove.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自動化測試。
  當然,對於爾等程序猿而言:
  領喵幣不是重點,重點是不能浪費寶貴的時間在領喵幣上。


祝君好運!

Life is all about choices!

將來的你一定會感激現在拼命的自己!

CSDN】【GitHub】【OSCHINA】【掘金】【微信公衆號
歡迎訂閱zxiaofan的微信公衆號,掃碼或直接搜索zxiaofan


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章