account_culture_poc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import random
  2. import subprocess
  3. import time
  4. import uiautomator2 as u2
  5. from uiautomator2 import Direction
  6. from tools import loggerKit
  7. from tools.common_util import cap_device
  8. class shell_status_content:
  9. def __init__(self, execute_status, error_code, error_msg, result, content, nickname):
  10. """
  11. :rtype: object
  12. """
  13. # 0失败 1成功
  14. self.execute_status = execute_status
  15. self.error_code = error_code
  16. self.error_msg = error_msg
  17. self.result = result
  18. self.content = content
  19. self.nickname = nickname
  20. def to_dict(self):
  21. return {
  22. 'execute_status': self.execute_status,
  23. 'error_code': self.error_code,
  24. 'error_msg': self.error_msg,
  25. 'result': self.result,
  26. 'content': self.content,
  27. 'nickname': self.nickname
  28. }
  29. # 保活任务 不打开app
  30. def keep_alive_sub_type(device):
  31. cmd_conn = f"adb connect {device}"
  32. conn_output = subprocess.check_output(cmd_conn, shell=True)
  33. print(f"{conn_output}")
  34. d = u2.connect(device)
  35. d.screen_on()
  36. d.unlock()
  37. d.debug = False
  38. swipe_list = ['right', 'left']
  39. d.swipe_ext(random.choice(swipe_list), 1)
  40. status = shell_status_content(1, 0, '', '', '', '')
  41. return status
  42. # 懂车帝 v7.9.0
  43. # 懂车帝demo
  44. def simulated_demo_operation(device, task_id):
  45. cmd_conn = f"adb connect {device}"
  46. conn_output = subprocess.check_output(cmd_conn, shell=True)
  47. print(f"{conn_output}")
  48. d = u2.connect(device)
  49. d.screen_on()
  50. d.unlock()
  51. d.debug = False
  52. d.app_stop("com.ss.android.auto")
  53. d.app_start('com.ss.android.auto', use_monkey=True)
  54. time.sleep(2)
  55. d.xpath('//*[@resource-id="com.ss.android.auto:id/gxf"]').click_exists(timeout=0.25)
  56. swipe_screen(d)
  57. swipe_screen(d)
  58. time.sleep(1)
  59. # 如果弹出问答页面 处理问答页面
  60. # handleQuestion(d)
  61. # 模拟随机滑动几屏 随机浏览文章
  62. # randomView(d)
  63. # 搜索关键词
  64. searchKeyword(d, task_id)
  65. status = shell_status_content(1, 0, '', '', '', '')
  66. return status
  67. # 懂车帝 v7.9.0
  68. # 3-5 天的养号操作
  69. def simulated_operation(device, title, task_id):
  70. cmd_conn = f"adb connect {device}"
  71. conn_output = subprocess.check_output(cmd_conn, shell=True)
  72. loggerKit.info(f"{conn_output}")
  73. d = u2.connect(device)
  74. d.screen_on()
  75. d.unlock()
  76. d.debug = False
  77. try:
  78. d.app_stop("com.ss.android.auto")
  79. d.app_start('com.ss.android.auto', use_monkey=True)
  80. # 跳过开屏广告
  81. d.watcher.when("跳过广告").click()
  82. # 跳过升级弹框
  83. d.watcher.when("以后再说").click()
  84. # 位置授权
  85. d.watcher.when("本次运行允许").click()
  86. # 稍后
  87. d.watcher.when("稍后").click()
  88. # 开始后台监控
  89. # 默认监控间隔2.0s
  90. d.watcher.start(2.0)
  91. # 进入我的获取账号信息
  92. d.xpath('//*[@resource-id="android:id/tabs"]/android.widget.RelativeLayout[5]').click()
  93. time.sleep(2)
  94. account_name = ""
  95. if d.xpath('//*[@resource-id="com.ss.android.auto:id/god"]').exists:
  96. account_name = d.xpath('//*[@resource-id="com.ss.android.auto:id/god"]').get_text()
  97. # 点击到首页
  98. # d.xpath('//*[@resource-id="android:id/tabs"]/android.widget.RelativeLayout[1]').click()
  99. if d.xpath('//*[@text="首页"]').exists:
  100. d.xpath('//*[@text="首页"]').click()
  101. else:
  102. d.xpath('//*[@resource-id="android:id/tabs"]/android.widget.RelativeLayout[1]').click()
  103. time.sleep(2)
  104. d.xpath('//*[@resource-id="com.ss.android.auto:id/gxf"]').click_exists(timeout=0.25)
  105. swipe_screen(d)
  106. swipe_screen(d)
  107. time.sleep(1)
  108. # 如果弹出问答页面 处理问答页面
  109. handleQuestion(d)
  110. # 模拟随机滑动几屏 随机浏览文章
  111. randomView(d)
  112. time.sleep(3)
  113. # 搜索关键词
  114. searchKeyword(d, title)
  115. # 点击车友圈按钮
  116. d.xpath('//*[@text="车友圈"]').click()
  117. time.sleep(3)
  118. # 进入车友圈
  119. if d.xpath('//*[@text="进入车友圈"]').exists:
  120. d.xpath('//*[@text="进入车友圈"]').click()
  121. else:
  122. d.xpath('//*[@text="进圈"]').click()
  123. time.sleep(2)
  124. # 点击加圈
  125. circleTextView = d(resourceId="com.ss.android.auto:id/ief")
  126. if circleTextView.exists:
  127. text = circleTextView.get_text()
  128. if text == "加圈":
  129. circleTextView.click_exists(timeout=1)
  130. time.sleep(0.5)
  131. carFriendWindow = d(resourceId="com.ss.android.auto:id/ala")
  132. if carFriendWindow.exists:
  133. d.xpath('//*[@resource-id="com.ss.android.auto:id/bpd"]').click_exists(timeout=1)
  134. # 切换到精华tab
  135. # d.xpath('//*[@text="精华"]').click_exists(timeout=2)
  136. # time.sleep(0.25)
  137. currentNum = 0
  138. # 滑动几屏
  139. while currentNum < random.randint(3, 5):
  140. d.swipe_ext(Direction.FORWARD)
  141. currentNum += 1
  142. time.sleep(random.random())
  143. # 随机滑动点击帖子
  144. click = 0
  145. while click <= random.randint(3, 5):
  146. print("each = ", click)
  147. readPost(d)
  148. click += 1
  149. d.swipe_ext(Direction.FORWARD)
  150. # 切换到热门帖子
  151. hotTabView = d.xpath('//*[@text="热门"]')
  152. if hotTabView.exists:
  153. hotTabView.click(timeout=1)
  154. time.sleep(0.25)
  155. # 随机滑动热门列表列表 、点击帖子
  156. for each in range(1, 4):
  157. readPost(d)
  158. d.swipe_ext(Direction.FORWARD)
  159. status = shell_status_content(1, 0, '', '', '', account_name)
  160. d.app_stop("com.ss.android.auto")
  161. return status
  162. except Exception as e:
  163. cap_device(d, device, task_id)
  164. exception_status = shell_status_content(0, 500, str(e), '', '', '')
  165. d.app_stop("com.ss.android.auto")
  166. loggerKit.error(f"设备{device}, 任务{task_id}, dongchedi v7.9.0 自动化养号 异常: {str(e)}")
  167. return exception_status
  168. # 搜索
  169. def searchKeyword(d, keyword):
  170. if d.xpath('//*[@resource-id="com.ss.android.auto:id/c9c"]').exists:
  171. d.xpath('//*[@resource-id="com.ss.android.auto:id/c9c"]').click()
  172. else:
  173. d.xpath('//*[@resource-id="com.ss.android.auto:id/d34"]').click()
  174. d.send_keys(keyword, clear=True)
  175. time.sleep(1)
  176. # 点击搜索
  177. d.xpath('//*[@resource-id="com.ss.android.auto:id/g9e"]').click(timeout=5)
  178. time.sleep(2)
  179. # 随机浏览
  180. def randomView(d):
  181. recyclerView = d(resourceId="com.ss.android.auto:id/hgo", className="androidx.recyclerview.widget.RecyclerView")
  182. if not recyclerView.exists:
  183. d.swipe_ext("right", 1)
  184. return
  185. num = 0
  186. # 循环随机浏览
  187. while True:
  188. if num >= 3:
  189. break
  190. num += 1
  191. layoutG = recyclerView.child(resourceId="com.ss.android.auto:id/dv6", className="android.widget.LinearLayout")
  192. if not layoutG.exists:
  193. print("dv_ layout 不存在 则滑动")
  194. swipe_screen(d)
  195. continue
  196. i = random.randint(0, len(layoutG) - 1)
  197. layoutEle = layoutG[i]
  198. # 点击对应的控件 可能是帖子 视频 图片等
  199. layoutEle.click_exists(timeout=1)
  200. # 进入详情之后之后
  201. forwardNo = random.randint(1, 5)
  202. # 随机向下滑动 forwardNo 次
  203. j = 0
  204. while j <= forwardNo:
  205. imageView = d(resourceId="com.ss.android.auto:id/cz")
  206. if imageView.exists:
  207. print("点击的图片 不滑动 直接结束")
  208. time.sleep(1)
  209. break
  210. d.swipe_ext(Direction.FORWARD)
  211. j += 1
  212. time.sleep(0.25)
  213. # 随机睡眠 1 ~ 5秒
  214. time.sleep(random.randint(1, 5))
  215. # 回到推荐列表
  216. d.swipe_ext("right", 1)
  217. time.sleep(1)
  218. swipe_screen(d)
  219. time.sleep(0.5)
  220. # 处理问答
  221. def handleQuestion(d):
  222. question = d(resourceId="athena-question-7130163961458065439")
  223. if question.exists:
  224. questionViewG = question.child(className="android.view.View")
  225. if questionViewG.exists:
  226. d.xpath('//*[@resource-id="athena-question-7130163961458065439"]/android.view.View[3]').click()
  227. d.xpath('//*[@text="提交"]').click()
  228. # 进入文章进行处理
  229. def readPost(d):
  230. # 判断右上角是否存在三个点
  231. # 车友圈
  232. recyclerView = d(resourceId="com.ss.android.auto:id/dv6")
  233. if recyclerView.exists:
  234. recyclerView.click()
  235. else:
  236. return
  237. time.sleep(2)
  238. if d.xpath('//*[@resource-id="com.ss.android.auto:id/iv_more"]').exists:
  239. print('进入车友圈')
  240. swipe = 0
  241. while swipe <= random.randint(5, 8):
  242. time.sleep(1)
  243. d.swipe_ext("up", 1)
  244. swipe += 1
  245. d.swipe_ext("right", 1)
  246. # 提问
  247. elif d.xpath('//*[@resource-id="com.ss.android.auto:id/kad"]').exists:
  248. print('进入提问')
  249. swipe = 0
  250. while swipe <= random.randint(5, 8):
  251. time.sleep(1)
  252. d.swipe_ext("up", 1)
  253. swipe += 1
  254. d.swipe_ext("right", 1)
  255. # 长文
  256. elif d.xpath('//*[@resource-id="com.ss.android.auto:id/ee1"]').exists:
  257. print('进入长文')
  258. swipe = 0
  259. while swipe <= random.randint(5, 8):
  260. time.sleep(1)
  261. d.swipe_ext("up", 1)
  262. swipe += 1
  263. d.swipe_ext("right", 1)
  264. elif d.xpath('//*[@resource-id="com.ss.android.auto:id/c5n"]').exists:
  265. # 短视频
  266. print('进入短视频')
  267. random_sleep = random.randint(10, 20)
  268. time.sleep(random_sleep)
  269. d.swipe_ext("right", 1)
  270. else:
  271. print('进入长视频')
  272. random_sleep = random.randint(10, 20)
  273. time.sleep(random_sleep)
  274. d.swipe_ext("right", 1)
  275. # 随机滑动屏幕
  276. def swipe_screen(d):
  277. # 获取设备的屏幕尺寸
  278. screen_width = d.info["displayWidth"]
  279. screen_height = d.info["displayHeight"]
  280. print("screen_width 屏幕的宽度:", screen_width, "屏幕的长度:", screen_height)
  281. # 设置滑动的起始点和终止点,可以根据需要进行调整
  282. start_x = screen_width // 2 # 屏幕宽度的一半
  283. start_y = random.randint(1000, screen_height) # 起始点纵坐标,可以根据需要调整
  284. end_x = screen_width // 2
  285. end_y = random.randint(10, 300)
  286. # 执行滑动操作
  287. d.swipe(start_x, start_y, end_x, end_y)
  288. # d.swipe_ext(Direction.FORWARD)
  289. # 等待滑动完成,可以根据需要进行适当的等待
  290. time.sleep(1)
  291. if __name__ == "__main__":
  292. simulated_operation("QXNUT21905001550", "飞凡F7", 12121)