-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_swatches.py
39 lines (29 loc) · 1.19 KB
/
get_swatches.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
from bs4 import BeautifulSoup
import re
import json
import redis
import os
from urllib.parse import urlparse
url = urlparse(os.environ.get('REDISCLOUD_URL'))
r = redis.StrictRedis(host=url.hostname, port=url.port, password=url.password)
r.flushdb()
hex_color_regex = re.compile(r'(#\w+)')
get_hex_color = lambda s: hex_color_regex.search(s).group(1)
soup = BeautifulSoup(open('2014-10-04.html'))
i = 0
for i, swatch in enumerate(soup.find_all('div', {'class': 'swatch_content'})):
month, day, year = [date.get_text() for date in swatch.find_all('date')]
inspiration = swatch.find('div', {'class': 'swatch_info_right'}).find('span').get_text()
bg_right = get_hex_color(swatch['style'])
bg_left = get_hex_color(swatch.find('div', {'class': 'swatch_content_left'})['style'])
colors = [get_hex_color(color['style']) for color in
swatch.find_all('div', {'class': 'swatch'})]
swatch = {
'date': {'year': year, 'month': month, 'day': day},
'inspiration': inspiration,
'bg_right': bg_right,
'bg_left': bg_left,
'colors': colors
}
r.set("%s-%s-%s" % (year, month, day), json.dumps(swatch))
print("Found and stored %d swatches" % (i))