douyin_watch_noval.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import time
  2. from func.action_func import del_key_vague
  3. from task.task_job import callback_task
  4. from tools import loggerKit, redis_client
  5. from tools.pic_base64_util import pic_to_base64
  6. from scene.oprator.atom_data import start_app, stop_app, continual_swipe_screen, single_click_by_control, \
  7. check_pic_exist, \
  8. click_pic, get_content_by_control, swipe_screen, single_click_by_text
  9. import yaml
  10. with open('config.yaml', 'r') as file:
  11. config = yaml.load(file, Loader=yaml.FullLoader)
  12. extern_domain = config['bmp-cp']['extern_domain']
  13. # 任务执行回调url
  14. task_callback_url = extern_domain + config['bmp-cp']['task_callback_url']
  15. # 抖音极速版看小说
  16. # version 28.8.0
  17. def douyin_spider(device_serial, task_id, keyword, media_channel, data):
  18. loggerKit.info('请求信息:{0}'.format(data))
  19. device_id = data.get("deviceID")
  20. perform_action_id = data.get("performActionId")
  21. result = data.get("result")
  22. if result is not None:
  23. """
  24. 非首个指令
  25. """
  26. perform_action_result = result.get("performActionResult")
  27. if perform_action_result is None:
  28. return_dict = {
  29. "data": "",
  30. "code": -2,
  31. "message": "fail, performActionResult is null"
  32. }
  33. # 回调任务中心
  34. del_key_vague(device_id)
  35. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  36. return return_dict
  37. if perform_action_result == "eleNotFound":
  38. # 回调任务中心
  39. return_dict = {
  40. "data": "",
  41. "code": -2,
  42. "message": "fail, performActionResult is not success"
  43. }
  44. del_key_vague(device_id)
  45. callback_task(500, '任务执行失败,未达到看小说任务领取金币要求', task_id, device_id, 0, None)
  46. return return_dict
  47. if perform_action_result == "failure":
  48. # 回调任务中心
  49. return_dict = {
  50. "data": "",
  51. "code": -2,
  52. "message": "fail, performActionResult is not success"
  53. }
  54. del_key_vague(device_id)
  55. callback_task(500, '任务执行失败,可能当前无法再次执行看广告任务', task_id, device_id, 0, None)
  56. return return_dict
  57. if perform_action_result == "stop":
  58. # 回调任务中心
  59. return_dict = {
  60. "data": "",
  61. "code": -2,
  62. "message": "fail, performActionResult is not success"
  63. }
  64. del_key_vague(device_id)
  65. callback_task(500, '指令被用户终止', task_id, device_id, 0, None)
  66. return return_dict
  67. # 每次操作完成后会将对应的操作唯一id存储到redis,并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  68. last_action_id = redis_client.get(device_id + "operate")
  69. step0 = redis_client.get(f"{device_id}_step0")
  70. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  71. last_action_id) and perform_action_result == "success":
  72. action1_id = int(round(time.time() * 1000))
  73. """
  74. 发送第1条指令
  75. 点击急速版抖音赚钱按键
  76. """
  77. action1_dict = single_click_by_control(action1_id, target_app="douyin", target_version="28.8.0",
  78. package_name="com.ss.android.ugc.aweme.lite",
  79. control_id="com.ss.android.ugc.aweme.lite:id/hvy",
  80. control_ids="com.ss.android.ugc.aweme.lite:id/hvy,"
  81. "com.ss.android.ugc.aweme.lite:id/lf3")
  82. redis_client.set(device_id + "operate", action1_id)
  83. redis_client.set(f"{device_id}_step1", "1")
  84. redis_client.delete(f"{device_id}_step0")
  85. loggerKit.info("设备:{0}, action1_id:{1}", device_id, action1_id)
  86. return action1_dict
  87. step1 = redis_client.get(f"{device_id}_step1")
  88. # 看小说地址
  89. noval_path = 'noval.png'
  90. # 检查是否存在看小说按钮
  91. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  92. last_action_id) and perform_action_result == "success":
  93. loggerKit.info("设备:{0}, action1_id_mem:{1}", device_id, int(last_action_id))
  94. action2_id = int(round(time.time() * 1000))
  95. """
  96. 发送第2条指令
  97. 检查是否有看小说图片
  98. """
  99. noval_base64 = pic_to_base64(noval_path)
  100. action2_dict = check_pic_exist(action2_id, target_app="douyin", target_version="28.8.0",
  101. package_name="com.ss.android.ugc.aweme.lite",
  102. pic_base64=noval_base64, swipe_count=3)
  103. redis_client.set(device_id + "operate", action2_id)
  104. redis_client.set(f"{device_id}_step2", "1")
  105. redis_client.delete(f"{device_id}_step1")
  106. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  107. return action2_dict
  108. # 点击看小说按钮
  109. step2 = redis_client.get(f"{device_id}_step2")
  110. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  111. last_action_id) \
  112. and perform_action_result == "success":
  113. loggerKit.info("设备:{0}, action2_id_mem:{1}", device_id, int(last_action_id))
  114. action3_id = int(round(time.time() * 1000))
  115. """
  116. 发送第3条指令
  117. 点击小说按钮
  118. """
  119. noval_base64 = pic_to_base64(noval_path)
  120. action3_dict = click_pic(action3_id, target_app="douyin", target_version="28.8.0",
  121. package_name="com.ss.android.ugc.aweme.lite",
  122. pic_base64=noval_base64)
  123. redis_client.set(device_id + "operate", action3_id)
  124. redis_client.set(f"{device_id}_step3", "1")
  125. redis_client.delete(f"{device_id}_step2")
  126. loggerKit.info("设备:{0}, action3_id:{1}", device_id, action3_id)
  127. return action3_dict
  128. step3 = redis_client.get(f"{device_id}_step3")
  129. # 进入排行榜
  130. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  131. last_action_id) \
  132. and perform_action_result == "success":
  133. loggerKit.info("设备:{0}, action3_id_mem:{1}", device_id, int(last_action_id))
  134. action4_id = int(round(time.time() * 1000))
  135. """
  136. 发送第4条指令
  137. 点击更多进入排行榜
  138. """
  139. action4_dict = single_click_by_text(action4_id, target_app="douyin", target_version="28.8.0",
  140. package_name="com.ss.android.ugc.aweme.lite",
  141. text="更多", timeout=10, sleep_time=5)
  142. redis_client.set(device_id + "operate", action4_id)
  143. redis_client.set(f"{device_id}_step4", "1")
  144. redis_client.delete(f"{device_id}_step3")
  145. loggerKit.info("设备:{0}, action4_id:{1}", device_id, action4_id)
  146. return action4_dict
  147. # 阅读排行第一小说
  148. step4 = redis_client.get(f"{device_id}_step4")
  149. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  150. last_action_id) \
  151. and perform_action_result == "success":
  152. loggerKit.info("设备:{0}, action4_id_mem:{1}", device_id, int(last_action_id))
  153. action5_id = int(round(time.time() * 1000))
  154. """
  155. 发送第5条指令
  156. 进入排行第一小说
  157. """
  158. action5_dict = single_click_by_text(action5_id, target_app="douyin", target_version="28.8.0",
  159. package_name="com.ss.android.ugc.aweme.lite",
  160. text="1", timeout=10, sleep_time=5)
  161. redis_client.set(device_id + "operate", action5_id)
  162. redis_client.set(f"{device_id}_step5", "1")
  163. redis_client.delete(f"{device_id}_step4")
  164. loggerKit.info("设备:{0}, action5_id:{1}", device_id, action5_id)
  165. return action5_dict
  166. # 阅读排行第一小说
  167. step4 = redis_client.get(f"{device_id}_step4")
  168. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  169. last_action_id) \
  170. and perform_action_result == "success":
  171. loggerKit.info("设备:{0}, action4_id_mem:{1}", device_id, int(last_action_id))
  172. action5_id = int(round(time.time() * 1000))
  173. """
  174. 发送第5条指令
  175. 进入排行第一小说
  176. """
  177. action5_dict = single_click_by_text(action5_id, target_app="douyin", target_version="28.8.0",
  178. package_name="com.ss.android.ugc.aweme.lite",
  179. text="1", timeout=10, sleep_time=5)
  180. redis_client.set(device_id + "operate", action5_id)
  181. redis_client.set(f"{device_id}_step5", "1")
  182. redis_client.delete(f"{device_id}_step4")
  183. loggerKit.info("设备:{0}, action5_id:{1}", device_id, action5_id)
  184. return action5_dict
  185. # 向右滑动五分钟每次间隔10s
  186. step5 = redis_client.get(f"{device_id}_step5")
  187. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  188. last_action_id) \
  189. and perform_action_result == "success":
  190. loggerKit.info("设备:{0}, action5_id_mem:{1}", device_id, int(last_action_id))
  191. action6_id = int(round(time.time() * 1000))
  192. """
  193. 发送第6条指令
  194. 向右滑动五分钟每次间隔8s
  195. """
  196. action6_dict = continual_swipe_screen(action6_id,
  197. package_name="com.ss.android.ugc.aweme.lite",
  198. sleep_time=10,
  199. continuous_time=60 * 5)
  200. redis_client.set(device_id + "operate", action6_id)
  201. redis_client.set(f"{device_id}_step6", "1")
  202. redis_client.delete(f"{device_id}_step5")
  203. loggerKit.info("设备:{0}, action6_id:{1}", device_id, action6_dict)
  204. return action6_dict
  205. # 点击右上方小金币 领取金币
  206. step6 = redis_client.get(f"{device_id}_step6")
  207. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  208. last_action_id) \
  209. and perform_action_result == "success":
  210. loggerKit.info("设备:{0}, action6_id_mem:{1}", device_id, int(last_action_id))
  211. action7_id = int(round(time.time() * 1000))
  212. """
  213. 发送第7条指令
  214. 点击小金币
  215. """
  216. action7_dict = single_click_by_control(action7_id, target_app="douyin", target_version="28.8.0",
  217. package_name="com.ss.android.ugc.aweme.lite",
  218. control_id="m.l.novel:id/novel_sdk_ug_reader_task_icon")
  219. redis_client.set(device_id + "operate", action7_id)
  220. redis_client.set(f"{device_id}_step7", "1")
  221. redis_client.delete(f"{device_id}_step6")
  222. loggerKit.info("设备:{0}, action6_id:{1}", device_id, action7_dict)
  223. return action7_dict
  224. # 点击右上方小金币 领取金币
  225. step7 = redis_client.get(f"{device_id}_step7")
  226. if step7 is not None and int(step7) == 1 and last_action_id is not None and int(perform_action_id) == int(
  227. last_action_id) \
  228. and perform_action_result == "success":
  229. loggerKit.info("设备:{0}, action7_id_mem:{1}", device_id, int(last_action_id))
  230. action8_id = int(round(time.time() * 1000))
  231. """
  232. 发送第7条指令
  233. 点击小金币
  234. """
  235. action8_dict = single_click_by_text(action8_id, target_app="douyin", target_version="28.8.0",
  236. package_name="com.ss.android.ugc.aweme.lite",
  237. text="去领取", timeout=10, sleep_time=5)
  238. redis_client.set(device_id + "operate", action8_id)
  239. redis_client.set(f"{device_id}_step8", "1")
  240. redis_client.delete(f"{device_id}_step7")
  241. loggerKit.info("设备:{0}, action8_id:{1}", device_id, action8_dict)
  242. return action8_dict
  243. # 关闭抖音
  244. step8 = redis_client.get(f"{device_id}_step8")
  245. if step8 is not None and int(step8) == 1 and last_action_id is not None and int(perform_action_id) == int(
  246. last_action_id) \
  247. and perform_action_result == "success":
  248. loggerKit.info("设备:{0}, action8_id_mem:{1}", device_id, int(last_action_id))
  249. action9_id = int(round(time.time() * 1000))
  250. """
  251. 停止指令
  252. 停止app
  253. """
  254. action9_dict = stop_app(action9_id, target_app="douyin", target_version="28.8.0",
  255. package_name="com.ss.android.ugc.aweme.lite")
  256. del_key_vague(device_id)
  257. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_dict)
  258. # 回调任务中心修改任务状态
  259. callback_task(None, None, task_id, device_id, 1, None)
  260. return action9_dict
  261. else:
  262. action0_id = int(round(time.time() * 1000))
  263. """
  264. 启动指令
  265. 启动app
  266. """
  267. action0_dict = start_app(action0_id, target_app="douyin", target_version="28.8.0",
  268. package_name="com.ss.android.ugc.aweme.lite")
  269. redis_client.set(device_id + "operate", action0_id)
  270. redis_client.set(f"{device_id}_step0", "1")
  271. redis_client.delete(f"{device_id}_step9")
  272. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  273. return action0_dict
  274. # 批量模糊删除缓存
  275. # def del_key_vague(device_id):
  276. # # 批量模糊删除keys
  277. # keys = redis_client.match_pattern_prefix(device_id)
  278. # if len(keys) > 0:
  279. # # 需要判断是否有匹配的值, 没有的话会报错
  280. # for key in keys:
  281. # redis_client.delete(key)
  282. # loggerKit.info(f"clear {device_id} keys success...")
  283. # else:
  284. # loggerKit.info(f"{device_id} keys none ...")
  285. # post请求
  286. # def call_back_request(error_code, error_msg, task_id, task_status, task_execute_response, content, demo_flag):
  287. # response = {
  288. # "errorCode": error_code,
  289. # "errorMsg": error_msg,
  290. # "taskId": task_id,
  291. # "taskStatus": task_status,
  292. # "taskExecuteResponse": task_execute_response,
  293. # "content": content,
  294. # "demoFlag": demo_flag
  295. # }
  296. # return response
  297. #
  298. #
  299. # # 回调任务中心接口
  300. # def callback_task(err_code, err_msg, task_id, device_id, execute_status, result):
  301. # callback_request2 = call_back_request(err_code, err_msg, task_id, execute_status, result, None, None)
  302. # loggerKit.info("thread[{0}=>{1}], taskId:{2}, 设备号:{3}, url:{4} 。回调开始", threading.current_thread().name,
  303. # threading.get_ident(), task_id, device_id, task_callback_url)
  304. # current_timeout = (5, 10)
  305. # callback_response2 = requests.post(task_callback_url, json=callback_request2, timeout=current_timeout)
  306. # loggerKit.info("thread[{0}=>{1}], taskId:{2},设备号:{3}。回调结束:{4}", threading.current_thread().name,
  307. # threading.get_ident(), task_id, device_id, json.dumps(callback_response2.text, ensure_ascii=False))
  308. # return callback_response2