dongchedi_interaction.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import time
  2. from func.action_func import del_key_vague
  3. from scene.oprator.atom_data import single_click_by_control, stop_app, start_app, \
  4. single_click_by_text, click_pic, send_text_by_control, get_content_by_control
  5. from task.task_job import callback_task
  6. from tools import loggerKit, redis_client
  7. from tools.pic_base64_util import pic_to_base64
  8. """
  9. 懂车帝策略互动
  10. """
  11. def dongchedi_interaction(task_id, mobile, data, sub_resource_name):
  12. loggerKit.info('请求信息:{0}'.format(data))
  13. device_id = data.get("deviceID")
  14. keyword = data.get("resourceName")
  15. perform_action_id = data.get("performActionId")
  16. result = data.get("result")
  17. if result is not None:
  18. """
  19. 非首个指令
  20. """
  21. perform_action_result = result.get("performActionResult")
  22. if perform_action_result is None:
  23. return_dict = {
  24. "data": "",
  25. "code": -2,
  26. "message": "fail, performActionResult is null"
  27. }
  28. # 回调任务中心
  29. del_key_vague(device_id)
  30. callback_task(500, '指令执行失败', task_id, mobile, 0, None)
  31. return return_dict
  32. # 指令执行失败
  33. if perform_action_result == "failure":
  34. # 回调任务中心
  35. return_dict = {
  36. "data": "",
  37. "code": -2,
  38. "message": "fail, performActionResult is null"
  39. }
  40. del_key_vague(device_id)
  41. callback_task(500, '指令执行失败', task_id, mobile, 0, None)
  42. return return_dict
  43. # 终止指令
  44. if perform_action_result == "stop":
  45. # 回调任务中心
  46. return_dict = {
  47. "data": "",
  48. "code": -2,
  49. "message": "指令被用户终止"
  50. }
  51. del_key_vague(device_id)
  52. callback_task(500, '指令被用户终止', task_id, mobile, 0, None)
  53. return return_dict
  54. """
  55. 每次操作完成后会将对应的操作唯一id存储到redis
  56. 并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  57. """
  58. last_action_id = redis_client.get(device_id + "operate")
  59. step0 = redis_client.get(f"{device_id}_step0")
  60. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  61. last_action_id) and perform_action_result == "success":
  62. action1_id = int(round(time.time() * 1000))
  63. """
  64. 发送第1条指令
  65. 点击搜索按钮
  66. """
  67. action1_dict = single_click_by_control(action1_id, target_app="dongchedi", target_version="7.9.0",
  68. package_name="com.ss.android.auto",
  69. control_id="com.ss.android.auto:id/d34", timeout=5, sleep_time=5,
  70. item_index=4)
  71. redis_client.set(device_id + "operate", action1_id)
  72. redis_client.set(f"{device_id}_step1", "1")
  73. redis_client.delete(f"{device_id}_step0")
  74. loggerKit.info("taskId:{0}, action1_id:{1}", task_id, action1_id)
  75. return action1_dict
  76. step1 = redis_client.get(f"{device_id}_step1")
  77. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  78. last_action_id):
  79. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  80. action2_id = int(round(time.time() * 1000))
  81. """
  82. 发送第2条指令
  83. 搜索框赋值
  84. """
  85. action2_dict = send_text_by_control(action2_id, target_app="dongchedi", target_version="7.9.0",
  86. package_name="com.ss.android.auto",
  87. control_id="com.ss.android.auto:id/h5q",
  88. content=keyword,
  89. timeout=5, sleep_time=5)
  90. redis_client.set(device_id + "operate", action2_id)
  91. redis_client.set(f"{device_id}_step2", "1")
  92. redis_client.delete(f"{device_id}_step1")
  93. loggerKit.info("taskId:{0}, action2_id:{1}", task_id, action2_id)
  94. return action2_dict
  95. step2 = redis_client.get(f"{device_id}_step2")
  96. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  97. last_action_id):
  98. action3_id = int(round(time.time() * 1000))
  99. """
  100. 发送第3条指令
  101. 点击搜索
  102. """
  103. action3_dict = single_click_by_control(action3_id, target_app="dongchedi", target_version="7.9.0",
  104. package_name="com.ss.android.auto",
  105. control_id="com.ss.android.auto:id/g9e", timeout=5, sleep_time=5,
  106. item_index=4)
  107. redis_client.set(device_id + "operate", action3_id)
  108. redis_client.set(f"{device_id}_step3", "1")
  109. redis_client.delete(f"{device_id}_step2")
  110. loggerKit.info("taskId:{0}, action3_id:{1}", task_id, action3_id)
  111. return action3_dict
  112. step3 = redis_client.get(f"{device_id}_step3")
  113. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  114. last_action_id):
  115. action4_id = int(round(time.time() * 1000))
  116. """
  117. 发送第4条指令
  118. 根据标题名称进行点击
  119. """
  120. action3_dict = single_click_by_text(action4_id, target_app="dongchedi", target_version="7.9.0",
  121. package_name="com.ss.android.auto",
  122. text=keyword, timeout=5, sleep_time=5, similarity=0.8)
  123. redis_client.set(device_id + "operate", action4_id)
  124. redis_client.set(f"{device_id}_step4", "1")
  125. redis_client.delete(f"{device_id}_step3")
  126. loggerKit.info("taskId:{0}, action4_id:{1}", device_id, action4_id)
  127. return action3_dict
  128. step4 = redis_client.get(f"{device_id}_step4")
  129. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  130. last_action_id):
  131. action5_id = int(round(time.time() * 1000))
  132. """
  133. 发送第5条指令
  134. 判断有没有点击到标题
  135. 如果点击到标题中 进行评论点赞收藏操作
  136. 如果没有点击到标题中 进行搜索用户操作
  137. """
  138. if perform_action_result == "invalid operation":
  139. action5_dict = send_text_by_control(action5_id, target_app="dongchedi", target_version="7.9.0",
  140. package_name="com.ss.android.auto",
  141. control_id="com.ss.android.auto:id/h5q",
  142. content=sub_resource_name,
  143. timeout=5, sleep_time=5)
  144. loggerKit.info("taskId:{0}, action25_id:{1},未根据帖子名找到对应的帖子,进行用户名搜索", device_id, action5_id)
  145. redis_client.set(device_id + "operate", action5_id)
  146. redis_client.set(f"{device_id}_step25", "1")
  147. redis_client.delete(f"{device_id}_step4")
  148. return action5_dict
  149. # 已经点击进入帖子 判断是不是视频。如果是视频不进行操作
  150. action4_dict = single_click_by_text(action5_id, target_app="dongchedi", target_version="7.9.0",
  151. package_name="com.ss.android.auto",
  152. text='视频', timeout=5, sleep_time=5)
  153. redis_client.set(device_id + "operate", action5_id)
  154. redis_client.set(f"{device_id}_step5", "1")
  155. redis_client.delete(f"{device_id}_step4")
  156. loggerKit.info("taskId:{0}, action5_id:{1},已进入帖子 判断是否未视频", device_id, action5_id)
  157. return action4_dict
  158. step5 = redis_client.get(f"{device_id}_step5")
  159. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  160. last_action_id):
  161. action6_id = int(round(time.time() * 1000))
  162. """
  163. 发送第6条指令
  164. 如果有视频对应文案,则返回失败 不支持视频操作
  165. 如果没有获取到对应的帖子内容
  166. """
  167. if perform_action_result == 'success':
  168. # 回调任务中心
  169. return_dict = {
  170. "data": "",
  171. "code": -2,
  172. "message": "fail, performActionResult is not success"
  173. }
  174. del_key_vague(device_id)
  175. callback_task(500, '该帖子为视频帖子,暂不支持', task_id, device_id, 0, None)
  176. return return_dict
  177. action3_dict = get_content_by_control(action6_id, target_app="dongchedi", target_version="7.9.0",
  178. package_name="com.ss.android.auto",
  179. control_id="", timeout=5, sleep_time=5)
  180. redis_client.set(device_id + "operate", action6_id)
  181. redis_client.set(f"{device_id}_step6", "1")
  182. redis_client.delete(f"{device_id}_step5")
  183. loggerKit.info("taskId:{0}, action4_id:{1}", device_id, action6_id)
  184. return action3_dict
  185. step3 = redis_client.get(f"{device_id}_step3")
  186. if step3 is not None and int(step3) == 1 and (last_action_id is not None
  187. and int(perform_action_id) == int(last_action_id)):
  188. loggerKit.info("设备:{0}, action4_id_mem:{1}", device_id, int(last_action_id))
  189. action4_id = int(round(time.time() * 1000))
  190. """
  191. 发送第4条指令
  192. 关闭app
  193. """
  194. action4_dict = stop_app(action4_id, target_version="7.9.0", target_app="dongchedi", timeout=5, sleep_time=3)
  195. del_key_vague(device_id)
  196. loggerKit.info("设备:{0}, action4_id:{1}", device_id, action4_dict)
  197. # 回调任务中心修改任务状态
  198. callback_task(None, None, task_id, mobile, 1, None)
  199. return action4_dict
  200. else:
  201. action0_id = int(round(time.time() * 1000))
  202. """
  203. 启动指令
  204. 启动app
  205. """
  206. action0_dict = start_app(action0_id, target_version="7.9.0")
  207. redis_client.set(device_id + "operate", action0_id)
  208. redis_client.set(f"{device_id}_step0", "1")
  209. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  210. return action0_dict