-
Notifications
You must be signed in to change notification settings - Fork 3
/
clone_redbubble.py
375 lines (293 loc) · 13.4 KB
/
clone_redbubble.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import io
import json
import time
import requests
from PIL import Image, ImageDraw, ImageFont
import random
import selenium
import shutil
from lxml import html
from lxml.html import clean
from seleniumrequests import Firefox
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException, \
ElementNotInteractableException, ElementClickInterceptedException
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import Select
from pathlib import Path
from urllib.parse import urljoin, urlparse
USERNAME = ""
PASSWORD = ""
DOWNLOAD_DIR = Path('./') / "redbubble"
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
class RedBubble:
def __init__(self):
self.options = Options()
self.options.set_preference("intl.accept_languages", 'en-us')
self.options.set_preference('useAutomationExtension', False)
self.options.set_preference('dom.webdriver.enabled', False)
self.driver = Firefox(options=self.options)
self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
self.driver.get("https://amiunique.org/fp")
time.sleep(5)
self.driver.get("https://antcpt.com/score_detector/")
time.sleep(5)
def get_to(self, url):
if self.driver.current_url != url:
self.driver.get(url)
def download_image(self, file_name, image_url):
if "x1000" in image_url:
big_image_url = image_url.replace("1000x1000", "2000x2000")
big_image_url = big_image_url.replace("750x1000", "1500x2000")
big_image_url = big_image_url.replace("1000", "2000")
big_image_url = big_image_url.replace("750", "1500")
if self.download_image(file_name, big_image_url):
print(f"Gotten bigger image for {file_name} @ {big_image_url}")
return True
try:
res = self.driver.request('GET', image_url)
except requests.exceptions.ConnectionError:
time.sleep(60)
res = self.driver.request('GET', image_url)
if 200 <= res.status_code < 300:
im = Image.open(io.BytesIO(res.content))
width, height = im.size
# Add watermark
if max(width, height) >= 2000:
font = ImageFont.truetype('Arial.ttf', 72)
else:
font = ImageFont.truetype('Arial.ttf', 36)
draw = ImageDraw.Draw(im)
text = "DuckHunt.me"
fill_color = (128, 0, 0)
textwidth, textheight = draw.textsize(text, font)
margin = 30
x = width - textwidth - margin
y = height - textheight - margin
try:
draw.text((x, y), text, font=font, fill=fill_color)
except TypeError:
draw.text((x, y), text, font=font, fill=128)
# Write to file
im.save(file_name)
return True
else:
print(f"Download of {file_name} (@{image_url}) FAILED.")
return False
def clean_html(self, html_string):
tree = html.fromstring(html_string)
cleaner = html.clean.Cleaner()
cleaner.safe_attrs_only = True
cleaner.safe_attrs = frozenset(['id'])
cleaned = cleaner.clean_html(tree)
return html.tostring(cleaned, encoding='unicode')
def dismiss_cookie_popup(self):
self.dismiss_member_prompt()
try:
self.driver.find_element_by_class_name("Toastify__toast-body").find_element_by_tag_name("button").click()
return True
except (NoSuchElementException, ElementClickInterceptedException):
return False
def dismiss_member_prompt(self):
try:
self.driver.find_element_by_class_name("sailthru-overlay-close").click()
return True
except NoSuchElementException:
return False
def login(self):
self.driver.get("https://www.redbubble.com/en/auth/login")
username = self.driver.find_element_by_xpath('//*[@id="ReduxFormInput1"]')
username.send_keys(USERNAME)
password = self.driver.find_element_by_xpath('//*[@id="ReduxFormInput2"]')
password.send_keys(PASSWORD)
connect = self.driver.find_element_by_xpath('/html/body/div[1]/div[7]/div[2]/div[2]/div/form/span/button')
prev_url = self.driver.current_url
connect.click()
time.sleep(5)
while prev_url == self.driver.current_url:
print("Please solve captcha")
time.sleep(1)
def change_locale(self):
self.get_to("https://www.redbubble.com/settings/show")
locale_dropdown = Select(self.driver.find_element_by_xpath('//*[@id="settings_locale"]'))
locale_dropdown.select_by_visible_text("English")
country_code_dropdown = Select(self.driver.find_element_by_xpath('//*[@id="settings_country_code"]'))
country_code_dropdown.select_by_value("US")
currency_dropdown = Select(self.driver.find_element_by_xpath('//*[@id="settings_currency_iso"]'))
currency_dropdown.select_by_value("USD")
try:
button = self.driver.find_element_by_xpath('/html/body/div/form/div[4]/input')
except NoSuchElementException:
button = self.driver.find_element_by_xpath('/html/body/div[1]/div[6]/div[2]/form/div[4]/button')
button.click()
def get_works_urls(self):
self.get_to("https://www.redbubble.com/en/portfolio/manage_works")
self.dismiss_cookie_popup()
works = []
links = self.driver.find_elements_by_class_name('works_work-menu-option')
for a in links:
if a.tag_name != 'a':
continue
href = a.get_attribute('href')
if href.startswith("https://www.redbubble.com/people/duckhuntdiscord/works"):
works.append(href)
random.shuffle(works)
return works
def get_work_products_urls(self, work_url):
self.get_to(work_url)
products_urls = []
links = self.driver.find_elements_by_class_name('carousel_item-link')
for a in links:
if a.tag_name != 'a':
continue
href = a.get_attribute('href')
if href.startswith("https://www.redbubble.com/i/"):
products_urls.append(href)
random.shuffle(products_urls)
return products_urls
def get_work_info(self, work_url):
self.get_to(work_url)
return {
"name": self.driver.find_element_by_class_name('work-information_title').text,
"url": urljoin(work_url, urlparse(work_url).path)
}
def _get_colorswatch(self):
try:
colors_button = self.driver.find_element_by_css_selector("[class^='ColorPickerActivator__colorPickerActivator--']")
except NoSuchElementException:
colors_button = None
if colors_button:
colors_button.click()
try:
colors_swatch = self.driver.find_elements_by_css_selector("[class^='DesktopColorControls__swatch--']")
except NoSuchElementException:
colors_swatch = [None]
if len(colors_swatch) == 0:
colors_swatch = [None]
return colors_swatch
def _get_print_locations(self):
print_locations = self.driver.find_elements_by_name("printLocation")
if not print_locations:
print_locations = [None]
return print_locations
def _get_sizes(self):
sizes = self.driver.find_elements_by_name("size")
if not sizes:
sizes = [None]
return sizes
def download_product_information(self, product_url, work_info):
self.get_to(product_url)
self.dismiss_cookie_popup()
varients = []
product_name = self.driver.find_element_by_tag_name('h1').text
print(f"Downloading {product_name}...")
download_to = DOWNLOAD_DIR / work_info["name"] / product_name
download_to.mkdir(exist_ok=True, parents=True)
if (download_to / "download.json").exists():
print("Skipping : already downloaded")
return
colors = len(self._get_colorswatch())
print(f"Found {colors} colors_swatch.")
for color_n in range(colors):
color_element = self._get_colorswatch()[color_n]
if color_element is None:
color_product_name = None
else:
color_product_name = color_element.get_attribute('title')
color_element.click()
self.dismiss_cookie_popup()
print_locations_count = len(self._get_print_locations())
print(f"Found {print_locations_count} print locations.")
for print_location_n in range(print_locations_count):
print_location_element = self._get_print_locations()[print_location_n]
if print_location_element is None:
print_location_text = None
else:
label = print_location_element.find_element_by_xpath('..')
print_location_text = label.text
label.click()
self.dismiss_cookie_popup()
sizes = self._get_sizes()
sizes_names = []
for size_element in sizes:
if size_element is None:
continue
else:
label = size_element.find_element_by_xpath('..')
size_name = label.text
sizes_names.append(size_name)
time.sleep(1) # Time for images to load
images = self.driver.find_elements_by_tag_name('img')
images_downloaded = 0
images_files = []
for img in images:
src = img.get_attribute('src')
klass = img.get_attribute('class')
if src.startswith("https://ih1.redbubble.net/image") and "GalleryImage__img--" in klass:
main = "PreviewGallery__rightColumn--" in img.find_element_by_xpath('..').find_element_by_xpath('..').find_element_by_xpath('..').get_attribute('class')
img_folder = download_to / f"{color_product_name} - {print_location_text}"
img_folder.mkdir(parents=True, exist_ok=True)
image_name = img_folder / f"{images_downloaded}.jpg"
dld = self.download_image(file_name=image_name, image_url=src)
if dld:
images_downloaded += 1
images_files.append({"link": str(image_name), "main": main})
price = ''
config_div = self.driver.find_element_by_tag_name('h1').find_element_by_xpath('..')
prices_maybe = config_div.find_elements_by_tag_name('span')
for price_maybe in prices_maybe:
try:
price = price_maybe.find_element_by_tag_name('span').text
break
except NoSuchElementException:
continue
varient = {
"color_product_name": color_product_name,
"print_location_text": print_location_text,
"sizes_names": sizes_names,
"images_count": images_downloaded,
"images": images_files,
"price": price,
"url": self.driver.current_url,
}
print(varient)
varients.append(varient)
try:
features = self.driver.find_element_by_xpath("//*[contains(text(), 'Features')]")\
.find_element_by_xpath('..')\
.find_element_by_tag_name('ul')\
.get_attribute('outerHTML')
except NoSuchElementException:
features = self.driver.find_element_by_xpath("//*[contains(text(), 'Features')]") \
.find_element_by_xpath('..') \
.find_element_by_xpath('..') \
.find_element_by_tag_name('ul') \
.get_attribute('outerHTML')
with open(download_to / "download.json", "w") as f:
json.dump({
"varients": varients,
"features_html": self.clean_html(features),
"work": work_info,
}, f, sort_keys=True)
def main(self):
self.login()
self.change_locale()
works_urls = self.get_works_urls()
print(f"Discovered {len(works_urls)} works")
for work_url in works_urls:
work_info = self.get_work_info(work_url)
# if (DOWNLOAD_DIR / work_info["name"]).exists():
# print(f"Skipping {work_info['name']} because it was already downloaded.")
# continue
product_urls = self.get_work_products_urls(work_url)
for product_url in product_urls:
ok = False
while not ok:
try:
self.download_product_information(product_url, work_info)
ok = True
except (StaleElementReferenceException, ElementNotInteractableException, NoSuchElementException) as e:
print(f"That failed {e}, retrying...")
self.driver.quit()
rb = RedBubble()
rb.main()