watch_goods.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """
  2. app: 头条极速版:
  3. desc:逛街
  4. """
  5. import base64
  6. import time
  7. from func.action_func import del_key_vague
  8. from scene.oprator.atom_data import single_click_by_control, click_pic, check_pic_exist, continual_swipe_screen, start_app, \
  9. stop_app
  10. from task.task_job import callback_task
  11. from tools import loggerKit, redis_client
  12. """
  13. 头条极速版
  14. 逛街
  15. """
  16. def watch_goods(task_id, data):
  17. loggerKit.info("[头条极速版]请求信息:{0}'.format(data))")
  18. device_id = data.get("deviceID")
  19. perform_action_id = data.get("performActionId")
  20. result = data.get("result")
  21. if result is not None:
  22. """
  23. 非首个指令
  24. """
  25. perform_action_result = result.get("performActionResult")
  26. if perform_action_result is None:
  27. return_dict = {
  28. "data": "",
  29. "code": -2,
  30. "message": "fail, performActionResult is null"
  31. }
  32. # 回调任务中心
  33. del_key_vague(device_id)
  34. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  35. return return_dict
  36. # 指令执行失败
  37. if perform_action_result == "failure":
  38. # 回调任务中心
  39. return_dict = {
  40. "data": "",
  41. "code": -2,
  42. "message": "fail, performActionResult is null"
  43. }
  44. del_key_vague(device_id)
  45. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  46. return return_dict
  47. # 终止指令
  48. if perform_action_result == "stop":
  49. # 回调任务中心
  50. return_dict = {
  51. "data": "",
  52. "code": -2,
  53. "message": "指令被用户终止"
  54. }
  55. del_key_vague(device_id)
  56. callback_task(500, '指令被用户终止', task_id, device_id, 0, None)
  57. return return_dict
  58. """
  59. 每次操作完成后会将对应的操作唯一id存储到redis
  60. 并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  61. """
  62. last_action_id = redis_client.get(device_id + "operate")
  63. step0 = redis_client.get(f"{device_id}_step0")
  64. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  65. last_action_id) and perform_action_result == "success":
  66. action1_id = int(round(time.time() * 1000))
  67. """
  68. 发送第1条指令
  69. 首页点击任务
  70. """
  71. action1_dict = single_click_by_control(action1_id, target_app="com.ss.android.article.lite",
  72. target_version="9.7.0.0",
  73. package_name="com.ss.android.article.lite",
  74. control_id="com.ss.android.article.lite:id/kp", timeout=30)
  75. redis_client.set(device_id + "operate", action1_id)
  76. redis_client.set(f"{device_id}_{action1_id}_step1", "1")
  77. redis_client.delete(f"{device_id}_step0")
  78. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action1_id)
  79. return action1_dict
  80. step1 = redis_client.get(f"{device_id}_{perform_action_id}_step1")
  81. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  82. last_action_id) and perform_action_result == "success":
  83. loggerKit.info("设备:{0}, action1_id_mem:{1}", device_id, int(last_action_id))
  84. action2_id = int(round(time.time() * 1000))
  85. """
  86. 发送第2条指令
  87. 点击去商城
  88. """
  89. with open("pic/wall.png", "rb") as image_file:
  90. encoded_string = base64.b64encode(image_file.read())
  91. action2_dict = check_pic_exist(action2_id, target_app="com.ss.android.article.lite",
  92. target_version="9.7.0.0",
  93. package_name="com.ss.android.article.lite", pic_base64=encoded_string,
  94. swipe_count=2)
  95. redis_client.set(device_id + "operate", action2_id)
  96. redis_client.set(f"{device_id}_{action2_id}_step2", "1")
  97. redis_client.delete(f"{device_id}_{action2_id}_step1")
  98. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  99. return action2_dict
  100. step2 = redis_client.get(f"{device_id}_{perform_action_id}_step2")
  101. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  102. last_action_id) and perform_action_result == "success":
  103. loggerKit.info("设备:{0}, action2_id_mem:{1}", device_id, int(last_action_id))
  104. action3_id = int(round(time.time() * 1000))
  105. """
  106. 发送第3条指令
  107. 持续滑动屏幕
  108. """
  109. action3_dict = continual_swipe_screen(action3_id, target_app="com.ss.android.article.lite",
  110. target_version="9.7.0.0",
  111. package_name="com.ss.android.article.lite", continuous_time=36135)
  112. redis_client.set(device_id + "operate", action3_id)
  113. redis_client.set(f"{device_id}_{action3_id}_step3", "1")
  114. redis_client.delete(f"{device_id}_{action3_id}_step2")
  115. loggerKit.info("设备:{0}, action3_id:{1}", device_id, action3_id)
  116. return action3_dict
  117. """
  118. 停止app
  119. """
  120. step4 = redis_client.get(f"{device_id}_step4")
  121. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  122. last_action_id) and perform_action_result == "success":
  123. loggerKit.info("设备:{0}, action10_id_mem:{1}", device_id, int(last_action_id))
  124. action5_id = int(round(time.time() * 1000))
  125. """
  126. 停止指令
  127. 停止app
  128. """
  129. action5_dict = stop_app(action5_id, target_app="com.ss.android.article.lite",
  130. target_version="9.7.0.0",
  131. package_name="com.ss.android.article.lite")
  132. redis_client.delete(f"{device_id}_step4")
  133. del_key_vague(device_id)
  134. loggerKit.info("设备:{0}, action12_id:{1}", device_id, action5_dict)
  135. # 回调任务中心修改任务状态
  136. callback_task(None, None, task_id, device_id, 1, None)
  137. return action5_dict
  138. else:
  139. action0_id = int(round(time.time() * 1000))
  140. """
  141. 启动指令
  142. 启动app
  143. """
  144. action0_dict = start_app(action0_id, target_app="com.ss.android.article.lite",
  145. target_version="9.7.0.0",
  146. package_name="com.ss.android.article.lite")
  147. redis_client.set(device_id + "operate", action0_id)
  148. redis_client.set(f"{device_id}_step0", "1")
  149. redis_client.delete(f"{device_id}_step9")
  150. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  151. return action0_dict