/
ATTScrape.js
144 lines (123 loc) · 3.88 KB
/
ATTScrape.js
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
const { writer } = require('./writer');
const { ppt } = require('./ppt');
const { msg } = require('./msg');
const puppeteer = require('puppeteer');
const { isArray, isObject } = require('lodash');
class ATTScrape {
/** @type {ppt} */
ppt;
async start() {
this.ppt = await new ppt();
await this.ppt.get();
return this;
}
async do() {
const states = await this.allStates();
for (const state of states) {
await this.getLocationsFromStates(state);
}
/** @type {{id: string, name: string, href: string, done: number}[]} */
const multiStores = await this.getAllMultiStores();
for (const multiStore of multiStores) {
await this.getLocationsMultiStores(multiStore, 'a.Teaser-titleLink');
}
}
/* MULTI-LOCATIONS */
async getAllMultiStores() {
const file = writer.read('multi-stores.json');
if (isObject(file) && Object.values(file).length > 0) {
const multiStores = Object.values(file).filter(s => s.done === 0);
if (multiStores.length > 0) {
msg.substep(`remaining multistores ${multiStores.length}`);
return multiStores;
}
}
}
/** @param {{id: string, name: string, href: string, done: number}} multiStore*/
async getLocationsMultiStores(multiStore, selector) {
msg.step(multiStore.name);
await this.ppt.goto(multiStore.href);
const pptLocations = await this.getPptList(selector);
if (pptLocations) {
const locations = await this.getListJson(pptLocations);
for (const l of Object.values(locations)) {
if (l.href.match(/\d+$/)) {
writer.set('stores.json', l.href.replace(/\W+/g, ''), { ...l });
} else {
writer.set('multi-misc-stores.json', l.href.replace(/\W+/g, ''), { ...l });
}
}
}
writer.set('multi-stores.json', multiStore.href.replace(/\W+/g, ''), { ...multiStore, done: 1 });
}
/* LOCATIONS */
/** @param {{id: string, name: string, href: string, done: number}} state*/
async getLocationsFromStates(state, selector) {
msg.step(state.name);
await this.ppt.goto(state.href);
const pptLocations = await this.getPptList(selector);
const locations = await this.getListJson(pptLocations);
for (const l of Object.values(locations)) {
if (l.href.match(/\d+$/)) {
writer.set('stores.json', l.href.replace(/\W+/g, ''), { ...l });
} else {
writer.set('multi-stores.json', l.href.replace(/\W+/g, ''), { ...l });
}
}
writer.set('states.json', state.name, { ...state, done: 1 });
}
/* STATES */
async allStates() {
const file = writer.read('states.json');
if (isObject(file) && Object.values(file).length > 0) {
const states = Object.values(file).filter(s => s.done === 0);
msg.substep(`existing remaining states ${states.length}`);
return states;
}
msg.step('openState');
await this.ppt.goto('https://www.att.com/stores/us');
const pptStates = await this.getPptList();
const states = await this.getListJson(pptStates);
writer.write('states.json', states);
return Object.values(file);
}
/* */
async getPptList(selector = 'a.Directory-listLink') {
try {
await this.ppt.waitForSelector(selector);
} catch (error) {
msg.error(error);
return null;
}
try {
let list = await this.ppt.page.$$(selector);
msg.substep('locations ' + list.length);
return list;
} catch (error) {
msg.die('Nothing found');
}
}
/**
* @param {puppeteer.ElementHandle<Element>[]} locations
* @return {[string:{ id: string; name: string; href: string; done: number; }]}
*/
async getListJson(locations) {
const ls = {};
for (const location of locations) {
const name = await (await location.getProperty('textContent')).jsonValue();
/** @type {String} */
const href = await (await location.getProperty('href')).jsonValue();
if (href.startsWith('https://www.att.com/stores') && !href.includes('#')) {
const l = {
name,
href,
id: href.replace(/\W+/g, ''),
done: 0,
};
ls[l.id] = l;
}
}
return ls;
}
}
module.exports = { ATTScrape };