What did this reptile do?

  1. Use ADB to control your phone and automatically swipe Tiktok for you
  2. Call baidu face recognition interface, to the video screen appears in the little sister score
  3. Automatically like videos of little sisters whose appearance level is above 70

What’s interesting is that according to Douyin’s recommendation algorithm, after a few days of browsing, douyin’s recommendations are full of pretty girls.

0. Achievement Display

Let me show you how it works.

Connect to your mobile phone (which should allow ADB debugging) and run the crawler, which will automatically open the Douyin APP and automatically find your beautiful little sister.

What’s interesting is that according to Douyin’s recommendation algorithm, after a few days of browsing, douyin’s recommendations are full of pretty girls. At the beginning of the brush, there may not be a beautiful little sister in dozens of them, but after a few days, basically every video you brush is a beautiful little sister. Look, this is the “results” I have been brushing for three days, intermittently brushing for three days, has “liked” more than 200 beautiful little sister videos.

Let’s just say, regardless of whether douyin’s beauty filters are powerful or not, judging from the crawly videos, these little sisters are really pretty and the results are satisfactory.

The following is a screenshot of a beautiful little sister detected in Douyin. Originally, this was a temporary file, which was deleted after face detection. I looked good, so I kept it.

That’s what this crawler looks like, and here’s how it’s done. 1. Basic preparation This crawler needs three things. The Python environment, the crawler is written in Python, must be configured first Python environment. Install Anaconda without one. In addition, you need to install some necessary libraries, such as Requests, URllib, PIL, etc. In THE ADB environment, our crawler needs to operate the mobile phone through ADB commands to realize the functions of automatically brushing and clicking “like”. Installation method self – calibration. Face recognition API, I use baidu AI face recognition interface, you need to register a Baidu AI open platform account, and then create a face recognition application, and then appID, API_key, secret_key three parameters to fill in the crawler code here.

Here is a brief introduction to ADB.

The full name of ADB is Android Debug Bridge, which can be used to Debug Android applications easily.

Simply put, you can use this tool to manipulate your phone, and it can do anything you can do on your phone.

Such as installing/uninstalling/opening/closing mobile apps, swiping, tapping, long pressing, etc. More complex operations can also be done, which we won’t cover here, because that’s basically all the ADB instructions our Tik Tok crawler uses. A. ADB to start the application, only need to know the package_name and activity_name of the application, and then call the following ADB directive to open the corresponding application

adb shell am start -n [package_name] [activity_name]

Take Douyin APP for example, its package_name and activity_name are as follows.

The initial Activity package_name = 'com.ss.android.ugc. Aweme 'activity_name = 'com.ss.android.ugc.aweme.splash.SplashActivity'Copy the code

Therefore, run the following command in the command line to open the Tiktok APP in the mobile phone.

adb shell am start -n com.ss.android.ugc.aweme com.ss.android.ugc.aweme.splash.SplashActivity
Copy the code

B. ADB click on the screen

Run the following command in the command line, you can realize the mobile phone screen click. 1330, 1750 are the x and y coordinates of the click.

adb shell input tap 1330 1750
Copy the code

Taking douyin praise in the crawler as an example, I just need to record the screen coordinates corresponding to the “like” button in Douyin App, and then call this command to complete automatic “like”.

C. ADB slide screen

Run the following command in the command line to achieve the sliding operation of the mobile phone screen. There are five parameters. These five numbers are the x and y coordinates of the starting point of the slide, the x and y coordinates of the ending point of the slide, and the length of the slide.

adb shell input swipe 900 1400 400 1400 100
Copy the code

For example, the above command indicates that the slide from coordinate (900,1400) to coordinate (400,1400) takes 100 milliseconds.

In fact, this instruction is slightly modified, set the start and end coordinates to be the same, set the slide time to be longer, and the slide operation becomes “long press the screen” operation.

That’s about all the basic preparation. Here’s the whole idea of a crawler.

2. Overall idea of crawler

As shown in the figure, the whole crawling idea of crawler is as follows.

First, start douyin APP. After the first video is loaded and played, the screen is captured and the face recognition interface of Baidu AI is called for face detection and scoring.

