douyin_random_search.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. """
  2. 抖音极速版
  3. 随机搜索
  4. """
  5. import random
  6. import time
  7. from func.action_func import del_key_vague
  8. from scene.oprator.atom_data import swipe_screen, single_click_by_control, send_text_by_control, single_click_by_text, \
  9. continual_swipe_screen, stop_app, start_app
  10. from task.task_job import callback_task
  11. from tools import loggerKit, redis_client
  12. import json
  13. # 版本29.5.0
  14. def douyin_random_search(task_id, device_id, data):
  15. loggerKit.info('请求信息:{0}'.format(data))
  16. device_id = data.get("deviceID")
  17. perform_action_id = data.get("performActionId")
  18. result = data.get("result")
  19. inner_data = data.get("data")
  20. # keyword = inner_data.get("resourceName")
  21. extension_info = data['data']['extendInfo']
  22. extension_info = json.loads(extension_info)
  23. keyword = extension_info['searchKey']
  24. loggerKit.info("inner_data:{0}, keyword:{1}", inner_data, inner_data.get("resourceName"))
  25. if result is not None:
  26. """
  27. 非首个指令
  28. """
  29. perform_action_result = result.get("performActionResult")
  30. if perform_action_result is None:
  31. return_dict = {
  32. "data": "",
  33. "code": -2,
  34. "message": "fail, performActionResult is null"
  35. }
  36. # 回调任务中心
  37. del_key_vague(device_id)
  38. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  39. return return_dict
  40. # 指令执行失败
  41. if perform_action_result == "failure":
  42. # 回调任务中心
  43. return_dict = {
  44. "data": "",
  45. "code": -2,
  46. "message": "fail, performActionResult is null"
  47. }
  48. del_key_vague(device_id)
  49. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  50. return return_dict
  51. # 终止指令
  52. if perform_action_result == "stop":
  53. # 回调任务中心
  54. return_dict = {
  55. "data": "",
  56. "code": -2,
  57. "message": "指令被用户终止"
  58. }
  59. del_key_vague(device_id)
  60. callback_task(500, '指令被用户终止', task_id, device_id, 0, None)
  61. return return_dict
  62. """
  63. 每次操作完成后会将对应的操作唯一id存储到redis
  64. 并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  65. """
  66. last_action_id = redis_client.get(device_id + "operate")
  67. step0 = redis_client.get(f"{device_id}_step0")
  68. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  69. last_action_id) and perform_action_result == "success":
  70. action1_id = int(round(time.time() * 1000))
  71. """
  72. 发送第1条指令
  73. 首页搜索关键词
  74. """
  75. action1_dict = single_click_by_control(action1_id, target_app="douyin", target_version="28.8.0",
  76. package_name="com.ss.android.ugc.aweme.lite",
  77. control_id="com.ss.android.ugc.aweme.lite:id/fsf",
  78. control_ids="com.ss.android.ugc.aweme.lite:id/fsf")
  79. redis_client.set(device_id + "operate", action1_id)
  80. redis_client.set(f"{device_id}_step1", "1")
  81. redis_client.delete(f"{device_id}_step0")
  82. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action1_id)
  83. return action1_dict
  84. step1 = redis_client.get(f"{device_id}_step1")
  85. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  86. last_action_id) and perform_action_result == "success":
  87. loggerKit.info("设备:{0}, action1_id_mem:{1}", device_id, int(last_action_id))
  88. action2_id = int(round(time.time() * 1000))
  89. """
  90. 发送第2条指令
  91. 点击搜索文本输入框
  92. """
  93. action2_dict = single_click_by_control(action2_id, target_app="douyin", target_version="28.8.0",
  94. package_name="com.ss.android.ugc.aweme.lite",
  95. control_id="com.ss.android.ugc.aweme.lite:id/et_search_kw",
  96. control_ids="com.ss.android.ugc.aweme.lite:id/et_search_kw")
  97. redis_client.set(device_id + "operate", action2_id)
  98. redis_client.set(f"{device_id}_step2", "1")
  99. redis_client.delete(f"{device_id}_step1")
  100. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  101. return action2_dict
  102. step2 = redis_client.get(f"{device_id}_step2")
  103. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  104. last_action_id) and perform_action_result == "success":
  105. loggerKit.info("设备:{0}, action2_id_mem:{1}", device_id, int(last_action_id))
  106. action3_id = int(round(time.time() * 1000))
  107. """
  108. 发送第3条指令
  109. 输入查询关键词
  110. """
  111. action3_dict = send_text_by_control(action3_id, target_app="douyin", target_version="28.8.0",
  112. package_name="com.ss.android.ugc.aweme.lite",
  113. control_id="com.ss.android.ugc.aweme.lite:id/et_search_kw",
  114. content=keyword)
  115. redis_client.set(device_id + "operate", action3_id)
  116. redis_client.set(f"{device_id}_step3", "1")
  117. redis_client.delete(f"{device_id}_step2")
  118. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action3_id)
  119. return action3_dict
  120. step3 = redis_client.get(f"{device_id}_step3")
  121. if step3 is not None and int(step3) == 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}, action3_id_mem:{1}", device_id, int(last_action_id))
  124. action4_id = int(round(time.time() * 1000))
  125. """
  126. 发送第4条指令
  127. 点击搜索
  128. """
  129. action4_dict = single_click_by_control(action4_id, target_app="douyin", target_version="28.8.0",
  130. package_name="com.ss.android.ugc.aweme.lite",
  131. control_id="com.ss.android.ugc.aweme.lite:id/nou",
  132. control_ids="com.ss.android.ugc.aweme.lite:id/nou")
  133. redis_client.set(device_id + "operate", action4_id)
  134. redis_client.set(f"{device_id}_step4", "1")
  135. redis_client.delete(f"{device_id}_step3")
  136. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action4_id)
  137. return action4_dict
  138. step4 = redis_client.get(f"{device_id}_step4")
  139. # 切换到"视频"Tab页面
  140. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  141. last_action_id) and perform_action_result == "success":
  142. loggerKit.info("设备:{0}, action5_id_mem:{1}", device_id, int(last_action_id))
  143. action5_id = int(round(time.time() * 1000))
  144. """
  145. 发送第5条指令
  146. 切换到"视频"Tab页面
  147. """
  148. action5_dict = single_click_by_text(action5_id, target_app="douyin", target_version="28.8.0",
  149. package_name="com.ss.android.ugc.aweme.lite",
  150. text="视频", timeout=20)
  151. redis_client.set(device_id + "operate", action5_id)
  152. redis_client.set(f"{device_id}_step5", "1")
  153. redis_client.delete(f"{device_id}_step4")
  154. loggerKit.info("设备:{0}, action5_id:{1}", device_id, action5_id)
  155. return action5_dict
  156. # 点击第一个视频
  157. step5 = redis_client.get(f"{device_id}_step5")
  158. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  159. last_action_id) and perform_action_result == "success":
  160. loggerKit.info("设备:{0}, action6_id_mem:{1}", device_id, int(last_action_id))
  161. action6_id = int(round(time.time() * 1000))
  162. """
  163. 发送第6条指令
  164. 点击第一个视频
  165. """
  166. action6_dict = single_click_by_control(action6_id, target_app="douyin", target_version="28.8.0",
  167. package_name="com.ss.android.ugc.aweme.lite",
  168. control_id="com.ss.android.ugc.aweme.lite:id/ksq", timeout=20)
  169. redis_client.set(device_id + "operate", action6_id)
  170. redis_client.set(f"{device_id}_step6", "1")
  171. redis_client.delete(f"{device_id}_step5")
  172. loggerKit.info("设备:{0}, action6_id:{1}", device_id, action6_id)
  173. return action6_dict
  174. # 下滑视频播放3分钟;3分钟内动作随机(仅下滑看视频动作)
  175. step6 = redis_client.get(f"{device_id}_step6")
  176. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  177. last_action_id) and perform_action_result == "success":
  178. action7_id = int(round(time.time() * 1000))
  179. """
  180. 发送第7条指令
  181. 下滑视频播放3分钟;3分钟内动作随机(仅下滑看视频动作)
  182. """
  183. action7_dict = continual_swipe_screen(action7_id, target_app="douyin", target_version="28.8.0",
  184. package_name="com.ss.android.ugc.aweme.lite",
  185. continuous_time=3 * 60)
  186. redis_client.set(device_id + "operate", action7_id)
  187. redis_client.set(f"{device_id}_step7", "1")
  188. redis_client.delete(f"{device_id}_step6")
  189. loggerKit.info("设备:{0}, action7_id:{1}", device_id, action7_id)
  190. return action7_dict
  191. # 停止
  192. step7 = redis_client.get(f"{device_id}_step7")
  193. if step7 is not None and int(step7) == 1 and last_action_id is not None and int(perform_action_id) == int(
  194. last_action_id) and perform_action_result == "success":
  195. loggerKit.info("设备:{0}, action10_id_mem:{1}", device_id, int(last_action_id))
  196. action8_id = int(round(time.time() * 1000))
  197. """
  198. 发送第8条指令
  199. 关闭
  200. """
  201. action8_dict = stop_app(action8_id, target_app="douyin", target_version="28.8.0",
  202. package_name="com.ss.android.ugc.aweme.lite", timeout=5)
  203. redis_client.set(device_id + "operate", action8_id)
  204. redis_client.set(f"{device_id}_step8", "1")
  205. redis_client.delete(f"{device_id}_step7")
  206. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action8_id)
  207. del_key_vague(device_id)
  208. # 回调任务中心修改任务状态
  209. callback_task(None, None, task_id, device_id, 1, None)
  210. return action8_dict
  211. else:
  212. action0_id = int(round(time.time() * 1000))
  213. """
  214. 启动指令
  215. 启动app
  216. """
  217. action0_dict = start_app(action0_id, target_app="douyin", target_version="29.5.0",
  218. package_name="com.ss.android.ugc.aweme.lite")
  219. redis_client.set(device_id + "operate", action0_id)
  220. redis_client.set(f"{device_id}_step0", "1")
  221. redis_client.delete(f"{device_id}_step9")
  222. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  223. return action0_dict