yiche_simple_action.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 call_back_task_reply,content_detail
  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_library_url']
  21. get_commented_list_url = extern_domain + config['bmp-cp']['get_commented_list_url']
  22. def yiche_simple_action(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. loggerKit.info("inner_data:{0}, keyword:{1}", inner_data, inner_data.get("resourceName"))
  30. if result is not None:
  31. """
  32. 非首个指令
  33. """
  34. perform_action_result = result.get("performActionResult")
  35. if perform_action_result is None:
  36. return_dict = {
  37. "data": "",
  38. "code": -2,
  39. "message": "fail, performActionResult is null"
  40. }
  41. # 回调任务中心
  42. del_key_vague(device_id)
  43. call_back_task_reply(500, '指令执行失败', task_id, device_id, 0, None, None)
  44. return return_dict
  45. # 指令执行失败
  46. if perform_action_result == "failure":
  47. # 回调任务中心
  48. return_dict = {
  49. "data": "",
  50. "code": -2,
  51. "message": "fail, performActionResult is null"
  52. }
  53. del_key_vague(device_id)
  54. call_back_task_reply(500, '指令执行失败', task_id, device_id, 0, None, None)
  55. return return_dict
  56. # 终止指令
  57. if perform_action_result == "stop":
  58. # 回调任务中心
  59. return_dict = {
  60. "data": "",
  61. "code": -2,
  62. "message": "指令被用户终止"
  63. }
  64. del_key_vague(device_id)
  65. call_back_task_reply(500, '指令被用户终止', task_id, device_id, 0, None, None)
  66. return return_dict
  67. # 终止指令
  68. if perform_action_result == "eleNotFound":
  69. # 回调任务中心
  70. return_dict = {
  71. "data": "",
  72. "code": -2,
  73. "message": "未找到签到指示"
  74. }
  75. del_key_vague(device_id)
  76. call_back_task_reply(500, '未找到签到指示,该账号当天可能已经签到过', task_id, device_id, 0, None, None)
  77. return return_dict
  78. # 元素未找到
  79. if perform_action_result == "invalid operation":
  80. # 回调任务中心
  81. return_dict = {
  82. "data": "",
  83. "code": -2,
  84. "message": "fail, performActionResult is not success"
  85. }
  86. del_key_vague(device_id)
  87. call_back_task_reply(500, '任务执行失败,元素未找到', task_id, device_id, 0, None, None)
  88. return return_dict
  89. """
  90. 每次操作完成后会将对应的操作唯一id存储到redis
  91. 并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  92. """
  93. last_action_id = redis_client.get(device_id + "operate")
  94. step0 = redis_client.get(f"{device_id}_step0")
  95. if (step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  96. last_action_id)
  97. and perform_action_result == "success"):
  98. action1_id = int(round(time.time() * 1000))
  99. """
  100. 发送第1条指令
  101. 点击
  102. """
  103. action1_dict = single_click_by_control(action1_id, target_app="yiche",
  104. target_version="11.11.0_c32",
  105. package_name="com.yiche.autoeasy",
  106. control_id="com.yiche.autoeasy:id/hbe")
  107. redis_client.set(device_id + "operate", action1_id)
  108. redis_client.set(f"{device_id}_step1", "1")
  109. redis_client.delete(f"{device_id}_step0")
  110. loggerKit.info("taskId:{0}, action1_id:{1}", task_id, action1_id)
  111. return action1_dict
  112. step1 = redis_client.get(f"{device_id}_step1")
  113. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  114. last_action_id) and perform_action_result == "success":
  115. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  116. action2_id = int(round(time.time() * 1000))
  117. """
  118. 发送第2条指令
  119. 点击
  120. """
  121. action2_dict = single_click_by_control(action2_id, target_app="yiche",
  122. target_version="11.11.0_c32",
  123. package_name="com.yiche.autoeasy",
  124. control_id="com.yiche.autoeasy:id/ixb",
  125. timeout=5, sleep_time=5)
  126. redis_client.set(device_id + "operate", action2_id)
  127. redis_client.set(f"{device_id}_step2", "1")
  128. redis_client.delete(f"{device_id}_step1")
  129. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  130. return action2_dict
  131. step2 = redis_client.get(f"{device_id}_step2")
  132. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  133. last_action_id):
  134. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  135. action3_id = int(round(time.time() * 1000))
  136. """
  137. 发送第3条指令
  138. 输入关键词
  139. """
  140. action3_dict = send_text_by_control(action3_id,
  141. target_app="yiche",
  142. target_version="11.11.0_c32",
  143. package_name="com.yiche.autoeasy",
  144. control_id="com.yiche.autoeasy:id/ixb",
  145. content=title)
  146. redis_client.set(device_id + "operate", action3_id)
  147. redis_client.set(f"{device_id}_step3", "1")
  148. redis_client.delete(f"{device_id}_step2")
  149. loggerKit.info("taskId:{0}, action3_id:{1}", task_id, action3_id)
  150. return action3_dict
  151. step3 = redis_client.get(f"{device_id}_step3")
  152. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  153. last_action_id):
  154. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  155. action4_id = int(round(time.time() * 1000))
  156. """
  157. 发送第4条指令
  158. 通过文本点击
  159. """
  160. action4_dict = single_click_by_control(action4_id,
  161. control_id="com.yiche.autoeasy:id/ixc",
  162. target_app="yiche",
  163. target_version="11.11.0_c32",
  164. package_name="com.yiche.autoeasy")
  165. redis_client.set(device_id + "operate", action4_id)
  166. redis_client.set(f"{device_id}_step4", "1")
  167. redis_client.delete(f"{device_id}_step3")
  168. loggerKit.info("taskId:{0}, action4_id:{1}", device_id, action4_id)
  169. return action4_dict
  170. step4 = redis_client.get(f"{device_id}_step4")
  171. if step4 is not None and int(step4) == 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. action5_id = int(round(time.time() * 1000))
  175. """
  176. 发送第5条指令
  177. 通过文本点击
  178. """
  179. action5_dict = single_click_by_text(action5_id,
  180. text="文章",
  181. target_app="yiche",
  182. target_version="11.11.0_c32",
  183. package_name="com.yiche.autoeasy")
  184. redis_client.set(device_id + "operate", action5_id)
  185. redis_client.set(f"{device_id}_step5", "1")
  186. redis_client.delete(f"{device_id}_step4")
  187. return action5_dict
  188. step5 = redis_client.get(f"{device_id}_step5")
  189. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  190. last_action_id):
  191. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  192. action6_id = int(round(time.time() * 1000))
  193. """
  194. 发送第6条指令
  195. 获取内容
  196. """
  197. action6_dict = get_content_by_control(action6_id, title=f"{title}",
  198. target_app="yiche",
  199. target_version="11.11.0_c32",
  200. package_name="com.yiche.autoeasy")
  201. redis_client.set(device_id + "operate", action6_id)
  202. redis_client.set(f"{device_id}_step6", "1")
  203. redis_client.delete(f"{device_id}_step5")
  204. return action6_dict
  205. step6 = redis_client.get(f"{device_id}_step6")
  206. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  207. last_action_id):
  208. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  209. action7_id = int(round(time.time() * 1000))
  210. """
  211. 点击
  212. """
  213. action7_dict = single_click_by_control(action7_id,
  214. target_app="yiche",
  215. target_version="11.11.0_c32",
  216. package_name="com.yiche.autoeasy",
  217. control_id="com.yiche.autoeasy:id/d44",
  218. control_ids="com.yiche.autoeasy:id/d44")
  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. return action7_dict
  223. step7 = redis_client.get(f"{device_id}_step7")
  224. if step7 is not None and int(step7) == 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. action8_id = int(round(time.time() * 1000))
  228. """
  229. 点击
  230. """
  231. action8_dict = single_click_by_control(action8_id,
  232. target_app="yiche",
  233. target_version="11.11.0_c32",
  234. package_name="com.yiche.autoeasy",
  235. control_id="com.yiche.autoeasy:id/d3o",
  236. control_ids="com.yiche.autoeasy:id/d3o")
  237. redis_client.set(device_id + "operate", action8_id)
  238. redis_client.set(f"{device_id}_step8", "1")
  239. redis_client.delete(f"{device_id}_step7")
  240. return action8_dict
  241. """
  242. 点击评论
  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}, last_action_id:{1}", device_id, int(last_action_id))
  249. action9_id = int(round(time.time() * 1000))
  250. """
  251. 发送第9条指令
  252. 根据标题 生成评论文案将文案缓存 并且点击评论
  253. """
  254. comment_response = httpx.get(get_commented_list_url + '?resourceName=' + title, timeout=120)
  255. # 评论实体
  256. comment_body = json.loads(comment_response.text)
  257. # 评论集合
  258. comment_list = comment_body.get('data')
  259. loggerKit.info("任务id:{0}对应标题:{1},已有评论:{2}", task_id, title, comment_list)
  260. existing_comment = ''
  261. if comment_list is not None:
  262. comment_list = list(set(comment_list))
  263. existing_comment = '||'.join(comment_list)
  264. loggerKit.info("任务id:{0}对应标题:{1},拼接后的评论集合:{2}", task_id, title, existing_comment)
  265. existing_comment = quote(existing_comment)
  266. request_data = {
  267. "mode": "1",
  268. "content": title,
  269. "existingComment": existing_comment
  270. }
  271. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC信息:{2}", task_id, title, request_data)
  272. response = httpx.post(post_ai_gc_url, json=request_data, timeout=120)
  273. # 点击评论框
  274. action9_dict = single_click_by_control(action9_id, target_app="yiche",
  275. target_version="11.11.0_c32",
  276. package_name="com.yiche.autoeasy",
  277. control_id="com.yiche.autoeasy:id/d3p",
  278. control_ids="com.yiche.autoeasy:id/d3p",
  279. timeout=5)
  280. if not response.is_success:
  281. # 调用AIGC获取评论失败
  282. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, title,
  283. request_data, response)
  284. redis_client.set(device_id + "yiche" + "comments", '不错👍')
  285. redis_client.set(device_id + "operate", action9_id)
  286. redis_client.set(f"{device_id}_step9", "1")
  287. redis_client.delete(f"{device_id}_step8")
  288. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
  289. return action9_dict
  290. response_body = json.loads(response.text)
  291. reply = response_body.get('comment')
  292. if '内容太少' in reply or '对不起' in reply or reply is None or reply == '' or '提供' in reply:
  293. # 获取到的评论内容不符合
  294. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, title,
  295. request_data, response)
  296. redis_client.set(device_id + "yiche" + "comments", '不错👍')
  297. redis_client.set(device_id + "operate", action9_id)
  298. redis_client.set(f"{device_id}_step9", "1")
  299. redis_client.delete(f"{device_id}_step8")
  300. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
  301. return action9_dict
  302. content_result = content_detail(title, title, reply, None)
  303. redis_client.set(device_id + "reply_json", json.dumps(content_result.to_dict(), ensure_ascii=False))
  304. # 将需要评论的内容存入缓存
  305. redis_client.set(device_id + "yiche" + "comments", reply)
  306. redis_client.set(device_id + "operate", action9_id)
  307. redis_client.set(f"{device_id}_step9", "1")
  308. redis_client.delete(f"{device_id}_step8")
  309. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
  310. return action9_dict
  311. """
  312. 输入评论
  313. """
  314. step9 = redis_client.get(f"{device_id}_step9")
  315. if step9 is not None and int(step9) == 1 and last_action_id is not None and int(perform_action_id) == int(
  316. last_action_id) \
  317. and perform_action_result == "success":
  318. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  319. action10_id = int(round(time.time() * 1000))
  320. """
  321. 发送第10条指令
  322. 输入评论
  323. """
  324. reply = redis_client.get(device_id + "yiche" + "comments")
  325. if reply is None:
  326. # 调用AIGC获取评论失败
  327. del_key_vague(device_id)
  328. return_dict = {
  329. "data": "",
  330. "code": -2,
  331. "message": "fail, result_text is null"
  332. }
  333. call_back_task_reply(500, '获取到的内容标题为null,无法评论',
  334. task_id, device_id, 0, None, None)
  335. return return_dict
  336. action10_dict = send_text_by_control(action10_id, target_app="yiche",
  337. target_version="11.11.0_c32",
  338. package_name="com.yiche.autoeasy",
  339. control_id="com.yiche.autoeasy:id/efd",
  340. content=reply,
  341. timeout=5)
  342. redis_client.set(device_id + "operate", action10_id)
  343. redis_client.set(f"{device_id}_step10", "1")
  344. redis_client.delete(f"{device_id}_step9")
  345. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
  346. return action10_dict
  347. """
  348. 发送评论
  349. """
  350. step10 = redis_client.get(f"{device_id}_step10")
  351. if step10 is not None and int(step10) == 1 and last_action_id is not None and int(perform_action_id) == int(
  352. last_action_id) \
  353. and perform_action_result == "success":
  354. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  355. action11_id = int(round(time.time() * 1000))
  356. """
  357. 发送第11条指令
  358. 发送评论
  359. """
  360. action11_dict = single_click_by_control(action11_id, target_app="yiche",
  361. target_version="11.11.0_c32",
  362. package_name="com.yiche.autoeasy",
  363. control_id="com.yiche.autoeasy:id/j09",
  364. timeout=5)
  365. redis_client.set(device_id + "operate", action11_id)
  366. redis_client.set(f"{device_id}_step11", "1")
  367. redis_client.delete(f"{device_id}_step10")
  368. loggerKit.info("设备:{0}, action11_id:{1}", device_id, action11_id)
  369. return action11_dict
  370. step11 = redis_client.get(f"{device_id}_step11")
  371. if step11 is not None and int(step11) == 1 and last_action_id is not None and int(perform_action_id) == int(
  372. last_action_id):
  373. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  374. action12_id = int(round(time.time() * 1000))
  375. """
  376. 发送第12条指令
  377. 关闭app
  378. """
  379. action12_dict = stop_app(action12_id, target_app="yiche",
  380. target_version="11.11.0_c32",
  381. package_name="com.yiche.autoeasy")
  382. reply_json = redis_client.get(device_id + "reply_json")
  383. del_key_vague(device_id)
  384. loggerKit.info("设备:{0}, action12_id:{1}", device_id, action12_id)
  385. # 回调任务中心修改任务状态
  386. call_back_task_reply(None, None, task_id, device_id, 1, None,reply_json)
  387. return action12_dict
  388. else:
  389. action0_id = int(round(time.time() * 1000))
  390. """
  391. 启动指令
  392. 启动app
  393. """
  394. action0_dict = start_app(action0_id, target_app="yiche",
  395. target_version="11.11.0",
  396. package_name="com.yiche.autoeasy")
  397. redis_client.set(device_id + "operate", action0_id)
  398. redis_client.set(f"{device_id}_step0", "1")
  399. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  400. return action0_dict