yiche_direction_action.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 single_click_by_control, send_text_by_control, click_by_content, stop_app, \
  11. start_app, single_click_by_text
  12. from task.task_job import call_back_task_reply
  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 = extern_domain + config['bmp-content-center']['comment_local_url']
  21. get_commented_list_url = extern_domain + config['bmp-cp']['get_commented_list_url']
  22. def yiche_direction_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. # author = inner_data.get("subResourceName")
  30. extension_info = data['data']['extendInfo']
  31. extension_info = json.loads(extension_info)
  32. title = extension_info['title']
  33. author = extension_info['author']
  34. loggerKit.info("inner_data:{0}, keyword:{1}", inner_data, inner_data.get("resourceName"))
  35. if result is not None:
  36. """
  37. 非首个指令
  38. """
  39. perform_action_result = result.get("performActionResult")
  40. if perform_action_result is None:
  41. return_dict = {
  42. "data": "",
  43. "code": -2,
  44. "message": "fail, performActionResult is null"
  45. }
  46. # 回调任务中心
  47. del_key_vague(device_id)
  48. call_back_task_reply(500, '指令执行失败', task_id, device_id, 0, None, None)
  49. return return_dict
  50. # 指令执行失败
  51. if perform_action_result == "failure":
  52. # 回调任务中心
  53. return_dict = {
  54. "data": "",
  55. "code": -2,
  56. "message": "fail, performActionResult is null"
  57. }
  58. del_key_vague(device_id)
  59. call_back_task_reply(500, '指令执行失败', task_id, device_id, 0, None, None)
  60. return return_dict
  61. # 终止指令
  62. if perform_action_result == "stop":
  63. # 回调任务中心
  64. return_dict = {
  65. "data": "",
  66. "code": -2,
  67. "message": "指令被用户终止"
  68. }
  69. del_key_vague(device_id)
  70. call_back_task_reply(500, '指令被用户终止', task_id, device_id, 0, None, None)
  71. return return_dict
  72. # 终止指令
  73. if perform_action_result == "eleNotFound":
  74. # 回调任务中心
  75. return_dict = {
  76. "data": "",
  77. "code": -2,
  78. "message": "未找到签到指示"
  79. }
  80. del_key_vague(device_id)
  81. call_back_task_reply(500, '未找到签到指示,该账号当天可能已经签到过', task_id, device_id, 0, None, None)
  82. return return_dict
  83. # 元素未找到
  84. if perform_action_result == "invalid operation":
  85. # 回调任务中心
  86. return_dict = {
  87. "data": "",
  88. "code": -2,
  89. "message": "fail, performActionResult is not success"
  90. }
  91. del_key_vague(device_id)
  92. call_back_task_reply(500, '任务执行失败,元素未找到', task_id, device_id, 0, None, None)
  93. return return_dict
  94. """
  95. 每次操作完成后会将对应的操作唯一id存储到redis
  96. 并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
  97. """
  98. last_action_id = redis_client.get(device_id + "operate")
  99. step0 = redis_client.get(f"{device_id}_step0")
  100. if (step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
  101. last_action_id)
  102. and perform_action_result == "success"):
  103. action1_id = int(round(time.time() * 1000))
  104. """
  105. 发送第1条指令
  106. 点击 博主搜索输入框
  107. """
  108. action1_dict = single_click_by_control(action1_id, target_app="yiche",
  109. target_version="11.11.0_c32",
  110. package_name="com.yiche.autoeasy",
  111. control_id="com.yiche.autoeasy:id/hbe")
  112. redis_client.set(device_id + "operate", action1_id)
  113. redis_client.set(f"{device_id}_step1", "1")
  114. redis_client.delete(f"{device_id}_step0")
  115. loggerKit.info("taskId:{0}, action1_id:{1}", task_id, action1_id)
  116. return action1_dict
  117. step1 = redis_client.get(f"{device_id}_step1")
  118. if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
  119. last_action_id) and perform_action_result == "success":
  120. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  121. action2_id = int(round(time.time() * 1000))
  122. """
  123. 发送第2条指令
  124. 输入待搜索的博主昵称
  125. """
  126. action2_dict = send_text_by_control(action2_id, target_app="yiche",
  127. target_version="11.11.0_c32",
  128. package_name="com.yiche.autoeasy",
  129. content=author,
  130. control_id="com.yiche.autoeasy:id/ixb")
  131. redis_client.set(device_id + "operate", action2_id)
  132. redis_client.set(f"{device_id}_step2", "1")
  133. redis_client.delete(f"{device_id}_step1")
  134. loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
  135. return action2_dict
  136. step2 = redis_client.get(f"{device_id}_step2")
  137. if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
  138. last_action_id):
  139. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  140. action3_id = int(round(time.time() * 1000))
  141. """
  142. 发送第3条指令
  143. 点击搜索按钮
  144. """
  145. action3_dict = single_click_by_control(action3_id,
  146. target_app="yiche",
  147. target_version="11.11.0_c32",
  148. package_name="com.yiche.autoeasy",
  149. control_id="com.yiche.autoeasy:id/ixc",
  150. control_ids="com.yiche.autoeasy:id/ixc")
  151. redis_client.set(device_id + "operate", action3_id)
  152. redis_client.set(f"{device_id}_step3", "1")
  153. redis_client.delete(f"{device_id}_step2")
  154. loggerKit.info("taskId:{0}, action3_id:{1}", task_id, action3_id)
  155. return action3_dict
  156. step3 = redis_client.get(f"{device_id}_step3")
  157. if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
  158. last_action_id):
  159. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  160. action4_id = int(round(time.time() * 1000))
  161. """
  162. 发送第4条指令
  163. 点击tab->文章
  164. """
  165. action4_dict = single_click_by_text(action4_id,
  166. target_app="yiche",
  167. target_version="11.11.0_c32",
  168. package_name="com.yiche.autoeasy",
  169. text="文章")
  170. redis_client.set(device_id + "operate", action4_id)
  171. redis_client.set(f"{device_id}_step4", "1")
  172. redis_client.delete(f"{device_id}_step3")
  173. loggerKit.info("taskId:{0}, action4_id:{1}", device_id, action4_id)
  174. return action4_dict
  175. step4 = redis_client.get(f"{device_id}_step4")
  176. if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
  177. last_action_id):
  178. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  179. action5_id = int(round(time.time() * 1000))
  180. """
  181. 发送第5条指令
  182. 点击tab->小视频
  183. """
  184. action5_dict = single_click_by_text(action5_id,
  185. target_app="yiche",
  186. target_version="11.11.0_c32",
  187. package_name="com.yiche.autoeasy",
  188. text="小视频")
  189. redis_client.set(device_id + "operate", action5_id)
  190. redis_client.set(f"{device_id}_step5", "1")
  191. redis_client.delete(f"{device_id}_step4")
  192. return action5_dict
  193. step5 = redis_client.get(f"{device_id}_step5")
  194. if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
  195. last_action_id):
  196. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  197. action6_id = int(round(time.time() * 1000))
  198. """
  199. 发送第6条指令
  200. 点击tab->摩托车
  201. """
  202. action6_dict = single_click_by_text(action6_id, target_app="yiche",
  203. target_version="11.11.0_c32",
  204. package_name="com.yiche.autoeasy",
  205. text="摩托车")
  206. redis_client.set(device_id + "operate", action6_id)
  207. redis_client.set(f"{device_id}_step6", "1")
  208. redis_client.delete(f"{device_id}_step5")
  209. return action6_dict
  210. step6 = redis_client.get(f"{device_id}_step6")
  211. if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
  212. last_action_id):
  213. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  214. action7_id = int(round(time.time() * 1000))
  215. """
  216. 点击tab->用户
  217. """
  218. action7_dict = single_click_by_text(action7_id,
  219. target_app="yiche",
  220. target_version="11.11.0_c32",
  221. package_name="com.yiche.autoeasy",
  222. text="用户")
  223. redis_client.set(device_id + "operate", action7_id)
  224. redis_client.set(f"{device_id}_step7", "1")
  225. redis_client.delete(f"{device_id}_step6")
  226. return action7_dict
  227. step7 = redis_client.get(f"{device_id}_step7")
  228. if step7 is not None and int(step7) == 1 and last_action_id is not None and int(perform_action_id) == int(
  229. last_action_id):
  230. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  231. action8_id = int(round(time.time() * 1000))
  232. """
  233. 点击搜索到的博主
  234. """
  235. action8_dict = single_click_by_control(action8_id,
  236. target_app="yiche",
  237. target_version="11.11.0_c32",
  238. package_name="com.yiche.autoeasy",
  239. control_id="com.yiche.autoeasy:id/lhh",
  240. control_ids="com.yiche.autoeasy:id/lhh")
  241. redis_client.set(device_id + "operate", action8_id)
  242. redis_client.set(f"{device_id}_step8", "1")
  243. redis_client.delete(f"{device_id}_step7")
  244. return action8_dict
  245. """
  246. 根据标题搜索博主的发布的内容
  247. """
  248. step8 = redis_client.get(f"{device_id}_step8")
  249. if step8 is not None and int(step8) == 1 and last_action_id is not None and int(perform_action_id) == int(
  250. last_action_id) \
  251. and perform_action_result == "success":
  252. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  253. action9_id = int(round(time.time() * 1000))
  254. """
  255. 发送第9条指令
  256. 向下滑动若干屏幕
  257. 根据标题搜索博主的发布的内容
  258. """
  259. action9_dict = click_by_content(action9_id,
  260. target_app="yiche",
  261. target_version="11.11.0_c32",
  262. package_name="com.yiche.autoeasy",
  263. title=title)
  264. redis_client.set(device_id + "operate", action9_id)
  265. redis_client.set(f"{device_id}_step9", "1")
  266. redis_client.delete(f"{device_id}_step8")
  267. loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
  268. return action9_dict
  269. """
  270. 搜藏搜索到的帖子
  271. """
  272. step9 = redis_client.get(f"{device_id}_step9")
  273. if step9 is not None and int(step9) == 1 and last_action_id is not None and int(perform_action_id) == int(
  274. last_action_id) \
  275. and perform_action_result == "success":
  276. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  277. action10_id = int(round(time.time() * 1000))
  278. """
  279. 发送第10条指令
  280. 搜藏搜索到的帖子
  281. """
  282. action10_dict = single_click_by_control(action10_id, target_app="yiche",
  283. target_version="11.11.0_c32",
  284. package_name="com.yiche.autoeasy",
  285. control_id="com.yiche.autoeasy:id/aai",
  286. control_ids="com.yiche.autoeasy:id/aai")
  287. redis_client.set(device_id + "operate", action10_id)
  288. redis_client.set(f"{device_id}_step10", "1")
  289. redis_client.delete(f"{device_id}_step9")
  290. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
  291. return action10_dict
  292. """
  293. 点赞搜索到的帖子
  294. """
  295. step10 = redis_client.get(f"{device_id}_step10")
  296. if step10 is not None and int(step10) == 1 and last_action_id is not None and int(perform_action_id) == int(
  297. last_action_id) \
  298. and perform_action_result == "success":
  299. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  300. action11_id = int(round(time.time() * 1000))
  301. """
  302. 发送第11条指令
  303. 点赞搜索到的帖子
  304. """
  305. action11_dict = single_click_by_control(action11_id, target_app="yiche",
  306. target_version="11.11.0_c32",
  307. package_name="com.yiche.autoeasy",
  308. control_id="com.yiche.autoeasy:id/d44",
  309. control_ids="com.yiche.autoeasy:id/d44")
  310. redis_client.set(device_id + "operate", action11_id)
  311. redis_client.set(f"{device_id}_step12", "1")
  312. redis_client.delete(f"{device_id}_step10")
  313. loggerKit.info("设备:{0}, action11_id:{1}", device_id, action11_id)
  314. return action11_dict
  315. """
  316. 点击评论输入框
  317. """
  318. step12 = redis_client.get(f"{device_id}_step12")
  319. if step12 is not None and int(step12) == 1 and last_action_id is not None and int(perform_action_id) == int(
  320. last_action_id) \
  321. and perform_action_result == "success":
  322. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  323. action13_id = int(round(time.time() * 1000))
  324. """
  325. 发送第13条指令
  326. 评论搜索到的帖子
  327. 点击评论按钮
  328. """
  329. comment_response = httpx.get(get_commented_list_url + '?resourceName=' + title, timeout=120)
  330. # 评论实体
  331. comment_body = json.loads(comment_response.text)
  332. # 评论集合
  333. comment_list = comment_body.get('data')
  334. loggerKit.info("任务id:{0}对应标题:{1},已有评论:{2}", task_id, title, comment_list)
  335. existing_comment = ''
  336. if comment_list is not None:
  337. comment_list = list(set(comment_list))
  338. existing_comment = '||'.join(comment_list)
  339. loggerKit.info("任务id:{0}对应标题:{1},拼接后的评论集合:{2}", task_id, title, existing_comment)
  340. existing_comment = quote(existing_comment)
  341. request_data = {
  342. "mode": "1",
  343. "title": title,
  344. "platform": "yiche",
  345. "comment": existing_comment
  346. }
  347. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC信息:{2}", task_id, title, request_data)
  348. response = httpx.post(post_ai_gc_url, json=request_data, timeout=120)
  349. # 点击评论框
  350. action13_dict = single_click_by_control(action13_id, target_app="yiche",
  351. target_version="11.11.0_c32",
  352. package_name="com.yiche.autoeasy",
  353. control_id="com.yiche.autoeasy:id/d3p",
  354. control_ids="com.yiche.autoeasy:id/d3p")
  355. if not response.is_success:
  356. # 调用AIGC获取评论失败
  357. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, title,
  358. request_data, response)
  359. redis_client.set(device_id + "yiche" + "comments", '不错👍')
  360. redis_client.set(device_id + "operate", action13_id)
  361. redis_client.set(f"{device_id}_step13", "1")
  362. redis_client.delete(f"{device_id}_step12")
  363. loggerKit.info("设备:{0}, action13_id:{1}", device_id, action13_id)
  364. response_body = json.loads(response.text)
  365. reply_data = response_body.get('data')
  366. reply = reply_data.get('comment')
  367. if '内容太少' in reply or '对不起' in reply or reply is None or reply == '' or '提供' in reply:
  368. # 获取到的评论内容不符合
  369. loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, title, request_data,
  370. response)
  371. redis_client.set(device_id + "yiche" + "comments", '不错👍')
  372. redis_client.set(device_id + "operate", action13_id)
  373. redis_client.set(f"{device_id}_step13", "1")
  374. redis_client.delete(f"{device_id}_step12")
  375. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action13_id)
  376. return action13_dict
  377. # 将需要评论的内容存入缓存
  378. redis_client.set(device_id + "yiche" + "comments", reply)
  379. redis_client.set(device_id + "operate", action13_id)
  380. redis_client.set(f"{device_id}_step13", "1")
  381. redis_client.delete(f"{device_id}_step12")
  382. loggerKit.info("设备:{0}, action10_id:{1}", device_id, action13_id)
  383. return action13_dict
  384. """
  385. 输入评论内容
  386. """
  387. step13 = redis_client.get(f"{device_id}_step13")
  388. if step13 is not None and int(step13) == 1 and last_action_id is not None and int(perform_action_id) == int(
  389. last_action_id) \
  390. and perform_action_result == "success":
  391. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  392. action14_id = int(round(time.time() * 1000))
  393. """
  394. 发送第14条指令
  395. 输入评论内容
  396. """
  397. reply = redis_client.get(device_id + "yiche" + "comments")
  398. if reply is None:
  399. # 调用AIGC获取评论失败
  400. del_key_vague(device_id)
  401. return_dict = {
  402. "data": "",
  403. "code": -2,
  404. "message": "fail, result_text is null"
  405. }
  406. call_back_task_reply(500, '获取到的内容标题为null,无法评论',
  407. task_id, device_id, 0, None, None)
  408. return return_dict
  409. action14_dict = send_text_by_control(action14_id, target_app="yiche",
  410. target_version="11.11.0_c32",
  411. package_name="com.yiche.autoeasy",
  412. control_id="com.yiche.autoeasy:id/efd",
  413. content=reply,
  414. timeout=5)
  415. content_result = content_detail(title, title, reply, None)
  416. redis_client.set(device_id + "reply_json", json.dumps(content_result.to_dict(), ensure_ascii=False))
  417. redis_client.set(device_id + "operate", action14_id)
  418. redis_client.set(f"{device_id}_step14", "1")
  419. redis_client.delete(f"{device_id}_step13")
  420. loggerKit.info("设备:{0}, action14_id:{1}", device_id, action14_id)
  421. return action14_dict
  422. """
  423. 点击评论发送按钮
  424. """
  425. step14 = redis_client.get(f"{device_id}_step14")
  426. if step14 is not None and int(step14) == 1 and last_action_id is not None and int(perform_action_id) == int(
  427. last_action_id) \
  428. and perform_action_result == "success":
  429. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  430. action15_id = int(round(time.time() * 1000))
  431. """
  432. 发送第15条指令
  433. 点击评论发送按钮
  434. """
  435. action15_dict = single_click_by_control(action15_id, target_app="yiche",
  436. target_version="11.11.0_c32",
  437. package_name="com.yiche.autoeasy",
  438. control_id="com.yiche.autoeasy:id/j09",
  439. control_ids="com.yiche.autoeasy:id/j09",
  440. timeout=5)
  441. redis_client.set(device_id + "operate", action15_id)
  442. redis_client.set(f"{device_id}_step15", "1")
  443. redis_client.delete(f"{device_id}_step14")
  444. loggerKit.info("设备:{0}, action11_id:{1}", device_id, action15_id)
  445. return action15_dict
  446. step15 = redis_client.get(f"{device_id}_step15")
  447. if step15 is not None and int(step15) == 1 and last_action_id is not None and int(perform_action_id) == int(
  448. last_action_id):
  449. loggerKit.info("设备:{0}, last_action_id:{1}", device_id, int(last_action_id))
  450. action16_id = int(round(time.time() * 1000))
  451. """
  452. 发送第16条指令
  453. 关闭app
  454. """
  455. action16_dict = stop_app(action16_id, target_app="yiche",
  456. target_version="11.11.0_c32",
  457. package_name="com.yiche.autoeasy")
  458. reply_json = redis_client.get(device_id + "reply_json")
  459. del_key_vague(device_id)
  460. loggerKit.info("设备:{0}, action12_id:{1}", device_id, action16_id)
  461. # 回调任务中心修改任务状态
  462. call_back_task_reply(None, None, task_id, device_id, 1, None, reply_json)
  463. return action16_dict
  464. else:
  465. action0_id = int(round(time.time() * 1000))
  466. """
  467. 启动指令
  468. 启动app
  469. """
  470. action0_dict = start_app(action0_id, target_app="yiche",
  471. target_version="11.11.0",
  472. package_name="com.yiche.autoeasy")
  473. redis_client.set(device_id + "operate", action0_id)
  474. redis_client.set(f"{device_id}_step0", "1")
  475. loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
  476. return action0_dict
  477. # 回复详情
  478. class content_detail:
  479. def __init__(self, keyword, title, reply, account_name):
  480. """
  481. :rtype: object
  482. """
  483. # 搜索关键词
  484. self.keyword = keyword
  485. self.title = title
  486. self.reply = reply
  487. self.account_name = account_name
  488. def to_dict(self):
  489. return {
  490. 'keyword': self.keyword,
  491. 'title': self.title,
  492. 'reply': self.reply,
  493. 'accountName': self.account_name
  494. }