douyin_train_home_page.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import random
  2. import time
  3. from func.action_func import del_key_vague
  4. from task.task_job import callback_task
  5. from tools import loggerKit, redis_client
  6. from scene.oprator.atom_data import start_app, stop_app, continual_swipe_screen, single_click_by_control
  7. import yaml
  8. with open('config.yaml', 'r') as file:
  9. config = yaml.load(file, Loader=yaml.FullLoader)
  10. extern_domain = config['bmp-cp']['extern_domain']
  11. # 任务执行回调url
  12. task_callback_url = extern_domain + config['bmp-cp']['task_callback_url']
  13. # 抖音养号首页(浏览30-50视频 随机时长和点赞收藏)
  14. # version 29.5.0
  15. def douyin_spider(task_id, keyword, data):
  16. loggerKit.info('请求信息:{0}'.format(data))
  17. device_id = data.get("deviceID")
  18. perform_action_id = data.get("performActionId")
  19. result = data.get("result")
  20. if result is not None:
  21. """
  22. 非首个指令
  23. """
  24. perform_action_result = result.get("performActionResult")
  25. if perform_action_result is None:
  26. return_dict = {
  27. "data": "",
  28. "code": -2,
  29. "message": "fail, performActionResult is null"
  30. }
  31. # 回调任务中心
  32. del_key_vague(device_id)
  33. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  34. return return_dict
  35. if perform_action_result == "failure":
  36. # 回调任务中心
  37. return_dict = {
  38. "data": "",
  39. "code": -2,
  40. "message": "fail, performActionResult is not success"
  41. }
  42. del_key_vague(device_id)
  43. callback_task(500, '任务执行失败,可能当前无法再次执行看广告任务', task_id, device_id, 0, None)
  44. return return_dict
  45. if perform_action_result == "stop":
  46. # 回调任务中心
  47. return_dict = {
  48. "data": "",
  49. "code": -2,
  50. "message": "fail, performActionResult is not success"
  51. }
  52. del_key_vague(device_id)
  53. callback_task(500, '指令被用户终止', task_id, device_id, 0, None)
  54. return return_dict
  55. # 每次操作完成后会将对应的操作唯一id存储到redis,并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  56. last_action_id = redis_client.get(device_id + "operate")
  57. step0 = redis_client.get(f"{device_id}_step0")
  58. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  59. last_action_id) and perform_action_result == "success":
  60. action1_id = int(round(time.time() * 1000))
  61. """
  62. 发送第1条指令
  63. 滑动10-15个视频
  64. """
  65. swipe_count = random.randint(5, 10)
  66. action1_dict = continual_swipe_screen(action1_id,
  67. package_name="com.ss.android.ugc.aweme.lite",
  68. continuous_time_interval='3,10',
  69. continuous_count=swipe_count)
  70. redis_client.set(device_id + "operate", action1_id)
  71. redis_client.set(f"{device_id}_step1", "1")
  72. redis_client.delete(f"{device_id}_step0")
  73. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action1_id)
  74. return action1_dict
  75. step1 = redis_client.get(f"{device_id}_step1")
  76. # 进行点赞
  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}, action2_id_mem:{1}", device_id, int(last_action_id))
  80. action2_id = int(round(time.time() * 1000))
  81. """
  82. 发送第2条指令
  83. 进行点赞
  84. """
  85. action2_dict = single_click_by_control(action2_id, target_app="douyin", target_version="28.8.0",
  86. package_name="com.ss.android.ugc.aweme.lite",
  87. control_id="com.ss.android.ugc.aweme.lite:id/cs5", timeout=5)
  88. redis_client.set(device_id + "operate", action2_id)
  89. redis_client.set(f"{device_id}_step2", "1")
  90. redis_client.delete(f"{device_id}_step1")
  91. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  92. return action2_dict
  93. # 进行收藏
  94. step2 = redis_client.get(f"{device_id}_step2")
  95. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  96. last_action_id):
  97. loggerKit.info("设备:{0}, action3_id_mem:{1}", device_id, int(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="douyin", target_version="28.8.0",
  104. package_name="com.ss.android.ugc.aweme.lite",
  105. control_id="com.ss.android.ugc.aweme.lite:id/bnb", timeout=5)
  106. redis_client.set(device_id + "operate", action3_id)
  107. redis_client.set(f"{device_id}_step3", "1")
  108. redis_client.delete(f"{device_id}_step2")
  109. loggerKit.info("设备:{0}, action3_id:{1}", device_id, action3_id)
  110. return action3_dict
  111. step3 = redis_client.get(f"{device_id}_step3")
  112. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  113. last_action_id):
  114. action4_id = int(round(time.time() * 1000))
  115. """
  116. 发送第4条指令
  117. 滑动10-15个视频
  118. """
  119. swipe_count = random.randint(5, 10)
  120. action4_dict = continual_swipe_screen(action4_id,
  121. package_name="com.ss.android.ugc.aweme.lite",
  122. continuous_time_interval='3,10',
  123. continuous_count=swipe_count)
  124. redis_client.set(device_id + "operate", action4_id)
  125. redis_client.set(f"{device_id}_step4", "1")
  126. redis_client.delete(f"{device_id}_step3")
  127. loggerKit.info("设备:{0}, action4_id:{1}", device_id, action4_id)
  128. return action4_dict
  129. step4 = redis_client.get(f"{device_id}_step4")
  130. # 进行点赞
  131. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  132. last_action_id):
  133. loggerKit.info("设备:{0}, action5_id_mem:{1}", device_id, int(last_action_id))
  134. action5_id = int(round(time.time() * 1000))
  135. """
  136. 发送第5条指令
  137. 进行点赞
  138. """
  139. action5_dict = single_click_by_control(action5_id, target_app="douyin", target_version="28.8.0",
  140. package_name="com.ss.android.ugc.aweme.lite",
  141. control_id="com.ss.android.ugc.aweme.lite:id/cs5", timeout=5)
  142. redis_client.set(device_id + "operate", action5_id)
  143. redis_client.set(f"{device_id}_step5", "1")
  144. redis_client.delete(f"{device_id}_step4")
  145. loggerKit.info("设备:{0}, action5_id:{1}", device_id, action5_id)
  146. return action5_dict
  147. # 进行收藏
  148. step5 = redis_client.get(f"{device_id}_step5")
  149. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  150. last_action_id):
  151. loggerKit.info("设备:{0}, action6_id_mem:{1}", device_id, int(last_action_id))
  152. action6_id = int(round(time.time() * 1000))
  153. """
  154. 发送第6条指令
  155. 进行收藏
  156. """
  157. action6_dict = single_click_by_control(action6_id, target_app="douyin", target_version="28.8.0",
  158. package_name="com.ss.android.ugc.aweme.lite",
  159. control_id="com.ss.android.ugc.aweme.lite:id/bnb", timeout=5)
  160. redis_client.set(device_id + "operate", action6_id)
  161. redis_client.set(f"{device_id}_step6", "1")
  162. redis_client.delete(f"{device_id}_step5")
  163. loggerKit.info("设备:{0}, action6_id:{1}", device_id, action6_id)
  164. return action6_dict
  165. step6 = redis_client.get(f"{device_id}_step6")
  166. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  167. last_action_id):
  168. action7_id = int(round(time.time() * 1000))
  169. """
  170. 发送第7条指令
  171. 滑动10-15个视频
  172. """
  173. swipe_count = random.randint(5, 10)
  174. action7_dict = continual_swipe_screen(action7_id,
  175. package_name="com.ss.android.ugc.aweme.lite",
  176. continuous_time_interval='3,10',
  177. continuous_count=swipe_count)
  178. redis_client.set(device_id + "operate", action7_id)
  179. redis_client.set(f"{device_id}_step7", "1")
  180. redis_client.delete(f"{device_id}_step6")
  181. loggerKit.info("设备:{0}, action7_id:{1}", device_id, action7_id)
  182. return action7_dict
  183. step7 = redis_client.get(f"{device_id}_step7")
  184. # 进行点赞
  185. if step7 is not None and int(step7) == 1 and last_action_id is not None and int(perform_action_id) == int(
  186. last_action_id):
  187. loggerKit.info("设备:{0}, action8_id_mem:{1}", device_id, int(last_action_id))
  188. action8_id = int(round(time.time() * 1000))
  189. """
  190. 发送第8条指令
  191. 进行点赞
  192. """
  193. action8_dict = single_click_by_control(action8_id, target_app="douyin", target_version="28.8.0",
  194. package_name="com.ss.android.ugc.aweme.lite",
  195. control_id="com.ss.android.ugc.aweme.lite:id/cs5", timeout=5)
  196. redis_client.set(device_id + "operate", action8_id)
  197. redis_client.set(f"{device_id}_step8", "1")
  198. redis_client.delete(f"{device_id}_step7")
  199. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action8_id)
  200. return action8_dict
  201. # 进行收藏
  202. step8 = redis_client.get(f"{device_id}_step8")
  203. if step8 is not None and int(step8) == 1 and last_action_id is not None and int(perform_action_id) == int(
  204. last_action_id):
  205. loggerKit.info("设备:{0}, action9_id_mem:{1}", device_id, int(last_action_id))
  206. action9_id = int(round(time.time() * 1000))
  207. """
  208. 发送第9条指令
  209. 进行收藏
  210. """
  211. action9_dict = single_click_by_control(action9_id, target_app="douyin", target_version="28.8.0",
  212. package_name="com.ss.android.ugc.aweme.lite",
  213. control_id="com.ss.android.ugc.aweme.lite:id/bnb", timeout=5)
  214. redis_client.set(device_id + "operate", action9_id)
  215. redis_client.set(f"{device_id}_step9", "1")
  216. redis_client.delete(f"{device_id}_step8")
  217. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
  218. return action9_dict
  219. # 停止
  220. step9 = redis_client.get(f"{device_id}_step9")
  221. if step9 is not None and int(step9) == 1 and last_action_id is not None and int(perform_action_id) == int(
  222. last_action_id):
  223. loggerKit.info("设备:{0}, action10_id_mem:{1}", device_id, int(last_action_id))
  224. action10_id = int(round(time.time() * 1000))
  225. """
  226. 发送第10条指令
  227. 关闭
  228. """
  229. action10_dict = stop_app(action10_id, target_app="douyin", target_version="28.8.0",
  230. package_name="com.ss.android.ugc.aweme.lite", timeout=5)
  231. redis_client.set(device_id + "operate", action10_id)
  232. redis_client.set(f"{device_id}_step10", "1")
  233. redis_client.delete(f"{device_id}_step9")
  234. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
  235. del_key_vague(device_id)
  236. # 回调任务中心修改任务状态
  237. callback_task(None, None, task_id, device_id, 1, None)
  238. return action10_dict
  239. else:
  240. action0_id = int(round(time.time() * 1000))
  241. """
  242. 启动指令
  243. 启动app
  244. """
  245. action0_dict = start_app(action0_id, target_app="douyin", target_version="29.5.0",
  246. package_name="com.ss.android.ugc.aweme.lite")
  247. redis_client.set(device_id + "operate", action0_id)
  248. redis_client.set(f"{device_id}_step0", "1")
  249. redis_client.delete(f"{device_id}_step9")
  250. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  251. return action0_dict
  252. # 批量模糊删除缓存
  253. # def del_key_vague(device_id):
  254. # # 批量模糊删除keys
  255. # keys = redis_client.match_pattern_prefix(device_id)
  256. # if len(keys) > 0:
  257. # # 需要判断是否有匹配的值, 没有的话会报错
  258. # for key in keys:
  259. # redis_client.delete(key)
  260. # loggerKit.info(f"clear {device_id} keys success...")
  261. # else:
  262. # loggerKit.info(f"{device_id} keys none ...")