interaction_poc.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import datetime
  2. import random
  3. import time
  4. import requests
  5. import uiautomator2 as u2
  6. from tools.ip_util import get_local_ip, get_city
  7. def dong_action(mobile, device_serial, keyword, content):
  8. # 连接设备
  9. d = u2.connect(device_serial)
  10. d.healthcheck()
  11. d.screen_on()
  12. d.unlock()
  13. d.debug = False
  14. # 关闭app
  15. d.app_stop('com.ss.android.auto')
  16. # 启动app
  17. d.app_start('com.ss.android.auto', use_monkey=True)
  18. txt = content.split('+')
  19. print('len', len(txt))
  20. print('type', type(txt))
  21. time.sleep(3)
  22. '''
  23. 搜索、点赞、收藏、回帖
  24. '''
  25. d.xpath('//*[@resource-id="com.ss.android.auto:id/bug"]').click_exists()
  26. d(focused=True).set_text(keyword)
  27. time.sleep(2)
  28. d.xpath('//*[@resource-id="com.ss.android.auto:id/fd4"]').click_exists()
  29. time.sleep(3)
  30. d.xpath('//*[@text="视频"]').click_exists()
  31. time.sleep(1)
  32. while 1:
  33. # 视频点击
  34. for elem in d.xpath('//android.view.View').all():
  35. d.click(0.23, 0.261)
  36. time.sleep(2)
  37. # 点赞
  38. d.xpath('//*[@resource-id="com.ss.android.auto:id/dxu"]/android.widget.ImageView[1]').click_exists()
  39. time.sleep(1)
  40. # 收藏
  41. d.xpath('//*[@resource-id="com.ss.android.auto:id/d1z"]').click_exists()
  42. time.sleep(2)
  43. # 回帖
  44. k = d.xpath('//*[@resource-id="com.ss.android.auto:id/j_b"]')
  45. j = d(text='说点什么...')
  46. if k:
  47. k.click_exists()
  48. if j:
  49. j.click_exists()
  50. time.sleep(3)
  51. # d.xpath('//*[contains(name(), "EditText")]').click_exists()
  52. d(focused=True).set_text(random.choice(txt))
  53. # d.send_keys(t)
  54. # print('text:', t)
  55. m = d.xpath('//*[@resource-id="com.ss.android.auto:id/f6f"]')
  56. # n = d(text='发布')
  57. if m:
  58. d.xpath('//*[@resource-id="com.ss.android.auto:id/f6f"]').click_exists()
  59. # if n:
  60. # d(text='发布').click()
  61. # time.sleep(3)
  62. # d.click(0.479, 0.23)
  63. # 返回
  64. d.xpath('//*[@resource-id="com.ss.android.auto:id/de"]').click_exists()
  65. #
  66. d.swipe_ext('up', scale=0.265)
  67. # mobile, operate_type, ip, content, operate_time, result, channel, city
  68. ip = get_local_ip()
  69. if ip is not None:
  70. city = get_city(ip)
  71. else:
  72. ip = ''
  73. city = ''
  74. # mobile = '13816996346'
  75. # 记录回帖数据
  76. url = 'http://localhost:8066/backend/operate'
  77. params = {"channel": "dongchedi", "city": city, "ip": ip, "mobile": mobile, "operateTime": datetime.datetime.now(), "operateType": "2", "result": "操作成功"}
  78. response = requests.post(url, data=params)
  79. if response.ok:
  80. result = response.json()
  81. print(result)
  82. else:
  83. print('请求失败:', response.status_code)
  84. def dongcd_stop(device_serial):
  85. # 连接设备
  86. d = u2.connect(device_serial)
  87. d.healthcheck()
  88. d.screen_on()
  89. d.unlock()
  90. d.debug = True
  91. # 关闭app
  92. d.app_stop('com.ss.android.auto')