create_ad.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. from wechat_action.login_ad import LogIn
  2. from selenium import webdriver
  3. from selenium.webdriver import ActionChains
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. from selenium.webdriver import ChromeOptions
  8. import time
  9. import random
  10. import re
  11. class CreateAd:
  12. def __init__(self):
  13. # TODO:创建完页面需要关闭所有页面并回到一开始的页面
  14. self.driver = LogIn().get_driver_loged()
  15. self.get_into_create_page()
  16. self.send_file_limit_num = 8
  17. def get_into_create_page(self):
  18. # 进入创建页面
  19. self.driver.find_element_by_id('material').click()
  20. WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('ui-fl-r'))
  21. time.sleep(random.uniform(0.1, 0.2))
  22. self.driver.find_element_by_class_name('ui-fl-r').click()
  23. WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_css_selector(
  24. '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button'))
  25. self.driver.find_element_by_css_selector(
  26. '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button').click()
  27. self.driver.switch_to.window(self.driver.window_handles[-1])
  28. WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('addContent-8pexaaAGYy'))
  29. self.driver.find_element_by_class_name('addContent-8pexaaAGYy').click()
  30. WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('topArea-qOwEAeNuIn'))
  31. def create_layout(self):
  32. # 创建编排
  33. pass
  34. def set_head_assemb(self, info):
  35. def single_page_set():
  36. pass
  37. def multi_page_set():
  38. self.driver.find_element_by_css_selector(
  39. '#stage-sidebar > div.topArea-qOwEAeNuIn > div > div:nth-child(2)').click()
  40. time.sleep(random.uniform(0.1, 0.2))
  41. page_size = len(info_v)
  42. if page_size == 3:
  43. pass
  44. if page_size == 4:
  45. self.driver.find_element_by_css_selector(
  46. '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(2)').click()
  47. if page_size == 6:
  48. self.driver.find_element_by_css_selector(
  49. '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(3)').click()
  50. time.sleep(random.uniform(0.1, 0.2))
  51. self.driver.find_element_by_class_name('imageUploadItem-tA9JX0RWua').click()
  52. WebDriverWait(self.driver, 1000).until(
  53. lambda driver: self.driver.find_element_by_class_name('adui-tabs-tab')
  54. )
  55. input_elements = self.driver.find_elements_by_tag_name('input')
  56. input_find_element = None
  57. for _ in input_elements:
  58. if '输入关键词搜索素材' in _.get_attribute('placeholder'):
  59. input_find_element = _
  60. from selenium.webdriver.common.keys import Keys
  61. for _ in info_v:
  62. import os
  63. file_name = os.path.basename(_)
  64. print(file_name)
  65. input_find_element.clear()
  66. print('clear')
  67. print(input_find_element.get_attribute('value'))
  68. time.sleep(5)
  69. #TODO:input----clear 进行清空有问题
  70. input_find_element.send_keys(file_name)
  71. input_find_element.send_keys(Keys.RETURN)
  72. WebDriverWait(self.driver, 1000).until(
  73. lambda driver: driver.find_element_by_class_name('imgWrp-3KLtxlD1Db'))
  74. file_element = self.driver.find_element_by_class_name('imgWrp-3KLtxlD1Db')
  75. WebDriverWait(self.driver, 1000).until(
  76. lambda x: (file_element.is_displayed() and file_element.is_enabled()))
  77. file_element.click()
  78. time.sleep(random.uniform(1,4))
  79. time.sleep(1000)
  80. self.driver.find_elements_by_class_name('adui-tabs-tab')[1].click()
  81. for i in range(len(info_v)):
  82. self.driver.find_element_by_xpath("//div[starts-with(@id,'js_multi_upload_material')]/input").send_keys(
  83. info_v[i])
  84. info_key = list(info.keys())[0]
  85. info_v = info[info_key]
  86. if info_key == 'page':
  87. single_page_set()
  88. if info_key == 'multi_page':
  89. multi_page_set()
  90. # 设置头部组件
  91. pass
  92. def set_page(self):
  93. # 设置图片模块
  94. pass
  95. def set_title(self):
  96. # 设置标题模块
  97. pass
  98. def set_content(self):
  99. # 输入文本内容
  100. pass
  101. def set_follow_button(self):
  102. # 设置关注按钮
  103. pass
  104. def set_set_text_button(self):
  105. # 设置图文按钮
  106. pass
  107. def send_file_multi(self, layout, err_num=0):
  108. # 上传文件,文件上传与整体流程切割开
  109. token = re.findall('token=(\d+)', self.driver.current_url)
  110. url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
  111. token[0])
  112. js = "window.open('{}')".format(url_)
  113. print(js)
  114. self.driver.execute_script(js)
  115. time.sleep(random.uniform(1, 2))
  116. self.driver.switch_to.window(self.driver.window_handles[-1])
  117. file_set = set()
  118. for v in layout.values():
  119. if isinstance(v, dict):
  120. if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
  121. v_name = list(v.keys())[0]
  122. if isinstance(v[v_name], list):
  123. for _ in v[v_name]:
  124. file_set.add(_)
  125. else:
  126. file_set.add(v[v_name])
  127. file_list = list(file_set)
  128. print(file_list)
  129. print('file list size', len(file_list))
  130. send_file_sign = self.send_file_limit_num
  131. for _ in file_list:
  132. print(_)
  133. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(_)
  134. time.sleep(100)
  135. while True:
  136. # 失败,失败直接退出重跑
  137. # 正在上传数量,如果是正在上传小于限定数量,就不断输入
  138. # 上传完毕,小于限定数量,不断输入,.....已经上传好了,就关闭
  139. time.sleep(1)
  140. send_reslut_content = self.driver.find_element_by_xpath(
  141. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
  142. if '失败' in send_reslut_content:
  143. if err_num < 3:
  144. self.send_file(layout, err_num=err_num + 1)
  145. else:
  146. exit()
  147. if '正在上传' in send_reslut_content:
  148. if send_file_sign < len(file_list):
  149. send_num = re.findall('(\d+)', send_reslut_content)[0]
  150. if int(send_num) < self.send_file_limit_num:
  151. # 还可以继续上传
  152. x = send_file_sign
  153. can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
  154. y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
  155. for i in range(x, y):
  156. send_file_sign = send_file_sign + 1
  157. self.driver.find_element_by_xpath(
  158. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
  159. else:
  160. pass
  161. if '上传成功' in send_reslut_content:
  162. if send_file_sign < len(file_list):
  163. send_num = re.findall('(\d+)', send_reslut_content)[0]
  164. if int(send_num) < self.send_file_limit_num:
  165. # 还可以继续上传
  166. x = send_file_sign
  167. can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
  168. y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
  169. for i in range(x, y):
  170. send_file_sign = send_file_sign + 1
  171. self.driver.find_element_by_xpath(
  172. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
  173. else:
  174. break
  175. time.sleep(1000)
  176. self.driver.execute_script('window.close();')
  177. def send_file(self, file_path, err_num=0):
  178. self.driver.execute_script('location.reload();')
  179. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_path)
  180. def send_file_alone(self, layout):
  181. # 上传文件,单线程
  182. # 上传文件,文件上传与整体流程切割开
  183. token = re.findall('token=(\d+)', self.driver.current_url)
  184. url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
  185. token[0])
  186. js = "window.open('{}')".format(url_)
  187. self.driver.execute_script(js)
  188. time.sleep(random.uniform(1, 2))
  189. self.driver.switch_to.window(self.driver.window_handles[-1])
  190. file_set = set()
  191. for v in layout.values():
  192. if isinstance(v, dict):
  193. if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
  194. v_name = list(v.keys())[0]
  195. if isinstance(v[v_name], list):
  196. for _ in v[v_name]:
  197. file_set.add(_)
  198. else:
  199. file_set.add(v[v_name])
  200. for _ in file_set:
  201. self.send_file(_)
  202. err_num = 0
  203. while True:
  204. time.sleep(1)
  205. send_reslut_content = self.driver.find_element_by_xpath(
  206. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
  207. if '上传成功' in send_reslut_content:
  208. break
  209. if '失败' in send_reslut_content:
  210. err_num = err_num + 1
  211. self.send_file(_)
  212. if err_num > 3:
  213. # TODO:退出机制需要考虑一下,tornado返回错误信息
  214. exit()
  215. self.driver.execute_script('window.close();')
  216. self.driver.switch_to.window(self.driver.window_handles[-1])
  217. def send_moive_spic(self):
  218. # 特殊情况下,上传本地视频
  219. pass
  220. def get_layout(self):
  221. # 得到编排
  222. layout = {-1: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
  223. 0: {'title': '这是标题'},
  224. 1: {'content': '这是文本'},
  225. 2: 'follow_button-1',
  226. 3: {'title': '这是标题'},
  227. 4: {'content': '这是文本'},
  228. 5: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
  229. 6: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
  230. 7: 'follow_button-2',
  231. 8: 'follow_button-3',
  232. }
  233. # TODO:测试,多图,测试,图文表
  234. layout = {-1: {'multi_page': ['/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg',
  235. '/usr/share/wallpapers/deepin/Sunset_of_the_Lake_Nam_by_Wang_Jinyu.jpg',
  236. '/usr/share/wallpapers/deepin/The_Gongga_Mountain_by_wangjinyu.jpg',
  237. '/usr/share/wallpapers/deepin/Reflection_of_the_Kanas_Lake_by_Wang_Jinyu.jpg']},
  238. 0: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'}
  239. }
  240. # self.send_file_alone(layout)
  241. self.set_head_assemb(layout[-1])
  242. for _ in range(max(layout.keys())):
  243. pass
  244. time.sleep(100)
  245. if __name__ == '__main__':
  246. create_ad = CreateAd()
  247. create_ad.get_layout()