search_article_comment.py 16 KB

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