1. The scene

Have you ever bothered to get water on the screen by swiping the screen to clean the dishes?

Have you ever been bothered to swipe tiktok while eating, but the screen size of your phone makes it hard to swipe videos with your fingers?

Or are you fidgeting about constantly reaching out to switch between videos while you’re lying under the covers in winter?

This article will use automation and crawler, accurate brush douyin, completely free their hands, do a real lazy

2. Implement

The specific implementation ideas are as follows: AccessibilityService is responsible for the automatic UI operation of Douyin App, Jsoup is responsible for the basic information of Douyin video, including the duration of each video. Finally, after each video is played, slide to the next video immediately to achieve step 1 through 6 steps. Create a new Android project with Android Studio and add dependencies with Gradle

//build. Gradle // add dependencies'org. Jsoup: jsoup: 1.13.1'//JSON data parsing implementation'com. Alibaba: fastjson: 1.2.70'
}
Copy the code

Step 2, and create a barrier-free service to handle the events of tiktok page changes

//DouYinJava // Public class DouYinService extends BaseService {private static final String PAGE_MAIN ="com.ss.android.ugc.aweme.main.MainActivity";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event)
    {
        String className = event.getClassName().toString();

        if(textutils.equals (PAGE_MAIN, className)) {// event handler}}}Copy the code

Step 3: Obtain the share address of a single video

First, simulate clicking the share button to jump to the video sharing dialog box

//DouYinJava // Share button ID Private static final String ID_SHARE ="com.ss.android.ugc.aweme:id/f4j"; AccessibilityNodeInfo shareElement = findViewByID(ID_SHARE); PerformViewClick (shareElement);Copy the code

Note that when the Share dialog is first displayed, the copy video link button is not visible

Therefore, swipe left at the bottom of the Share dialog until the copy video link button is visible and then click to copy the address of the current video to the system clipboard

//DouYinService. Java // Swipe left to the copy button to seewhile (true)
{
    if (null == findViewByIDAndText(ID_SHARE_TAG, TEXT_SHARE_TAG))
    {

          break;
    }

    AccessibilityNodeInfo copyElement = findViewByIDAndText(ID_COPY_LINK, TEXT_COPY_LINK);
    if (null == copyElement)
    {

          Runtime.getRuntime().exec("adb shell input swipe 900 1600 300 1600");
    } else
    {
          Log.d("xag"."Find the copy button and click on it.");
          performViewClick(copyElement);
          result = true;
          break; }}Copy the code

The fourth step is to obtain the real address of the video. The content is read from the system clipboard, and then the regular expression is used to filter out the real share address of the current video

// stringutil. Java /*** * uses regular expressions to filter out the real video address * @param data * @return
 */
public static String findUrlByStr(String data)
{
    Pattern pattern = Pattern.compile("https? ://[-A-Za-z0-9+&@#/%?=~_|!:,.;] +[-A-Za-z0-9+&@#/%=~_|]");
    Matcher matcher = pattern.matcher(data);
    if (matcher.find())
    {
        return matcher.group();
    }
    return "";
}
Copy the code

Step 5: Crawl the video length to get the real address of the video, open it with Chrome browser for analysis, and find a redirection

Use Jsoup to simulate the above operation, connect to the share address of the video, and obtain the URL after the video is redirected

import org.jsoup.Connection; import org.jsoup.Jsoup; Url = jsoup.connect (url).followredirects (true)
      .execute().url().toExternalForm();
Copy the code

Analysis revealed that the redirected address contained the ID of the video

In addition, the following request parameters happen to contain the video ID, and the returned results contain information such as the duration of the video

Therefore, we just need to filter out the video ID from the address and then simulate the request above

String Item_id = stringutil. getSubUtil(url,"video/(.*?) / \ \? region").get(0); // New address String new_URL ="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + item_id;

Connection connection = Jsoup.connect(new_url).ignoreContentType(true); Connection data = connection.headers(Header.generateHeader()); String result = data.get().body().html();Copy the code

Finally, create a new entity class VideoNewItem and use FastJson to parse the data and extract the duration of the video

VideoNewItem item = json.parseObject (result, videonewitem.class); Int videoDuration = item.getitem_list ().get(0).getduration (); Log.d("xag"."Video Length :" + videoDuration + "Start waiting...");
Copy the code

Step 6: Wait for the video after getting the length of the video, you can perform the wait operation and then slide up the page to jump to the next video

// Countdown, waiting time to complete thread. sleep(videoDuration); Log.d("xag"."Wait for completion, ready to slide to the next video."); // Slide to the next video try {runtime.getruntime ().exec()"adb shell input swipe 600 1200 600 600");
} catch (IOException e)
{
    e.printStackTrace();
}
Copy the code

5. The last

Repeat the above operation, that is, you can crawl the video length, automatically brush douyin short video, completely free your hands

I have uploaded all the source code in the article to the public account background, follow the public account “AirPython” reply “dyauto” can get all the source code

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!

Recommended reading

Talk about Python to do wechat applet automation, those who step on the pit?

In order to protect the little sister’s eyes, I made a voice robot with automation

Automated essay | the folded circle of friends? There will be no automation