Recently, it was found that Douyin has a new function, which can display the online status of Douyin, as shown in the picture below:

On a whim, I decided to use Python to monitor one of my friends’ online status.

This monitoring mainly uses python appium library, there are many tutorials about the use and installation of Appium, but I have not tried any successful, so I bite my teeth to make a set of tutorials, tutorial at the end of the article, unsuccessful come to me!!

First, open Douyin with appium client and configure as follows:

{
  "platformName": "Android",
  "deviceName": "VOG-AL00",
  "appPackage": "com.ss.android.ugc.aweme",
  "appActivity": ".main.MainActivity",
  "noReset": "true",
  "fullReset": "false"
}
Copy the code

Click start Session:

Click on the message element:

Click again to enter the chat window:

The id of the presence element is:

com.ss.android.ugc.aweme:id/blc
Copy the code

So, we just need to keep requesting the value of this element in the current page to obtain the online status of this friend. If “online” is returned, the user is judged to be online. Otherwise, it is judged to be offline.

Python code up!!

1. The Appium module controls and opens Tiktok

desired_caps = {
            "platformName": "Android",
            "deviceName": "MHA_AL00",
            "appPackage": "com.ss.android.ugc.aweme",
            "appActivity": ".main.MainActivity",
            "noReset": "true",
            "fullReset": "false"
}
server = 'http://localhost:4723/wd/hub'
driver = webdriver.Remote(server, desired_caps)
Copy the code

2. Click to enter the message list and user chat window

driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Message")] '). Click () time. Sleep. (1) the driver find_element_by_xpath (' / / android. Widget. TextView [contains (@ text, ]').click()Copy the code

3. Obtain the online status and time and save the information

while True: status=driver.find_element_by_id('com.ss.android.ugc.aweme:id/blc').text strtime = time.strftime("%H:%M:%S", Time.localtime (time.time())) if status==' online ': status=' online 'else: Status =' not online 'wb = xlrd.open_workbook('1.xls') tabsheet = wb.sheets()[0] k = tabsheet.nrows #  0, i['name']) worksheet.write(k , 1, i['addr']) time.sleep(60)Copy the code

4. Draw a line chart based on the online status

import pandas as pd import pyecharts.options as opts from pyecharts.charts import Line df = Pd. read_excel(' online time.xls ',usecols=[' online time ', 'status']) x = df [r].' time 'values. The tolist () y = df [' status'] values. Tolist () line = (line () add_xaxis xaxis_data = (x) .add_yaxis(series_name=" online time ",y_axis=y, Is_step =True).set_global_opts(title_opts= opts.titleopts)) line.render_notebook()Copy the code

Can you find any secret in the picture?

The code for this article and the Appium installation and use tutorial can be obtained as follows:

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join the discussion and answer resource base