-
Notifications
You must be signed in to change notification settings - Fork 0
/
history-fetch-all.js
80 lines (71 loc) · 2.71 KB
/
history-fetch-all.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
import * as dotenv from 'dotenv';
import fs from 'fs';
import { createHistoryModule, sipgateIO } from 'sipgateio';
import {
createFeatureJSON,
createHeatJSON,
forecast,
isCountryNumber,
parseAreaCodeCSV
} from './utils.js';
dotenv.config();
const personalAccessTokenId = process.env.SIPGATE_TOKEN_ID || '';
const personalAccessToken = process.env.SIPGATE_TOKEN || '';
/**
* For details on how to instantiate the client, see 'examples/client/client.ts'
*/
let areaCodeMap = parseAreaCodeCSV('./data/area_codes.csv');
const client = sipgateIO({
tokenId: personalAccessTokenId,
token: personalAccessToken,
});
const historyModule = createHistoryModule(client);
export function fetchAll() {
historyModule
.fetchAll(
{ types: ['SMS', 'FAX', 'CALL'], directions: ['INCOMING'] },
{ offset: 0, limit: 100 }
)
.then((historyElements) => {
let maxOccurrence = 0;
historyElements.forEach((entry) => {
let number = entry.source;
if (!isCountryNumber('+49', number)) {
return;
}
number = '0' + number.substring(3);
for (let i = 5; i > 1; i--) {
const areaCode = number.substr(0, i);
if (areaCodeMap[areaCode]) {
areaCodeMap[areaCode].occurrences += 1;
if (areaCodeMap[areaCode].occurrences > maxOccurrence) {
maxOccurrence = areaCodeMap[areaCode].occurrences;
}
break;
}
}
});
let featureDataJSON = createFeatureJSON(areaCodeMap, maxOccurrence);
fs.writeFile('./map/features.geo.json', featureDataJSON, (err) => {
if (err) return console.log(err);
console.log('Created ./map/features.geo.json');
});
let heatDataJSON = createHeatJSON(areaCodeMap, maxOccurrence);
fs.writeFile('./map/heat.json', heatDataJSON, (err) => {
if (err) return console.log(err);
console.log('Created ./map/heat.json');
});
let forecastData = forecast(areaCodeMap, 10);
// console.log('Forecast:\n', forecastData);
let topFeatureDataJSON = createFeatureJSON(forecastData);
fs.writeFile(
'./map/features.top.geo.json',
topFeatureDataJSON,
(err) => {
if (err) return console.log(err);
console.log('Created ./map/featues.top.geo.json');
}
);
})
.catch(console.error);
}