Python takes you on a walk-on tour around the world

Keywords: Python Mobile Android shell

1. Target scenario

Eleven long holidays, I believe that most of your friends will be in the waves around the country or on the road, friends circle will become the place for you to perform.

Of course, there is a small friend who chooses to squatting at home. Do you feel bored? Do you want to go out and take part in the Friendship Photo Competition?

The purpose of this article is to use Python to take us on a walk-and-talk trip around the world and win the photo competition of the circle of friends.

2. Preparations

Before you start writing scripts, you need to do the following preparations:

1. A Android phone after Root.

2. Configuring Android ADB Development Environment

3. Installing pocoui dependency libraries in Python virtual environment

4. Virtual Location Application FackLocation

5. Choose a scenic spot, download some photos of the corresponding scenic spots, and prepare a forced text.

3. Coding

There are 6 steps to complete the operation. They are: get longitude and latitude through location, open virtual location service, import pictures to mobile albums, open circle of friends, select photo albums, edit content and choose geographical location, and release friends circle.

First, use the adb command to get the application package name and LaunchActivity of FackLocation and WeChat.

adb shell dumpsys activity | grep -i run

After getting the application package name and Launch Activity, it is very convenient to open FackLocation application and add geographic location by using Airtest and Monitor tools.

After reaching the search interface, the results of the search can not be found under Monitor.

Therefore, with the help of the pointer position in the developer's option, we can get the coordinates of the first item in the list, and use adb to perform click operation, which can complete the selection of longitude and latitude.

def __mock_location(self):
    """
    //Simulated positioning
    :return:
    """
    home()
    stop_app(package_name_location)
    start_my_app(package_name_location, activity_location)

    # Click Add Location
    self.poco('com.lerist.fakelocation:id/fab').click()

    # Click search
    self.poco('com.lerist.fakelocation:id/m_item_search').click()

    # Input box input destination
    self.poco('com.lerist.fakelocation:id/l_search_panel_et_input').set_text(self.location)

    sleep(2)

    # Width and height
    size = self.poco.get_screen_size()

    # Because the selected UI tree cannot be found, coordinates are used here to perform click operations.
    adb_click(500, 283)

    # Position determination
    while self.poco('com.lerist.fakelocation:id/a_map_btn_done').exists():
         self.poco('com.lerist.fakelocation:id/a_map_btn_done').click()

The second step is to judge whether the service is opened by the text content of the element. If it is not opened, it completes the opening of the positioning service by a click operation.

def __start_mock(self):
    """
    //Open analog position
    :return:
    """
    mock_element = self.poco('com.lerist.fakelocation:id/f_fakeloc_tv_service_switch')
    if mock_element.get_text() == 'Starting simulation':
         mock_element.click()
         # Waiting for startup simulation to complete
         self.poco('com.lerist.fakelocation:id/f_fakeloc_tv_service_switch', text='Stop simulation').wait_for_appearance()
    else:
         pass
    print('The simulation has been started.')

The third step is to import the pictures on the PC side into the mobile album directory by using the adb push command.

# Images waiting to be sent locally
files = get_all_files('./image/')

# Total number of images to be sent
self.image_num = 9 if len(files) > 9 else len(files)

# Directory of mobile albums
phone_image_path = 'sdcard/DCIM/Camera/'

# One picture into the mobile album.
for file in files[:self.image_num]:
    exec_cmd('adb push %s %s' % (file, phone_image_path))

It should be noted that after importing the pictures, you need to send a refresh broadcast again to find the pictures in the album.

# Update mobile albums
exec_cmd('adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///%s' % phone_image_path)

The fourth step is to open the client by using Airtest, and then automatically jump to the circle of friends.

def __open_friend_circle(self):
    """
    //Open the circle of friends
    :return:
    """
    # Waiting to be opened completely, App
    self.poco(text='WeChat').wait_for_appearance()
    self.poco(text='Mail list').wait_for_appearance()
    self.poco(text='find').wait_for_appearance()
    self.poco(text='I').wait_for_appearance()

    print('Fully open')

    # Click on Tab
    self.poco('com.tencent.mm:id/djv', text='find').parent().click()

    # Open the circle of friends
    self.poco('android:id/title', text='Circle of friends').click()

    # Waiting for Friend Loop Dynamic Loading Complete
    self.poco('com.tencent.mm:id/eyx').wait_for_appearance()

The fifth step is to select pictures from the mobile albums.

The photos of mobile albums are sorted according to time. According to the number of pictures pushed above, the photos in front of the album can be selected.

# Select a specified number of photos
cbs = self.poco('com.tencent.mm:id/ek8').offspring('com.tencent.mm:id/bwn')

index = 0

# Select a fixed number of photos
for cb in cbs:
    if index < self.image_num:
        cb.click()
    else:
        break
    index += 1

# Confirm Picture Selection
self.poco('com.tencent.mm:id/lm').click()

Step 6, enter the text content and select the specific location.

Because the virtual positioning refresh is not timely, in order to ensure the accuracy of the location, two consecutive positioning is carried out.

def __put_content_and_gps(self):
  """
  //Input content and location
  :return:
  """
  # Enter the content of the circle of friends
  self.poco('com.tencent.mm:id/d3k').set_text(self.msg)

  # The number of locations, usually twice
  location_count = 0

  # Click on the location Icon
  while True:

      self.poco('com.tencent.mm:id/d0a', text='location').click()

      # Waiting for results to appear in the search list
      self.poco('com.tencent.mm:id/du7').wait_for_appearance()

      if location_count == 0:
           # Return
           keyevent('BACK')
           location_count += 1
      else:
           # Exclude the first two items of ListView (no display, urban area), and click on the third item directly (specific location)
           self.poco('com.tencent.mm:id/dul').children()[2].click()
           break

Step 7, get the release button at the top, and you can publish the dynamics with one click.

For example, I sent a friend circle in TAM on YB.

4. RESULTS CONCLUSION

With the above operation, we can achieve any scenic spot in the country and play this sausage anytime and anywhere.

This article starts with the WeChat public address "AirPython". I have uploaded all the source code to the backstage. I can reply to the "global travel" after paying attention to the public address and get the download link.

If you think the article is good, please share it with us. You must be my greatest encouragement and support.

Posted by claypots on Sat, 05 Oct 2019 12:54:28 -0700