search_article_like_comment.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. """
  2. 搜索文章、点赞、评论
  3. """
  4. import json
  5. import time
  6. from urllib.parse import quote
  7. import httpx
  8. import yaml
  9. from func.action_func import del_key_vague
  10. from scene.oprator.atom_data import swipe_screen, single_click_by_control, single_click_by_text, send_text_by_control, \
  11. get_content_by_control, start_app, stop_app
  12. from task.task_job import callback_task
  13. from tools import loggerKit, redis_client
  14. with open('config.yaml', 'r') as file:
  15. config = yaml.load(file, Loader=yaml.FullLoader)
  16. extern_domain = config['bmp-cp']['extern_domain']
  17. # 任务执行回调url
  18. task_callback_url = extern_domain + config['bmp-cp']['task_callback_url']
  19. # 调用AIGC接口
  20. post_ai_gc_url = config['bmp-cp']['post_ai_gc_url']
  21. get_commented_list_url = extern_domain + config['bmp-cp']['get_commented_list_url']
  22. def search_article_like_comment(task_id, device_id, data):
  23. loggerKit.info('请求信息:{0}'.format(data))
  24. device_id = data.get("deviceID")
  25. perform_action_id = data.get("performActionId")
  26. result = data.get("result")
  27. inner_data = data.get("data")
  28. title = inner_data.get("resourceName")
  29. author = inner_data.get("subResourceName")
  30. loggerKit.info("inner_data:{0}, keyword:{1}", inner_data, inner_data.get("resourceName"))
  31. if result is not None:
  32. """
  33. 非首个指令
  34. """
  35. perform_action_result = result.get("performActionResult")
  36. if perform_action_result is None:
  37. return_dict = {
  38. "data": "",
  39. "code": -2,
  40. "message": "fail, performActionResult is null"
  41. }
  42. # 回调任务中心
  43. del_key_vague(device_id)
  44. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  45. return return_dict
  46. # 指令执行失败
  47. if perform_action_result == "failure":
  48. # 回调任务中心
  49. return_dict = {
  50. "data": "",
  51. "code": -2,
  52. "message": "fail, performActionResult is null"
  53. }
  54. del_key_vague(device_id)
  55. callback_task(500, '指令执行失败', task_id, device_id, 0, None)
  56. return return_dict
  57. # 终止指令
  58. if perform_action_result == "stop":
  59. # 回调任务中心
  60. return_dict = {
  61. "data": "",
  62. "code": -2,
  63. "message": "指令被用户终止"
  64. }
  65. del_key_vague(device_id)
  66. callback_task(500, '指令被用户终止', task_id, device_id, 0, None)
  67. return return_dict
  68. # 终止指令
  69. if perform_action_result == "eleNotFound":
  70. # 回调任务中心
  71. return_dict = {
  72. "data": "",
  73. "code": -2,
  74. "message": "未找到签到指示"
  75. }
  76. del_key_vague(device_id)
  77. callback_task(500, '未找到签到指示,该账号当天可能已经签到过', task_id, device_id, 0, None)
  78. return return_dict
  79. last_action_id = redis_client.get(device_id + "operate")
  80. step0 = redis_client.get(f"{device_id}_step0")
  81. if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  82. last_action_id) and perform_action_result == "success":
  83. action1_id = int(round(time.time() * 1000))
  84. """
  85. 发送第1条指令
  86. 懂车帝APP首页点击搜索框
  87. """
  88. action1_dict = single_click_by_control(action1_id, target_version="7.9.0",
  89. control_id="com.ss.android.auto:id/c9c")
  90. redis_client.set(device_id + "operate", action1_id)
  91. redis_client.set(f"{device_id}_step1", "1")
  92. redis_client.delete(f"{device_id}_step0")
  93. loggerKit.info("taskId:{0}, action1_id:{1}", task_id, action1_id)
  94. return action1_dict
  95. step1 = redis_client.get(f"{device_id}_step1")
  96. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  97. last_action_id):
  98. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  99. action2_id = int(round(time.time() * 1000))
  100. """
  101. 发送第2条指令
  102. 在搜索框输入关键词
  103. """
  104. action2_dict = send_text_by_control(action2_id, control_id="com.ss.android.auto:id/h5q",
  105. content=f'{author}')
  106. redis_client.set(device_id + "operate", action2_id)
  107. redis_client.set(f"{device_id}_step2", "1")
  108. redis_client.delete(f"{device_id}_step1")
  109. loggerKit.info("taskId:{0}, action2_id:{1}", task_id, action2_id)
  110. return action2_dict
  111. step2 = redis_client.get(f"{device_id}_step2")
  112. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  113. last_action_id):
  114. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  115. action3_id = int(round(time.time() * 1000))
  116. """
  117. 发送第3条指令
  118. 点击搜索
  119. """
  120. action3_dict = single_click_by_control(action3_id, control_id="com.ss.android.auto:id/g9e")
  121. redis_client.set(device_id + "operate", action3_id)
  122. redis_client.set(f"{device_id}_step3", "1")
  123. redis_client.delete(f"{device_id}_step2")
  124. loggerKit.info("taskId:{0}, action3_id:{1}", task_id, action3_id)
  125. return action3_dict
  126. step3 = redis_client.get(f"{device_id}_step3")
  127. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  128. last_action_id):
  129. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  130. action4_id = int(round(time.time() * 1000))
  131. """
  132. 发送第4条指令
  133. 点击tab->口碑
  134. """
  135. action4_dict = single_click_by_text(action4_id, text="二手车")
  136. redis_client.set(device_id + "operate", action4_id)
  137. redis_client.set(f"{device_id}_step4", "1")
  138. redis_client.delete(f"{device_id}_step3")
  139. loggerKit.info("taskId:{0}, action4_id:{1}", device_id, action4_id)
  140. return action4_dict
  141. step4 = redis_client.get(f"{device_id}_step4")
  142. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  143. last_action_id):
  144. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  145. action5_id = int(round(time.time() * 1000))
  146. """
  147. 发送第5条指令
  148. 点击tab->车友圈
  149. """
  150. action5_dict = single_click_by_text(action5_id, text="车友圈")
  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. return action5_dict
  155. step5 = redis_client.get(f"{device_id}_step5")
  156. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  157. last_action_id):
  158. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  159. action6_id = int(round(time.time() * 1000))
  160. """
  161. 发送第6条指令
  162. 点击tab->用户
  163. """
  164. action6_dict = single_click_by_text(action6_id, text="用户")
  165. redis_client.set(device_id + "operate", action6_id)
  166. redis_client.set(f"{device_id}_step6", "1")
  167. redis_client.delete(f"{device_id}_step5")
  168. return action6_dict
  169. # 判断是否有搜索结果
  170. step6 = redis_client.get(f"{device_id}_step6")
  171. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  172. last_action_id):
  173. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  174. action7_id = int(round(time.time() * 1000))
  175. """
  176. 点击搜素出来的博主
  177. """
  178. action7_dict = single_click_by_text(action7_id, text=f"{author}")
  179. redis_client.set(device_id + "operate", action7_id)
  180. redis_client.set(f"{device_id}_step7", "1")
  181. redis_client.delete(f"{device_id}_step6")
  182. return action7_dict
  183. # 存在搜索结果
  184. step7 = redis_client.get(f"{device_id}_step7")
  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}, last_action_id:{1}", device_id, int(last_action_id))
  188. action8_id = int(round(time.time() * 1000))
  189. """
  190. 存在搜索结果,已经跳转到了用户中心首页
  191. 开始匹配文章
  192. """
  193. action8_dict = get_content_by_control(action8_id, control_id="android.widget.TextView",
  194. title=f'{title}')
  195. redis_client.set(device_id + "operate", action8_id)
  196. redis_client.set(f"{device_id}_step8", "1")
  197. redis_client.delete(f"{device_id}_step7")
  198. return action8_dict
  199. # 文章评论,点击铅笔父节点
  200. step8 = redis_client.get(f"{device_id}_step8")
  201. if step8 is not None and int(step8) == 1 and last_action_id is not None and int(
  202. perform_action_id) == int(
  203. last_action_id):
  204. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  205. action9_id = int(round(time.time() * 1000))
  206. """
  207. 文章匹配,点击评论
  208. """
  209. loggerKit.info("action9_id:{0}, 文章评论,点击铅笔父节点", action9_id)
  210. action9_dict = single_click_by_control(action9_id, control_id="com.ss.android.auto:id/d98")
  211. redis_client.set(device_id + "operate", action9_id)
  212. redis_client.set(f"{device_id}_step9", "1")
  213. redis_client.delete(f"{device_id}_step8")
  214. return action9_dict
  215. """
  216. 文章匹配,点击输入框,返回回复内容
  217. """
  218. step9 = redis_client.get(f"{device_id}_step9")
  219. if step9 is not None and int(step9) == 1 and last_action_id is not None and int(
  220. perform_action_id) == int(
  221. last_action_id):
  222. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  223. action10_id = int(round(time.time() * 1000))
  224. """
  225. 文章匹配,点击输入框,返回回复内容
  226. """
  227. loggerKit.info("action10_id:{0}, 文章title:{1}, 回复内容:{2}")
  228. comment_response = httpx.get(get_commented_list_url + '?resourceName=' + title, timeout=120)
  229. # 评论实体
  230. comment_body = json.loads(comment_response.text)
  231. # 评论集合
  232. comment_list = comment_body.get('data')
  233. loggerKit.info("任务id:{0}对应标题:{1},已有评论:{2}", task_id, title, comment_list)
  234. existing_comment = ''
  235. if comment_list is not None:
  236. comment_list = list(set(comment_list))
  237. existing_comment = '||'.join(comment_list)
  238. loggerKit.info("任务id:{0}对应标题:{1},拼接后的评论集合:{2}", task_id, title, existing_comment)
  239. existing_comment = quote(existing_comment)
  240. request_data = {
  241. "templateId": "dcd_comment",
  242. "content": title,
  243. "existingComment": existing_comment
  244. }
  245. response = httpx.post(post_ai_gc_url, json=request_data, timeout=120)
  246. loggerKit.info("任务id:{0}获取AIGC评论结果:{1}", task_id, response)
  247. reply_content = "不错👍"
  248. if response.is_success:
  249. response_body = json.loads(response.text)
  250. data = response_body.get('data')
  251. reply_text = data.get('answer')
  252. reply_text_json = json.loads(reply_text)
  253. reply_content = reply_text_json.get('comment')
  254. if len(reply_content) == 0:
  255. reply_content = "赞👍"
  256. content_result = content_detail(title, author, reply_content, None)
  257. redis_client.set(device_id + "reply_json", json.dumps(content_result.to_dict(), ensure_ascii=False))
  258. action10_dict = send_text_by_control(action10_id,
  259. control_id="com.ss.android.auto:id/byd",
  260. content=f'{reply_content}')
  261. redis_client.set(device_id + "operate", action10_id)
  262. redis_client.set(f"{device_id}_step10", "1")
  263. redis_client.delete(f"{device_id}_step9")
  264. return action10_dict
  265. """
  266. 点击发送按钮
  267. """
  268. step10 = redis_client.get(f"{device_id}_step10")
  269. if step10 is not None and int(step10) == 1 and last_action_id is not None and int(
  270. perform_action_id) == int(
  271. last_action_id):
  272. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  273. action11_id = int(round(time.time() * 1000))
  274. """
  275. 文章匹配,点击输入框,返回回复内容
  276. """
  277. action11_dict = single_click_by_control(action11_id, control_id="com.ss.android.auto:id/jxq")
  278. redis_client.set(device_id + "operate", action11_id)
  279. redis_client.set(f"{device_id}_step11", "1")
  280. redis_client.delete(f"{device_id}_step10")
  281. return action11_dict
  282. """
  283. 点赞
  284. """
  285. step11 = redis_client.get(f"{device_id}_step11")
  286. if step11 is not None and int(step11) == 1 and last_action_id is not None and int(
  287. perform_action_id) == int(
  288. last_action_id):
  289. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  290. action12_id = int(round(time.time() * 1000))
  291. """
  292. 文章匹配,点击输入框,返回回复内容
  293. """
  294. action12_dict = single_click_by_control(action12_id, control_id="com.ss.android.auto:id/en3")
  295. redis_client.set(device_id + "operate", action12_id)
  296. redis_client.set(f"{device_id}_step12", "1")
  297. redis_client.delete(f"{device_id}_step11")
  298. return action12_dict
  299. step12 = redis_client.get(f"{device_id}_step12")
  300. if step12 is not None and int(step12) == 1 and last_action_id is not None and int(
  301. perform_action_id) == int(
  302. last_action_id):
  303. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  304. action13_id = int(round(time.time() * 1000))
  305. """
  306. 发送第13条指令
  307. 关闭app
  308. """
  309. action13_dict = stop_app(action13_id, target_version="7.9.0")
  310. del_key_vague(device_id)
  311. loggerKit.info("设备:{0}, action13_id:{1}", device_id, action13_id)
  312. # 回调任务中心修改任务状态
  313. callback_task(None, None, task_id, device_id, 1, None)
  314. return action13_dict
  315. else:
  316. action0_id = int(round(time.time() * 1000))
  317. """
  318. 启动指令
  319. 启动app
  320. """
  321. action0_dict = start_app(action0_id, target_version="7.9.0")
  322. redis_client.set(device_id + "operate", action0_id)
  323. redis_client.set(f"{device_id}_step0", "1")
  324. redis_client.delete(f"{device_id}_step11")
  325. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  326. return action0_dict
  327. class content_detail:
  328. def __init__(self, keyword, title, reply, account_name):
  329. """
  330. :rtype: object
  331. """
  332. # 搜索关键词
  333. self.keyword = keyword
  334. self.title = title
  335. self.reply = reply
  336. self.account_name = account_name
  337. def to_dict(self):
  338. return {
  339. 'keyword': self.keyword,
  340. 'title': self.title,
  341. 'reply': self.reply,
  342. 'accountName': self.account_name
  343. }