|
@@ -1,24 +1,22 @@
|
|
|
-from selenium import webdriver
|
|
|
from selenium.webdriver import ActionChains
|
|
|
-from selenium.webdriver.common.by import By
|
|
|
-from selenium.webdriver.support import expected_conditions as EC
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
from wechat_action.create_ad_plan_idea import IdeaAction
|
|
|
from wechat_action.human_ad import HumanAd
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
-from wechat_action.login_ad import LogIn
|
|
|
-from selenium.webdriver import ChromeOptions
|
|
|
import re
|
|
|
import time
|
|
|
import logging
|
|
|
import random
|
|
|
+import requests
|
|
|
|
|
|
|
|
|
class CreateAdPlan():
|
|
|
# TODO:因为人群包现在可能存在唯一性,公众号间的复制,需要怎么处理
|
|
|
- def __init__(self, task, login_ad):
|
|
|
+ def __init__(self, task, login_ad, service_name, wechat_name):
|
|
|
self.task = task
|
|
|
# self.task_check()
|
|
|
+ self.service_name = service_name
|
|
|
+ self.wechat_name = wechat_name
|
|
|
self.login_ad = login_ad
|
|
|
self.driver = login_ad.get_driver_loged()
|
|
|
self.ad_idea_action = IdeaAction(self.driver, task)
|
|
@@ -306,35 +304,23 @@ class CreateAdPlan():
|
|
|
# exit()
|
|
|
pass
|
|
|
|
|
|
- def check_is_sucess(self, plan_name):
|
|
|
+ def check_is_sucess(self):
|
|
|
logging.info('检查广告计划创建是否成功')
|
|
|
- # TODO:进一步修改
|
|
|
-
|
|
|
- # 进入搜索页面,
|
|
|
- click_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "navItem-3MjWMCbkWT")]/span')
|
|
|
- for _ in click_elements:
|
|
|
- if '广告' in _.text:
|
|
|
- _.click()
|
|
|
-
|
|
|
- time.sleep(random.uniform(0.1, 0.2))
|
|
|
- # 设置状态为--审核中
|
|
|
- choice_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "tag-3loTlkncVq")]/span')
|
|
|
- for _ in choice_elements:
|
|
|
- if '状态' in _.text:
|
|
|
- _.click()
|
|
|
- time.sleep(random.uniform(0.1, 0.2))
|
|
|
- lable_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "label-2ZOAbuO31o")]/span')
|
|
|
- for _ in lable_elements:
|
|
|
- if '审核中' in _.text:
|
|
|
- _.click()
|
|
|
-
|
|
|
- # 输入搜索字段
|
|
|
- input_element = self.driver.find_element_by_xpath('//*[contains(@class, "search-2yiDvyFHoH")]/span')
|
|
|
- input_element.click()
|
|
|
- time.sleep(random.uniform(0.1, 0.2))
|
|
|
- input_element.send_keys()
|
|
|
-
|
|
|
- # 查看有无对应数据
|
|
|
+ plan_name = self.task['title'].replace(' ', '')[:29]
|
|
|
+ WebDriverWait(self.login_ad.driver, 100).until(
|
|
|
+ lambda driver: True if 'token' in self.login_ad.driver.current_url else False)
|
|
|
+ cookie_dict = self.login_ad.get_cookie(self.login_ad.driver, login_cookie=False)
|
|
|
+ token_id = re.findall('token=(\d+)', self.login_ad.driver.current_url)[0]
|
|
|
+ time_big = str(int(time.time() * 1000))
|
|
|
+ time_small = str(int(time.time()))
|
|
|
+ token = token_id
|
|
|
+ plan_check_url = 'https://mp.weixin.qq.com/promotion/as_rock?action=get_campaign_data&args={"op_type":1,"where":{},"page":1,"page_size":20,"pos_type":999,"ad_filter":{"name":"' \
|
|
|
+ + plan_name + '"},"advanced":true,"create_time_range":{"start_time":1610884851},"time_range":{"start_time":1610748800,"last_time":' + time_small + '}}&token=' + token + '&appid=&spid=&_=' + time_big
|
|
|
+ rsp = requests.get(plan_check_url, cookies=cookie_dict)
|
|
|
+ print(rsp.text)
|
|
|
+ if 'list' in rsp.json():
|
|
|
+ if len(rsp.json()['list']):
|
|
|
+ return True
|
|
|
|
|
|
def run(self, err_num=0):
|
|
|
try:
|
|
@@ -355,17 +341,26 @@ class CreateAdPlan():
|
|
|
self.push_ad()
|
|
|
|
|
|
# 切回到前一页
|
|
|
- self.driver.switch_to.window(self.driver.window_handles[-1])
|
|
|
- logging.info('创建广告计划成功')
|
|
|
- return {'sucess': True, 'result_info': ''}
|
|
|
+ if self.check_is_sucess():
|
|
|
+ logging.info('创建广告计划成功')
|
|
|
+ return {'sucess': True, 'result_info': ''}
|
|
|
+ else:
|
|
|
+ if err_num > 3:
|
|
|
+ return {'sucess': False, 'result_info': '过程中全程无错误,失败'}
|
|
|
+ else:
|
|
|
+
|
|
|
+ self.login_ad.refresh_driver()
|
|
|
+ self.login_ad.select_ad_master(service_name=self, wechat_name=self.wechat_name)
|
|
|
+ return self.run(err_num=err_num + 1)
|
|
|
except Exception as e:
|
|
|
time.sleep(5)
|
|
|
self.driver.save_screenshot('liuyi{}.png'.format(time.time()))
|
|
|
time.sleep(5)
|
|
|
- raise
|
|
|
+
|
|
|
# TODO:有空时讲 e 内容设置为原始内容
|
|
|
if err_num > 3:
|
|
|
return {'sucess': False, 'result_info': str(e)}
|
|
|
else:
|
|
|
+ self.login_ad.refresh_driver()
|
|
|
+ self.login_ad.select_ad_master(service_name=self, wechat_name=self.wechat_name)
|
|
|
return self.run(err_num=err_num + 1)
|
|
|
- pass
|