-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
299 lines (257 loc) · 8.08 KB
/
index.ts
File metadata and controls
299 lines (257 loc) · 8.08 KB
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
import { success } from "signale";
import fs, { WriteStream } from "fs";
import Crawler from "crawler";
import cTable from "console.table";
import cheerio from "cheerio";
import { StatisticsManager } from "./statistics";
export type CategoryListType = {
label: string;
tag: string;
};
type ParsedRow = {
pid: string;
name: string;
cost: string;
quantity: string;
category: string;
url: string;
};
type ParsedCategory = {
[id: string]: Array<ParsedRow>;
};
class TescoScrapper {
private writer: WriteStream;
private categories: Array<CategoryListType>;
private pageLimit: number;
private baseURL: string;
private batchLength: number;
private stats: StatisticsManager;
private crawler: Crawler;
private currentRunningCategory: number = 0;
private parsedList: ParsedCategory = {};
constructor(
cats: Array<CategoryListType>,
baseURL: string,
pageLimit: number = 10,
batchLength: number = 1
) {
this.categories = cats;
this.pageLimit = pageLimit;
this.baseURL = baseURL;
this.batchLength = batchLength;
this.initStats();
this.initCrawler();
}
private initCrawler() {
this.crawler = new Crawler({
maxConnections: this.batchLength,
retries: 10
});
}
// https://www.npmjs.com/package/statware
private initStats() {
this.stats = new StatisticsManager();
}
private getFormattedTime(): string {
var today = new Date();
var y = today.getFullYear();
// JavaScript months are 0-based.
var m = today.getMonth() + 1;
var d = today.getDate();
return y + "-" + m + "-" + d;
}
private getFileStream(suffix: string): WriteStream {
const FOLDER_NAME = `data`;
const SUBFOLDER_NAME = `${this.getFormattedTime()}`;
const FILE_NAME = `./${FOLDER_NAME}/${SUBFOLDER_NAME}/${suffix
.trim()
.replace(" ", "")
.toLowerCase()}.csv`;
let toRet;
// check if folder already exists?
if (!fs.existsSync(FOLDER_NAME)) {
// Make directory
fs.mkdirSync(FOLDER_NAME);
}
if (!fs.existsSync(FOLDER_NAME + "/" + SUBFOLDER_NAME)) {
// Make directory
fs.mkdirSync(FOLDER_NAME + "/" + SUBFOLDER_NAME);
}
return fs.createWriteStream(FILE_NAME);
}
private printToDoc(data: ParsedCategory) {
Object.keys(data).forEach(categoryKey => {
let writer = this.getFileStream(categoryKey);
writer.write(`Product Id; Name; Cost; Quantity; Url; Category\n`); // Start categories items to queue
let catData = data[categoryKey].forEach(row => {
writer.write(
`${row.pid}; ${row.name}; ${row.cost}; ${row.quantity}; ${row.url}; ${row.category}\n`
);
});
});
}
private nextPage(category: CategoryListType, pageNumber) {
// Generate URL
let url = `${TESCO_BASE_URL}/groceries/shop/${category.tag}/all?page=${pageNumber}&count=${this.pageLimit}`;
// Closure to add to queue
let pushToQueue = newRl => {
this.crawler.queue({
uri: newRl,
category: category,
page: pageNumber,
url: url,
callback: this.parse.bind(this)
});
};
// Add initial url
pushToQueue(url);
}
logStatus(category: CategoryListType, newStatus) {
this.stats.addStatusUpdate(category.label, newStatus);
}
addPageData(category: CategoryListType, newItems: Array<ParsedRow>) {
this.stats.incrementCount(category.label, newItems.length);
this.stats.incrementPage(category.label);
if (!this.parsedList[category.label]) {
this.parsedList[category.label] = [...newItems];
} else {
this.parsedList[category.label] = this.parsedList[category.label].concat(
...newItems
);
}
this.rePrintLog();
}
logError(error) {
this.stats.addError(error);
this.rePrintLog();
}
rePrintLog() {
console.clear();
let table: any = this.stats.print();
table = Object.values(table);
console.table(cTable.getTable(table));
let errors = this.stats.printErrors();
console.log(errors);
}
private async parse(error, res, done) {
let newItems: Array<ParsedRow> = [];
// Handle Errors
if (error) {
this.logError(`${res.options.category.label} - Parsing failed`);
done();
return;
}
// Use this to check when future rounds are marked as done.
var markStopped = false;
// init and crap
var $ = await cheerio.load(res.body);
const listItem = await $(".product-list--list-item"); // If No items exist
// if no objects are found
if (listItem.length <= 0) {
// If on first page and no data then show error flag
if (res.options.page == 1) {
this.logError(`${res.options.category.label} - No data found`);
this.logStatus(res.options.category, "🔴 - ERROR");
} else {
// else show stoppped flag
this.logStatus(res.options.category, "✅ - Completed");
}
done();
return;
}
// Add next page to queue if size is more than or equal to 100
if (listItem.length >= PAGE_LIMIT) {
this.nextPage(res.options.category, ++res.options.page);
} else {
// If page size is more than 0 but less than #PAGE_LIMIT then mark this as last round of this category
markStopped = true;
}
// For each list item...
listItem.each((i, element) => {
var elm = $(element);
const prodNameWithPid = elm.find(
".product-tile--title.product-tile--browsable"
);
const prodCost = elm
.find("div.price-details--wrapper > div.price-control-wrapper")
.text()
.trim();
const prodQuantity = elm
.find("div.price-details--wrapper > div.price-per-quantity-weight")
.text()
.trim();
let regexForPid = /[^\/groceries\/en\-GB\/products\/].*/;
let itemPid = regexForPid.exec(prodNameWithPid.attr()["href"] || "")[0];
newItems.push({
pid: itemPid,
name: prodNameWithPid.text().trim(),
cost: prodCost,
quantity: prodQuantity,
category: res.options.category.label,
url: `${this.baseURL}${prodNameWithPid.attr()["href"] || ""}`
});
});
this.logStatus(
res.options.category,
markStopped ? "✅ - Completed" : "🌕 - Running"
);
this.addPageData(res.options.category, newItems);
done();
}
public run() {
this.categories.map((cat, index) => {
this.stats.addCategory(index, cat);
});
this.getBatch().forEach(category => {
this.nextPage(this.categories[this.currentRunningCategory], 1);
this.currentRunningCategory++;
});
let self = this;
// this.crawler.on("request", options => {
// console.log("currentRunningCategory", this.currentRunningCategory);
// });
this.crawler.on("drain", () => {
if (this.currentRunningCategory < this.categories.length - 1) {
// this.nextPage(this.categories[++this.currentRunningCategory], 1);
this.getBatch().forEach(category => {
this.nextPage(this.categories[this.currentRunningCategory], 1);
this.currentRunningCategory++;
});
} else {
this.printToDoc(this.parsedList);
success("all done!");
}
});
}
getBatch(): Array<CategoryListType> {
let toRet = [];
for (let index = 0; index < this.batchLength; index++) {
let cat = this.categories[this.currentRunningCategory + index];
if (cat) toRet.push(cat);
}
return toRet;
}
}
const tescoCategories: Array<CategoryListType> = [
{ label: "Fresh Food", tag: "fresh-food" },
{ label: "Grocery", tag: "grocery" },
{ label: "Baby", tag: "baby" },
{ label: "Chilled & Frozen", tag: "chilled-and-frozen" },
{ label: "Drinks", tag: "drinks" },
{ label: "Health & Beauty", tag: "health-and-beauty" },
{ label: "Household", tag: "household" },
{ label: "Pets", tag: "pets" },
{ label: "Non-Food & Gifting", tag: "non-food-and-gifting" }
];
// cannot change in tesco
const PAGE_LIMIT = 50;
// groceries/products/7070980683
// groceries/shop/fresh-food/all
const TESCO_BASE_URL = "https://eshop.tesco.com.my";
const BATCH_LENGTH = 50;
new TescoScrapper(
tescoCategories,
TESCO_BASE_URL,
PAGE_LIMIT,
BATCH_LENGTH
).run();