| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- import time
- from func.action_func import del_key_vague
- from task.task_job import callback_task
- from tools import loggerKit, redis_client
- from scene.oprator.atom_data import start_app, stop_app, single_click_by_control, \
- send_text_by_control, single_click_by_text, get_text_by_control, click_pic
- from tools.pic_base64_util import pic_to_base64
- import requests
- import json
- import threading
- import yaml
- import httpx
- with open('config.yaml', 'r') as file:
- config = yaml.load(file, Loader=yaml.FullLoader)
- extern_domain = config['bmp-cp']['extern_domain']
- # 调用AIGC接口
- post_ai_gc_url = config['bmp-cp']['post_ai_gc_url']
- # 任务执行回调url
- task_callback_url = extern_domain + config['bmp-cp']['task_callback_url']
- # 抖音极速版用户操作(对指定用户作品进行点赞 评论和收藏)
- # version 28.8.0
- def douyin_spider(task_id, keyword, data):
- loggerKit.info('请求信息:{0}'.format(data))
- device_id = data.get("deviceID")
- perform_action_id = data.get("performActionId")
- result = data.get("result")
- if result is not None:
- """
- 非首个指令
- """
- perform_action_result = result.get("performActionResult")
- if perform_action_result is None:
- return_dict = {
- "data": "",
- "code": -2,
- "message": "fail, performActionResult is null"
- }
- # 回调任务中心
- del_key_vague(device_id)
- callback_task(500, '指令执行失败', task_id, device_id, 0, None)
- return return_dict
- if perform_action_result != "success":
- # 回调任务中心
- return_dict = {
- "data": "",
- "code": -2,
- "message": "fail, performActionResult is null"
- }
- del_key_vague(device_id)
- callback_task(500, '任务执行失败,可能当日已签到过', task_id, device_id, 0, None)
- return return_dict
- # 每次操作完成后会将对应的操作唯一id存储到redis,并且返回给手机端 手机端下次带着上个操作id来执行下一个操作
- last_action_id = redis_client.get(device_id + "operate")
- step0 = redis_client.get(f"{device_id}_step0")
- if step0 is not None and int(step0) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) and perform_action_result == "success":
- action1_id = int(round(time.time() * 1000))
- """
- 发送第1条指令
- 点击急速版抖音搜索按钮
- """
- action1_dict = single_click_by_control(action1_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/hvy",
- control_ids="com.ss.android.ugc.aweme.lite:id/hvy,"
- "com.ss.android.ugc.aweme.lite:id/lf3")
- redis_client.set(device_id + "operate", action1_id)
- redis_client.set(f"{device_id}_step1", "1")
- redis_client.delete(f"{device_id}_step0")
- loggerKit.info("设备:{0}, action1_id:{1}", device_id, action1_id)
- return action1_dict
- step1 = redis_client.get(f"{device_id}_step1")
- if step1 is not None and int(step1) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) and perform_action_result == "success":
- loggerKit.info("设备:{0}, action2_id_mem:{1}", device_id, int(last_action_id))
- action2_id = int(round(time.time() * 1000))
- """
- 发送第2条指令
- 对搜索框进行赋值
- """
- action2_dict = send_text_by_control(action2_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/et_search_kw",
- content=keyword, timeout=5)
- redis_client.set(device_id + "operate", action2_id)
- redis_client.set(f"{device_id}_step2", "1")
- redis_client.delete(f"{device_id}_step1")
- loggerKit.info("设备:{0}, action2_id:{1}", device_id, action2_id)
- return action2_dict
- step2 = redis_client.get(f"{device_id}_step2")
- if step2 is not None and int(step2) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) and perform_action_result == "success":
- loggerKit.info("设备:{0}, action3_id_mem:{1}", device_id, int(last_action_id))
- action3_id = int(round(time.time() * 1000))
- """
- 发送第3条指令
- 点击搜索按钮 根据图片识别
- """
- action3_dict = single_click_by_control(action3_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/nup")
- redis_client.set(device_id + "operate", action3_id)
- redis_client.set(f"{device_id}_step3", "1")
- redis_client.delete(f"{device_id}_step2")
- loggerKit.info("设备:{0}, action3_id:{1}", device_id, action3_id)
- return action3_dict
- step3 = redis_client.get(f"{device_id}_step3")
- if step3 is not None and int(step3) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action4_id_mem:{1}", device_id, int(last_action_id))
- action4_id = int(round(time.time() * 1000))
- """
- 发送第4条指令
- 点击用户按钮
- """
- action4_dict = single_click_by_text(action4_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- text="用户", timeout=5)
- redis_client.set(device_id + "operate", action4_id)
- redis_client.set(f"{device_id}_step4", "1")
- redis_client.delete(f"{device_id}_step3")
- loggerKit.info("设备:{0}, action4_id:{1}", device_id, action4_id)
- return action4_dict
- step4 = redis_client.get(f"{device_id}_step4")
- if step4 is not None and int(step4) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action5_id_mem:{1}", device_id, int(last_action_id))
- action5_id = int(round(time.time() * 1000))
- """
- 发送第5条指令
- 点击用户tab下的第一个用户
- """
- action5_dict = single_click_by_control(action5_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- item_index=0,
- control_id="com.ss.android.ugc.aweme.lite:id/gzs", timeout=5)
- redis_client.set(device_id + "operate", action5_id)
- redis_client.set(f"{device_id}_step5", "1")
- redis_client.delete(f"{device_id}_step4")
- loggerKit.info("设备:{0}, action5_id:{1}", device_id, action5_id)
- return action5_dict
- step5 = redis_client.get(f"{device_id}_step5")
- if step5 is not None and int(step5) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action6_id_mem:{1}", device_id, int(last_action_id))
- action6_id = int(round(time.time() * 1000))
- """
- 发送第6条指令
- 进入用户对应的视频
- """
- action6_dict = single_click_by_control(action6_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/hld", timeout=5)
- redis_client.set(device_id + "operate", action6_id)
- redis_client.set(f"{device_id}_step6", "1")
- redis_client.delete(f"{device_id}_step5")
- loggerKit.info("设备:{0}, action6_id:{1}", device_id, action6_id)
- return action6_dict
- step6 = redis_client.get(f"{device_id}_step6")
- if step6 is not None and int(step6) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action7_id_mem:{1}", device_id, int(last_action_id))
- action7_id = int(round(time.time() * 1000))
- """
- 发送第7条指令
- 进行点赞
- """
- action7_dict = single_click_by_control(action7_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/cug", timeout=5)
- redis_client.set(device_id + "operate", action7_id)
- redis_client.set(f"{device_id}_step7", "1")
- redis_client.delete(f"{device_id}_step6")
- loggerKit.info("设备:{0}, action7_id:{1}", device_id, action7_id)
- return action7_dict
- step7 = redis_client.get(f"{device_id}_step7")
- if step7 is not None and int(step7) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action8_id_mem:{1}", device_id, int(last_action_id))
- action8_id = int(round(time.time() * 1000))
- """
- 发送第8条指令
- 收藏
- """
- action8_dict = single_click_by_control(action8_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/bn=", timeout=5)
- redis_client.set(device_id + "operate", action8_id)
- redis_client.set(f"{device_id}_step8", "1")
- redis_client.delete(f"{device_id}_step7")
- loggerKit.info("设备:{0}, action8_id:{1}", device_id, action8_dict)
- return action8_dict
- step8 = redis_client.get(f"{device_id}_step8")
- if step8 is not None and int(step8) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action9_id_mem:{1}", device_id, int(last_action_id))
- action9_id = int(round(time.time() * 1000))
- """
- 发送第9条指令
- 获取到用户的帖子内容
- """
- action9_dict = get_text_by_control(action9_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- max_page='1',
- control_id="com.ss.android.ugc.aweme.lite:id/desc", timeout=5)
- redis_client.set(device_id + "operate", action9_id)
- redis_client.set(f"{device_id}_step9", "1")
- redis_client.delete(f"{device_id}_step8")
- loggerKit.info("设备:{0}, action9_id:{1}", device_id, action9_id)
- return action9_dict
- step9 = redis_client.get(f"{device_id}_step9")
- if step9 is not None and int(step9) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action4_id_mem:{1}", device_id, int(last_action_id))
- action10_id = int(round(time.time() * 1000))
- """
- 发送第10条指令
- 根据视频内容 生成评论文案将文案缓存 并且点击评论
- """
- result_text = result.get("performActionText")
- if result_text is None:
- return_dict = {
- "data": "",
- "code": -2,
- "message": "fail, result_text is null"
- }
- # 回调任务中心
- del_key_vague(device_id)
- callback_task(500, '获取到的内容标题为null,无法评论', task_id, device_id, 0, None)
- return return_dict
- request_data = {
- "templateId": "dcd_comment",
- "content": result_text,
- "existingComment": None
- }
- loggerKit.info("任务id:{0}对应标题:{1},请求AIGC信息:{2}", task_id, keyword, request_data)
- response = httpx.post(post_ai_gc_url, json=request_data, timeout=120)
- # 点击评论框
- action10_dict = single_click_by_control(action10_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/comment_container",
- timeout=5)
- if not response.is_success:
- # 调用AIGC获取评论失败
- loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, keyword, request_data, response)
- redis_client.set(device_id + "douyin" + "comments", '不错👍')
- redis_client.set(device_id + "operate", action10_id)
- redis_client.set(f"{device_id}_step10", "1")
- redis_client.delete(f"{device_id}_step9")
- loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
- return action10_dict
- response_body = json.loads(response.text)
- data = response_body.get('data')
- reply_text = data.get('answer')
- reply_text_json = json.loads(reply_text)
- reply = reply_text_json.get('comment')
- if '内容太少' in reply or '对不起' in reply or reply is None or reply == '' or '提供' in reply:
- # 获取到的评论内容不符合
- loggerKit.info("任务id:{0}对应标题:{1},请求AIGC失败信息:{2},返回信息:{3}", task_id, keyword, request_data, response)
- redis_client.set(device_id + "douyin" + "comments", '不错👍')
- redis_client.set(device_id + "operate", action10_id)
- redis_client.set(f"{device_id}_step10", "1")
- redis_client.delete(f"{device_id}_step9")
- loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
- return action10_dict
- # 将需要评论的内容存入缓存
- redis_client.set(device_id + "douyin" + "comments", reply)
- redis_client.set(device_id + "operate", action10_id)
- redis_client.set(f"{device_id}_step10", "1")
- redis_client.delete(f"{device_id}_step9")
- loggerKit.info("设备:{0}, action10_id:{1}", device_id, action10_id)
- return action10_dict
- step10 = redis_client.get(f"{device_id}_step10")
- if step10 is not None and int(step10) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action11_id_mem:{1}", device_id, int(last_action_id))
- action11_id = int(round(time.time() * 1000))
- """
- 发送第11条指令
- 点击评论框
- """
- reply = redis_client.get(device_id + "douyin" + "comments")
- if reply is None:
- # 调用AIGC获取评论失败
- del_key_vague(device_id)
- return_dict = {
- "data": "",
- "code": -2,
- "message": "fail, result_text is null"
- }
- callback_task(500, '获取到的内容标题为null,无法评论', task_id, device_id, 0, None)
- return return_dict
- action11_dict = single_click_by_control(action11_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/brv", timeout=5)
- redis_client.set(device_id + "operate", action11_id)
- redis_client.set(f"{device_id}_step11", "1")
- redis_client.delete(f"{device_id}_step10")
- loggerKit.info("设备:{0}, action11_id:{1}", device_id, action11_id)
- return action11_dict
- step11 = redis_client.get(f"{device_id}_step11")
- if step11 is not None and int(step11) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action12_id_mem:{1}", device_id, int(last_action_id))
- action12_id = int(round(time.time() * 1000))
- """
- 发送第12条指令
- 对评论框进行赋值
- """
- reply = redis_client.get(device_id + "douyin" + "comments")
- if reply is None:
- # 调用AIGC获取评论失败
- del_key_vague(device_id)
- return_dict = {
- "data": "",
- "code": -2,
- "message": "fail, result_text is null"
- }
- callback_task(500, '获取到的内容标题为null,无法评论', task_id, device_id, 0, None)
- return return_dict
- action12_dict = send_text_by_control(action12_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/brv",
- content=reply, timeout=5)
- redis_client.set(device_id + "operate", action12_id)
- redis_client.set(f"{device_id}_step12", "1")
- redis_client.delete(f"{device_id}_step11")
- loggerKit.info("设备:{0}, action12_id:{1}", device_id, action12_id)
- return action12_dict
- step12 = redis_client.get(f"{device_id}_step12")
- if step12 is not None and int(step12) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) \
- and perform_action_result == "success":
- loggerKit.info("设备:{0}, action13_id_mem:{1}", device_id, int(last_action_id))
- action13_id = int(round(time.time() * 1000))
- """
- 发送第13条指令
- 发送评论
- """
- action12_dict = single_click_by_control(action13_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite",
- control_id="com.ss.android.ugc.aweme.lite:id/bcw", timeout=5)
- redis_client.set(device_id + "operate", action13_id)
- redis_client.set(f"{device_id}_step13", "1")
- redis_client.delete(f"{device_id}_step12")
- loggerKit.info("设备:{0}, action13_id:{1}", device_id, action12_dict)
- return action12_dict
- """
- 停止app
- """
- step13 = redis_client.get(f"{device_id}_step13")
- if step13 is not None and int(step13) == 1 and last_action_id is not None and int(perform_action_id) == int(
- last_action_id) and perform_action_result == "success":
- loggerKit.info("设备:{0}, action14_id_mem:{1}", device_id, int(last_action_id))
- action14_id = int(round(time.time() * 1000))
- """
- 停止指令
- 停止app
- """
- action14_dict = stop_app(action14_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite", timeout=5)
- redis_client.delete(f"{device_id}_step4")
- del_key_vague(device_id)
- loggerKit.info("设备:{0}, action14_id:{1}", device_id, action14_id)
- # 回调任务中心修改任务状态
- callback_task(None, None, task_id, device_id, 1, None)
- return action14_dict
- else:
- action0_id = int(round(time.time() * 1000))
- """
- 启动指令
- 启动app
- """
- action0_dict = start_app(action0_id, target_app="douyin", target_version="28.8.0",
- package_name="com.ss.android.ugc.aweme.lite")
- redis_client.set(device_id + "operate", action0_id)
- redis_client.set(f"{device_id}_step0", "1")
- redis_client.delete(f"{device_id}_step9")
- loggerKit.info("设备:{0}, action0_id:{1}", device_id, action0_id)
- return action0_dict
- # 批量模糊删除缓存
- # def del_key_vague(device_id):
- # # 批量模糊删除keys
- # keys = redis_client.match_pattern_prefix(device_id)
- # if len(keys) > 0:
- # # 需要判断是否有匹配的值, 没有的话会报错
- # for key in keys:
- # redis_client.delete(key)
- # loggerKit.info(f"clear {device_id} keys success...")
- # else:
- # loggerKit.info(f"{device_id} keys none ...")
- # post请求
- # def call_back_request(error_code, error_msg, task_id, task_status, task_execute_response, content, demo_flag):
- # response = {
- # "errorCode": error_code,
- # "errorMsg": error_msg,
- # "taskId": task_id,
- # "taskStatus": task_status,
- # "taskExecuteResponse": task_execute_response,
- # "content": content,
- # "demoFlag": demo_flag
- # }
- # return response
- #
- #
- # # 回调任务中心接口
- # def callback_task(err_code, err_msg, task_id, device_id, execute_status, result):
- # callback_request2 = call_back_request(err_code, err_msg, task_id, execute_status, result, None, None)
- # loggerKit.info("thread[{0}=>{1}], taskId:{2}, 设备号:{3}, url:{4} 。回调开始", threading.current_thread().name,
- # threading.get_ident(), task_id, device_id, task_callback_url)
- # current_timeout = (5, 10)
- # callback_response2 = requests.post(task_callback_url, json=callback_request2, timeout=current_timeout)
- # loggerKit.info("thread[{0}=>{1}], taskId:{2},设备号:{3}。回调结束:{4}", threading.current_thread().name,
- # threading.get_ident(), task_id, device_id, json.dumps(callback_response2.text, ensure_ascii=False))
- # return callback_response2
|