simple_comment.py 19 KB

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