-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
305 lines (217 loc) · 8.1 KB
/
scraper.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
#!/usr/bin/python3
import requests
#from bs4 import BeautifulSoup
import re
#from requests_html import HTMLSession
from pprint import pprint
from yattag import Doc
import json
from datetime import datetime as dt
from anytree import Node, NodeMixin, RenderTree
from anytree.search import findall_by_attr, findall, find
from anytree.exporter.dictexporter import DictExporter
from anytree.importer import DictImporter
class Item(Node):
def __init__(self):
self.items = dict()
self.saving = float()
self.price = float()
self.savingText = str()
self.udoCat = list()
self.image = str()
self.percentage = float()
self.min_purchase = int()
self.tag = str()
def sanitise(self):
self.name = self.items["title"].lower()
self.tag = "product"
self.saving = self.items["saving"]
self.price = self.items["price"]
self.savingText = self.items["savingText"]
if not self.savingText:
self.savingText = "Special bonus at price %s" % (self.price)
self.udoCat = self.items["udoCat"]
self.image = "https:" + self.items["image"]["srcset"][-1][0]
self.percentage, self.min_purchase = match_text(self.savingText)
if not self.savingText:
self.savingText = "Sale"
print(self.name)
print(self.percentage)
print(self.min_purchase)
print(self.saving)
print(self.price)
print(self.savingText)
def clean_title(self, string):
string.lower()
def match_text(text):
if match:= re.findall(r"([0-9]) for ([0-9])", text):
return int(match[0][1])/int(match[0][0]), 0
elif match:= re.findall(r"([0-9]+)% (on|per|ab) ([0-9])*.?", text):
return int(match[0][0])/100, int(match[0][2])
elif match:= re.findall(r"([0-9]+)% ([0-9]) or more", text):
return int(match[0][0])/100, int(match[0][1])
elif match:= re.findall(r"([0-9]+)%", text):
return int(match[0])/100, 0
else:
return 0.5, 0
def read_html_file():
with open("files/webpage.txt", encoding="utf-8") as f:
text = f.read()
return text
def strip(text):
pattern = r"<meta data-pagecontent-json='(.*?)'>"
match=re.findall(pattern, text)
json_data = json.loads(match[0])
anchors = json_data["anchors"]
return anchors
def populate_product(product):
obj = Item()
items = {"title": None,
"saving": None,
"price": None,
"savingText": None,
"udoCat": None,
"image": None}
for datapoint, value in product.items():
if datapoint in items.keys():
items[datapoint] = value
obj.items = items
obj.sanitise()
return obj
def make_title(title, promotion):
try:
return "{} ({})".format(title, promotion)
except:
return title
def build_tree(products):
### Import skeleton
if False:
tree = Item()
tree.name = "Coop"
tree.tag = "branch"
else:
importer = DictImporter()
with open("convert.json") as importf:
raw_tree = json.load(importf)
tree = importer.import_(raw_tree)
### Add current deals to tree
for product in products:
HEAD = tree
try:
node = populate_product(product)
if find(HEAD, lambda tmp: tmp.name == node.name):
continue
edges = node.udoCat
for edge in edges:
if (answer := findall_by_attr(HEAD, edge)):
HEAD = answer[0]
else:
new_node = Item()
new_node.name = edge
new_node.tag = "branch"
new_node.parent = HEAD
HEAD = new_node
# product_title = make_title(obj.title, obj.savingText)
node.parent = HEAD
except Exception as e:
print("We get the following error for product", e)
return None
### Print tree
for pre, fill, node in RenderTree(tree):
print("%s%s" % (pre, node.name))
### Export skeleton
exporter = DictExporter(childiter=lambda children: [child for child in children if child.tag=="branch"])
saved_tree = exporter.export(tree)
with open('convert.json', 'w') as convert_file:
convert_file.write(json.dumps(saved_tree))
return tree
def extract_keys(filename):
with open("files/{}".format(filename)) as f:
inpt = f.read()
exprs_pattern = r'"(.*?)"'
exprs = re.findall(exprs_pattern, inpt)
re.sub(r'"(.*?)"', "", inpt)
single_words = re.split(r"\t|\n| ", inpt)
keywords = single_words + exprs
keywords = [word for word in keywords if word not in ["", " "]]
return keywords
def check_ancestors(ancestors, baskets):
for ancestor in ancestors:
if ancestor.name in baskets:
return True
return False
def query_filter(tree):
## based on keywords
keywords = extract_keys("keywords.txt")
selected_objects = findall(tree, lambda node: any(keyword for keyword in keywords if keyword in node.name))
## based on high discounts
big_whales = findall(tree, lambda node: node.tag == "product" and node.percentage > 0.3 and node.min_purchase < 2)
## based on categories
basket_ids = extract_keys("baskets.txt")
baskets = findall(tree, lambda node: node.tag == "product" and check_ancestors(node.ancestors, basket_ids))
return selected_objects, big_whales, baskets
def display_products(objects, filename, title):
doc, tag, text = Doc().tagtext()
with tag('html'):
with tag('body'):
with tag('h1', id = 'main'):
text(title)
with tag('h3'):
for obj in objects:
with tag('p'):
text(obj.name)
with tag('p'):
text(obj.savingText)
try:
text("\nCHF ", obj.price)
except:
continue
try:
with tag('img', src=obj.image):
continue
except:
continue
with open('{}.html'.format(filename), 'w') as f:
f.write(doc.getvalue())
def display_tree(tree):
doc, tag, text = Doc().tagtext()
tree_struct = ""
for pre, fill, node in RenderTree(tree):
tree_struct += "%s%s\n" % (pre, node.name)
with tag('html'):
with tag('body'):
with tag('h1', id = 'main'):
text("All categories and current discounts")
with tag('span', style = "white-space: pre"):
text(tree_struct)
result = doc.getvalue()
with open('tree.html', 'w') as f:
f.write(doc.getvalue())
def main_loop():
#if is_monday():
# garbage_leaves()
offline = False
if offline:
lines = read_html_file()
content = strip(lines)
else:
url = "https://www.coop.ch/en/promotions/weekly-special-offers/c/m_1011"
session = requests.get(url)
content = strip(session.text)
product_content=list()
for category in content:
product_content += category["json"]["elements"]
tree = build_tree(product_content)
selected, whales, cats = query_filter(tree)
display_products(selected, filename="selection", title="Your wishlist")
display_products(whales, filename="whales", title="$$$ Big discounts $$$")
display_products(cats, filename="basket", title="Your baskets are offering...")
display_tree(tree)
if __name__=="__main__":
main_loop()
###########################################################
def is_monday():
return True if dt.weekday(dt.now()) == 0 else False
def garbage_leaves():
"""Go through all nodes and delete all product leaf nodes."""
##for every node, if tag is product, then prune