-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (176 loc) · 7.72 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from selenium import webdriver
import os
import time
from glob import glob
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# from lxml import etree
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support import ui
from webdriver_manager.chrome import ChromeDriverManager
def xpath_soup(element):
components = []
child = element if element.name else element.parent
for parent in child.parents:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
child = parent
components.reverse()
return '/%s' % '/'.join(components)
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
options=options) # version_main allows to specify your chrome version instead of following chrome global version
driver.maximize_window()
driver.get("https://www.pinterest.com/login/")
time.sleep(20)
title = input("Enter title : ")
description = input("Enter description : ")
link = input("Enter link : ")
choose = input("Enter \"y\" after logging in : ")
if choose == "y":
driver.get("https://www.pinterest.com/pin-builder/")
selected_element = None
while (True):
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
selected_element = soup.find("input", attrs={"aria-label": "File upload"})
if selected_element is not None:
break
time.sleep(1)
time.sleep(5)
images = glob(os.getcwd() + "\\bulk_post_pinterest" + "\\*")
for i in range(len(images)):
img = images[i]
print("Posting image no. => " + str(i + 1) + " out of " + str(len(images)))
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
drag_drop_div = soup.find("input", attrs={"aria-label": "File upload"})
driver.find_element(By.XPATH, xpath_soup(drag_drop_div)).send_keys(img)
#Title
element = driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-title')]")
driver.execute_script("arguments[0].scrollIntoView()", element)
time.sleep(1)
driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-title')]").click()
time.sleep(1)
driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-title')]").send_keys(title)
#Description
element = driver.find_element(By.XPATH, "//div[contains(@id,'pin-draft-description')]")
driver.execute_script("arguments[0].scrollIntoView()", element)
time.sleep(1)
driver.find_element(By.XPATH, "//div[contains(@id,'pin-draft-description')]").click()
time.sleep(4)
inpt1 = ui.WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@id,'pin-draft-description')]")))
ActionChains(driver).move_to_element(inpt1).send_keys(description).perform()
#driver.find_element(By.XPATH, "//div[contains(@id,'pin-draft-description')]").send_keys(description)
#Link
element = driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-link')]")
driver.execute_script("arguments[0].scrollIntoView()", element)
time.sleep(1)
driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-link')]").click()
time.sleep(1)
driver.find_element(By.XPATH, "//textarea[contains(@id,'pin-draft-link')]").send_keys(link)
#Board select
select_board_dropdown = soup.find("button", attrs={"data-test-id": "board-dropdown-select-button"})
element = driver.find_element(By.XPATH, xpath_soup(select_board_dropdown))
driver.execute_script("arguments[0].scrollIntoView()", element)
time.sleep(1)
driver.find_element(By.XPATH, xpath_soup(select_board_dropdown)).click()
time.sleep(2)
driver.find_element(By.ID, "pickerSearchField").send_keys("Coupons")
time.sleep(2)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
first_item_in_list = soup.find("div", attrs={"data-test-id": "boardWithoutSection"})
driver.find_element(By.XPATH, xpath_soup(first_item_in_list)).click()
time.sleep(2)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
save_button = soup.find("button", attrs={"data-test-id": "board-dropdown-save-button"})
driver.find_element(By.XPATH, xpath_soup(save_button)).click()
time.sleep(5)
loading_image = None
#Post
while (True):
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
loading_image = soup.find("svg",attrs={"role":"img","aria-label":"Saving Pin..."})
if loading_image == None:
break
time.sleep(2)
time.sleep(2)
print("Done posting image no. => " + str(i + 1) + " out of " + str(len(images)))
if i == len(images) - 1:
print("================Finished================")
if i != len(images) - 1:
driver.get("https://www.pinterest.com/pin-builder/")
time.sleep(2)
# while (True):
# html = driver.page_source
# soup = BeautifulSoup(html, features="html.parser")
#
# create_story_btn = soup.find("div", attrs={"data-test-id": "storyboard-create-button"})
#
# if create_story_btn != None:
# break
# time.sleep(1)
#
# time.sleep(5)
#
# while (True):
# html = driver.page_source
# soup = BeautifulSoup(html, features="html.parser")
#
# story_edit = soup.find("div", attrs={"data-test-id": "storyboard-editor-details-media"})
#
# if story_edit != None:
# break
# time.sleep(1)
#
# time.sleep(5)
# html = driver.page_source
# soup = BeautifulSoup(html, features="html.parser")
#
# story_next = soup.find("div", attrs={"data-test-id": "storyboard-creation-nav-done"})
#
# driver.find_element(By.XPATH, xpath_soup(story_next)).click()
#
# time.sleep(5)
#
# html = driver.page_source
# soup = BeautifulSoup(html, features="html.parser")
#
# story_next = soup.find("div", attrs={"data-test-id": "storyboard-creation-nav-done"})
#
# # driver.find_element(By.ID, "storyboard-selector-title").send_keys(post_title)
# driver.find_element(By.XPATH, xpath_soup(story_next)).click()
#
# while (True):
# html = driver.page_source
# soup = BeautifulSoup(html, features="html.parser")
#
# pin_stat_div = soup.find("div", attrs={"data-test-id": "pin-stats-module"})
#
# if pin_stat_div != None:
# break
# time.sleep(1)
#
# time.sleep(2)
#
# print("Done posting image no. => " + str(i + 1) + " out of " + str(len(images)))
# if i == len(images) - 1:
# print("================Finished================")
#
# if i != len(images) - 1:
# driver.get("https://www.pinterest.com/idea-pin-builder/")
# time.sleep(10)
#
#