Python takes you to grab a video red envelope, not let go of a red envelope!

Keywords: Python Attribute Mobile Android

Today, I want to share with you a red packet of how to use Python technology to capture video! Not only can you learn Python technology, but you can also make money. Hand-in-hand teaching, with source code! After reading, I think we can have a wave of watching + forwarding!

1

Target scenario

Python Resource Sharing Group: 626017123

In today's era of short video, a short video as the leader, backed by strong financial backing, madly bombing platform users with red envelopes.

 

 

 

 

 

Unlike traditional red packets, video red packets contain location uncertainty, size uncertainty, element ID uncertainty and so on. These uncertainties will lead to the operation of red packets grabbing becomes extremely complex.

The purpose of this article is to use Python automation to realize the operation of "grabbing video red envelope".

ps: This article is only for learning and communication, not for other purposes.

2

Preparation

Before you start writing scripts, you need to prepare as follows

1. An Android Phone and the ADB Running Environment Configured on the PC

2. Installation of Automation Dependency Library and Picture Contrast Dependency Library in Python Virtual Environment

3. PS or other photo editing software

#Automation dependence
pip3 install pocoui
#Picture contrast dependence
pip3 install aircv

 

3

Write code

In the first step, we need to use Airtest to drive the mobile phone to open the target short video App.

#The package name and LaunchActivity of the target application
self.package_name = 'com.**.weishi'
self.home_activity = 'com.**.oscar.module.splash.SplashActivity'
#Back to the main interface
home()
# stop_app(self.package_name)
#Open applications
start_my_app(self.package_name, self.home_activity)

 

The first time you open an application, a warning dialog pops up.

 

 

 

 

 

Here we use "asynchronous threads" to process.

Loop detection dialog box elements exist, once it appears, it simulates the click operation, closes the dialog box.

def __handle_dialog(self):
    """
    //Handling warning dialog box
    :return:
    """
    count = 0
    while count < self.wait_for_dialog_timeout:
         tip_notice = self.poco('com.tencent.weishi:id/title_text', text=u'Teenagers Protection Function Tips')
         try:
            if tip_notice.exists():
                #Shut down
                print('A warning dialog box appears and closes it.')
                self.poco('com.tencent.weishi:id/close_btn').click()
                break
            else:
                pass
         except Exception as e:
            print('It's abnormal.')
         time.sleep(1)
         count += 1
#Asynchronous processing
threading.Thread(target=self.__handle_dialog, name='thread1').start()

 

The second step is to "judge" whether the video currently playing is a video containing a red envelope.

Through the observation of a large number of videos, we can see that there are two types of red packets in the video, namely ordinary video red packets and question-and-answer video red packets.

 

 

 

 

 

View the interface elements through Android SDK's automatic tool Monitor.

Unfortunately, this interactive red envelope element identifies "element ID does not exist" and the Text attribute is empty, which is not available in the traditional way.

 

 

 

 

 

 

In another way, I use the "picture contrast" technology to judge whether the interactive red envelope element exists, and then judge whether the video is a video containing red envelope.

Use "PS" to cut out the interactive red envelope element image from the screen shot.

It should be noted that because of the irregular shape of this element, only a regular rectangular area can be cut here, and other redundant areas can not be intercepted, otherwise the image comparison will fail.

 

 

 

 

 

Then use the adb command to intercept the picture of the mobile phone screen, and then save it to the local area.

def save_screenshot_to_pc(desc):
    """
    //Get screen shots
    desc Screen save path
    :return:
    """
    exec_cmd('adb shell /system/bin/screencap -p /sdcard/screenshot.png')
    exec_cmd('adb pull /sdcard/screenshot.png %s' % desc)

 

Then we can use the "aircv" library to compare the clipped pictures and screen captures to determine whether the clipped interactive red envelope elements can match.

 

When the matching index is above 0.8, it is assumed that the current video must contain red packets.

def find_image(source_path, part_path):
    """
    //Matching template
    :param source_path: Original picture
    :param part_path: Pictures to be matched
    :return:
    """
    #Original image
    source = ac.imread(source_path)
    #Parts to be found
    part = ac.imread(part_path)
    result_raw = ac.find_template(source, part)
    #Matching picture center coordinates
    if result_raw and result_raw.get('confidence') >= 0.8:
        center_position = result_raw.get('result')
        print(result_raw)
    else:
        center_position = None
    return center_position

 

In the third step, if we judge that the current video contains interactive red envelope elements, we can perform the operation of "grabbing red envelope".

Firstly, using Monitor to intercept the interface element tree at the moment when the red envelope appears, we can still see that the red envelope image elements do not have ID and Text attributes.

 

 

 

 

 

Then I first thought about whether we could use the above method to get the center coordinates of the red envelope image by local image matching.

 

 

 

 

 

However, through a lot of tests, it is found that the size of red envelope image elements in video is "uncertain", and the pictures intercepted by PS can not be applied to all videos.

 

 

 

 

 

Finally, only by analyzing the hierarchical structure of the element, can we get the nearest parent element with the ID of the element, and then get the red envelope element, and then get the bound attribute value.

vp = self.poco('com.tencent.weishi:id/hippy_container')
if vp.exists():
     #Element
     try:
           red_package_element = vp.children()[0].children()[0].children()[0].children()[0]
     except Exception:
           print('Generate an exception')
           continue
     #Get bound() attribute
     element_size = red_package_element.get_bounds()

 

After getting the bound attribute of the red envelope picture element, the coordinates of the center point of the red envelope picture element can be calculated.

 

 

 

 

 

def get_element_center_position(poco, bound):
    """
    //Get the coordinates of the central point of the element
    :return:
    """
    #Get the width and height of the mobile screen
    screen_width = poco.get_screen_size()[0]
    screen_height = poco.get_screen_size()[1]
    #Central coordinates of elements
    center_position = (bound[1] + bound[-1]) / 2 * screen_width, (
            bound[0] + bound[2]) / 2 * screen_height
    return center_position

 

We all know that in a video, the time of red envelope appears is uncertain.

By comparing the element tree before and after the appearance of red envelope picture, we can find that when red envelope appears, the element of red envelope picture "has many sub-elements".

So you can loop to get the UI element tree until the red envelope element has child elements, and then exit the loop.

#Waiting for the red envelope element to appear before clicking
if len(red_package_element.children()) > 0:
     print(center_position)
     break
else:
     #print('Wait for the red envelope to appear and click')
     pass

 

By clicking on the "center coordinates" of the red envelope element, the final operation of grabbing the video red envelope can be completed.

 #After obtaining the red envelope coordinates, click and click until the red envelope is grabbed.
exec_cmd('adb shell input tap %d %d' % (center_position[0], center_position[1]))

 

 

4

RESULTS CONCLUSION

Through the above operation, the operation of grabbing ordinary video red packets is completed, and the circular operation can get all the ordinary video red packets in the application.

 

For the red packet of Q & a video, the text content of questions and answers can be obtained directly by using Monitor, which needs to match the content of questions and answers once.

Posted by robotta1530 on Fri, 20 Sep 2019 22:50:58 -0700