-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttAddressFromStores.js
67 lines (55 loc) · 1.76 KB
/
AttAddressFromStores.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
const { writer } = require('./writer');
const { msg } = require('./msg');
const { isObject, indexOf } = require('lodash');
const { fetch } = require('./fetch');
class AttAddressFromStores {
async do() {
const stores = await this.allStores();
for (const store of stores) {
msg.error(stores.length - indexOf(stores, store));
await this.getAddressFromStore(store);
}
}
/** @param {{id: string, name: string, href: string, done: number}} store*/
async getAddressFromStore(store) {
msg.step(store.href);
const html = String(fetch(store.href));
const startphone = html.indexOf('"Phone-link" href="tel:');
let phone = html.substr(startphone, 35).slice(-12);
const startadd = html.indexOf('{"dimension1":');
const endadd = html.indexOf('"pageview"}');
let add = html.substr(startadd, endadd - startadd + 11);
try {
add = JSON.parse(add);
} catch (error) {
msg.die('can parse address');
}
const start = html.indexOf('<meta itemprop="latitude" content="');
const str = html.substr(start, 200).split(/[\s+"]/);
store['latitude'] = str[5];
store['longitude'] = str[11];
store['cid'] = str[14];
store['phone'] = phone;
store['address'] = [
add.dimension4,
add.dimension3,
add.dimension2,
add.dimension5,
add.dimension6,
].join(', ');
if (!String(store['cid']).includes('https://')) {
msg.die(str);
}
writer.set('stores.json', store.href.replace(/\W+/g, ''), { ...store, done: 1 });
}
async allStores() {
const file = writer.read('stores.json');
if (isObject(file) && Object.values(file).length > 0) {
const stores = Object.values(file).filter(s => s.done === 0);
msg.substep(`existing remaining stores ${stores.length}`);
return stores;
}
return [];
}
}
module.exports = { AttAddressFromStores };