If the appearance level score is above 70 and the gender is female face detected in the screen, click the “Like” button in the screen, and then slide up to play the next video

If there is no face in the picture, or the gender is male, or the appearance level is less than 70, the screenshots will continue to be detected until the end of the video, or until the face that meets the conditions is found, and then the next video will be played by sliding up. In order not to miss douyin with a pretty little sister as much as possible, we will take 4-5 pictures from each video for detection. As long as there is a screenshot with a high appearance level of the little sister, we will give the video a “like”. If this is not detected, can only say that the little sister is hidden too deep, or beautiful not obvious enough

This is probably the idea, let’s start to complete the crawler.

3. Write crawlers by hand

Since the crawler is still a little bit big, I’ll break it down into three parts. Face recognition section, ADB control section, and crawler main logic section.

(1) Face recognition part

This part is used to call the baidu AI face recognition interface, you need to apply your own appID, API_key, secret_key three parameters to fill in the code.

Import base64 import urllib import json import requests import sys APPID = 'Fill in your application APPID' API_key = 'Fill in your application API_key' Secret_key = 'Fill in the secret_key you requested'Copy the code

This section of the code mainly consists of four functions, including

  • The get_access_token function is used to obtain the access_token parameter required by the face recognition interface.

  • The identify_faces and parse_face_pic functions, which upload images, call the face recognition interface for face recognition, and return a list of recognized faces.

  • Analysis_face function, the main function is to parse the list of faces returned by the first two functions, so as to determine whether there is a beautiful little sister in the picture.

    def get_access_token(): The validity period for obtaining an Access_token is generally one month. Client_id = API_key client_secret = secret_key AUTH_URL = ‘aip.baidubce.com/oauth/2.0/t… ‘+ client_id + ‘&client_secret=’ + client_secret header_dict = {‘ user-agent ‘: ‘Mozilla/5.0 (Windows NT 6.1; Trident / 7.0; Rv :11.0) like Gecko’, “content-type “: “application/json”}

    Response_at = requests. Get (auth_URL, headers=header_dict) json_result = json.loads(response_at.text) access_token = json_result['access_token'] return access_tokenCopy the code

    Def identify_faces(pic_URL, pic_type, url_fi): headers = {‘ content-type ‘: ‘application/json; charset=UTF-8’ }

    if pic_type == TYPE_IMAGE_NETWORK: image = pic_url image_type = 'URL' else: with open(pic_url, 'rb') as file: image = base64.b64encode(file.read()) image_type = 'BASE64' post_data = { 'image': image, 'image_type': image_type, 'face_field': 'facetype,gender,age,beauty', # expression,faceshape,landmark,race,quality,glasses 'max_face_num': 2 } response_fi = requests.post(url_fi, headers=headers, Data =post_data) json_fi_result = json.loads(response_fi.text) # If not json_fi_result or json_fi_result['error_msg']! = 'SUCCESS': return None else: return json_fi_result['result']['face_list']Copy the code

    Def parse_face_pic (pic_url, pic_type access_token) : “” “face recognition, return to face list “” “url_fi = ‘aip.baidubce.com/rest/2.0/fa… ‘ + access_token

    Json_faces = identify_FACES (pic_URL, pic_type, url_FI) if not json_FACES: return None else: return json_facesCopy the code

    Def analysis_face(face_list): “”” def analysis_face(face_list): “”” If face[‘gender’][‘type’] == ‘female’: age = face[‘age’] beauty = face[‘beauty’]

    If beauty >= 70: print(' %d '+ STR (age) +' %d ') ' % beauty) find_plxjj= True break else: Print (' find a '+ STR (age) +' age ', %d, fail, continue ~' % beauty) continue return find_plxjjCopy the code

(2) ADB control part

ADB control part, the general idea is through python OS library to execute ADB instructions, to operate on the phone.

