This repository has been archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
124 lines (103 loc) · 2.73 KB
/
app.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
/**
* Google geocoder latitude/longitude and name attributes for cities list (rocid.xml)
* */
var fs = require('fs')
, xml2js = require('xml2js')
, http = require('http')
, httpResponce = require('./googleGeoCodeCity.js')
, CityObject = require('./cityObject.js').init()
, parser = new xml2js.Parser();
var writer = fs.createWriteStream('city.json', {flags: 'w'});
var requestsPerSec = 1000 / 7;//free account requests limit
var lang = 'en';//localization data (ru/en/other...)
writer.on('finish', function () {
console.log('writer finish completed.');
});
function write(text) {
var success = writer.write(text);
if (!success) {
writer.once('drain', write);
}
}
fs.readFile(__dirname + '/rocid_capital.xml', {encoding: 'utf8'}, function (error, data) {
parser.parseString(data, function (error, result) {
var cityList = result.rocid.city;
var index = 0;
var lastIndex = cityList.length - 1;
var parseOptions = {
provider: 'GOOGLE',
host: 'maps.googleapis.com',
path: '/maps/api/geocode/json?'
};
/* START */
console.log('start...');
nextCity();
function nextCity() {
setTimeout(function () {
if (index === lastIndex) {
writer.end();
return;
}
parseCity(function (obj) {
if (obj.error) {
console.log('write error : ' + obj.error.error);
} else if (!obj.value) {
console.log('json city is null');
} else {
console.log(obj.value);
/* write to the file */
if (index === 0) {
write('[');
} else if (index < lastIndex - 1) {
write(obj.value + ',');
} else {
write(obj.value + ']');
}
}
nextCity();
++index;
});
}, requestsPerSec);
}
function getCity(index) {
return cityList[index];
}
function googleParse(cityName, callback) {
var googlePath = Object.create(parseOptions);
googlePath.path = parseOptions.path
+ 'language=' + lang
+ '&sensor=false'
+ '&address='
+ encodeURI(cityName);
httpResponce
.getPage(googlePath)
.html(function (bodyText) {
var jsonCity = null;
var cityObj = null;
cityObj = new CityObject(bodyText, {
provider: googlePath.provider
});
if (cityObj instanceof CityObject) {
jsonCity = cityObj.convertToJSON();//simple JSON
//jsonCity = cityObj.convertoToJSON_LD();//JSON_LD
} else {
return callback({error: true});
}
return callback({
error: null,
value: jsonCity
});
})
;
}
function parseCity(callback) {
var city = getCity(index);
var cityName = city.name[0].toLowerCase();
if (parseOptions.provider.toUpperCase() === 'GOOGLE') {
googleParse(cityName, callback);
} else {
throw 'ANOTHER PROVIDER WHILE IS NOT EXIST';
}
}
});
});