It mainly includes four functions:

  • The start_my_app function is used to launch tiktok APP.

  • Save_video_met, click the “like” button. Called after pretty little sister is detected.

  • The play_next_video function slides up to play the next video.

  • Get_screen_shot_part_img function, used to capture the screen image, save to the computer for subsequent face recognition.

    import os from PIL import Image

    The application package name and initial Activity of Douyin App

    package_name = ‘com.ss.android.ugc.aweme’ activity_name = ‘com.ss.android.ugc.aweme.splash.SplashActivity’

    def start_my_app(package_name, activity_name): Popen (‘adb shell am start -n %s/%s’ % (package_name, activity_name))

    def save_video_met(screen_name, find_girl_num): Img = image.open (screen_name).convert(‘RGB’) img.save(” beautiful little sister /DYGirl_%d.jpg” % find_girl_num) # os.system(“adb shell input tap 1330 1750”)

    Os. system(“adb shell input swipe 540 1300 540 500 100″) def play_next_video(): # adb shell input swipe 540 1300 540 500 100”)

    def get_screen_shot_part_img(image_name): Os.system (“adb shell /system/bin/screencap -p /sdcard/screenshot.jpg”) os.system(“adb pull /sdcard/screenshot.jpg”) os.system(“adb pull /sdcard/screenshot.jpg” Img = image.open (image_name).convert(‘RGB’) # img = image.open (image_name).convert(‘RGB’) # img = image.open (image_name).convert(‘RGB’) Img = img.crop((0, 400, 1200, 2750)) img.thumbnail((int(w / 1.5), Int (h / 1.5)) # save to local img.save(image_name) return image_name

The coordinates in these functions are written according to the screen coordinates of my mobile phone (Huawei Mate 20 Pro). Students with other mobile phones can adjust these parameters according to the screen resolution of their mobile phones.

And the way to change the coordinates so I’m going to give you a general idea, go to your phone’s Settings, go to Developer, and turn the pointer Position on, and you’ll see where you clicked on the screen, and the coordinates of the position you clicked on will be displayed at the top.

Turn on Tiktok, place your finger on the “like” button, and record the coordinates displayed at the top of the screen. Then replace the coordinates with the code.

(3) The main logic part of the program

According to the flow chart analyzed above, write the main logic of crawler program.

import datetime import time import shutil if __name__ == '__main__': Access_token = get_access_token() # set the maximum recognition time for a video. RECOGNITE_TOTAL_TIME = 10 # Recognite_count = 0 # Picture type [network and Local] TYPE_IMAGE_NETWORK = 0 TYPE_IMAGE_LOCAL = 1 Print (" open ") start_my_app(package_name, Activity_name) time.sleep(5) print(" start playing ~") find_girl_num = 0 Recognite_time_start = datetime.datetime.now() # recognite_count = 1 # recognite_count = 1 while True: Screen_name = get_screen_shot_part_img('images/temp%d.jpg' % recognite_count) # recognite_result = analysis_face(parse_face_pic(screen_name, TYPE_IMAGE_LOCAL, Access_token)) recognite_count += 1 # Recognite_time_end = datetime.datetime.now() # It's very recognite_count += 1 # Recognite_time_end = datetime.datetime recognite_result: Save_video_met (screen_name, find_girl_num) print(screen_name, find_girl_num) print(screen_name, find_girl_num) Recognite_time_end - recognite_time_start). Seconds < RECOGNITE_TOTAL_TIME: continue else: print(' time-out!! This is an unattractive video! Sleep (0.05) os.mkdir('./images') # Print ('==' * 30) # print('==' * 30) Time.sleep (2) print(' Ready to play next video ~') play_next_video() time.sleep(2) print(' Ready to play next video ~') play_next_video() time.sleep(2)Copy the code

Sometimes the network may be jammed, and the video takes some time to load, so every time you swipe to the next video, stop for two seconds and wait until the video is almost loaded before starting to check.

Afterword.

The reptile actually wrote it before, for his own amusement. This time just happens to be 1024 programmers festival, share for the benefit of our bald programmers little brothers.

This crawler I intermittently played for a period of time, anyway, no matter when the phone put aside, the crawler opened the background running, let it brush their own play. Then pick up the phone at night a look, wow phone is full of beautiful little sister. Ha ha ha ha

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base