Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
parsing status message added
Browse files Browse the repository at this point in the history
  • Loading branch information
baskovsky committed Sep 18, 2014
1 parent 5256109 commit 0184611
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
76 changes: 50 additions & 26 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,66 @@
* 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()
, xml2js = require('xml2js')
, http = require('http')
, httpResponce = require('./googleGeoCodeCity.js')
, CityObject = require('./cityObject.js').init()
;

var parser = new xml2js.Parser();
var requestsPerSec = 5;//TODO for free account 10 requests per second! I have not recommend to set less more than 15
var requestsPerSec = 7;//TODO for free account 10 requests per second! I have not recommend to set less more than 15

var writer = fs.createWriteStream('city.json', {flags: 'w'});
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.xml', { encoding: 'utf8' }, function (err, data) {
parser.parseString(data, function (err, result) {
var cityList = result.rocid.city;
var index = cityList.length - 1;
var cityList = result.rocid.city
, index = 5//cityList.length - 1;

const parseOptions = {
provider: "GOOGLE"
, host: 'maps.googleapis.com'
, path: '/maps/api/geocode/json?'
provider: "GOOGLE", host: 'maps.googleapis.com', path: '/maps/api/geocode/json?'
};

//START
/* START */
nextCity();

function nextCity() {
setTimeout(function() {
if(index < 0) {
setTimeout(function () {
if (index < 0) {
writer.end();
return;
}

parseCity(function() {
parseCity(function (jsonCity) {

/* write to the file */
if (jsonCity !== null) {
if (index === cityList.length - 1) {
write('[');
} else if (index > 0) {
write(jsonCity + ',');
} else {
write(jsonCity + ']');
}
}

nextCity();
index--;
});
}, 1000 / requestsPerSec );
}, 1000 / requestsPerSec);
}

function getCity(index) {
return cityList[index];
}
Expand All @@ -52,33 +77,32 @@ fs.readFile(__dirname + '/rocid.xml', { encoding: 'utf8' }, function (err, data)
httpResponce
.getPage(googlePath)
.html(function (bodyText) {
var cityObj = new CityObject(bodyText, {
var jsonCity = null
, cityObj = new CityObject(bodyText, {
provider: googlePath.provider
});
if(cityObj !== null) {

if (cityObj !== null && cityObj.name && cityObj.geo && cityObj.geo.latitude && cityObj.geo.longitude) {
cityObj.setName(cityName.toUpperCase());//если не будет вызвана - добавит данные из гугла
// var jsonCity = cityObj.convertToJSON();//simple
var jsonCity = cityObj.convertoToJSON_LD();//progressive

//FIXME: записывать этот json в файл?
console.log(jsonCity);
jsonCity = cityObj.convertToJSON();//simple
// var jsonCity = cityObj.convertoToJSON_LD();//progressive
}

callback();
callback(jsonCity);
})
;
}

function parseCity(callback) {
var city = getCity(index);
var cityName = city.name[0].toLowerCase();
var city = getCity(index)
, cityName = city.name[0].toLowerCase();

//TODO выделить в отдельный класс с проверками на провайдер
if (parseOptions.provider.toUpperCase() === 'GOOGLE') {
console.log(city)
googleParse(cityName, callback);
} else {
throw 'ANOTHER PROVIDER WHILE IS NOT AVAILABLE';
throw 'ANOTHER PROVIDER WHILE IS NOT EXIST';
}
}
});
Expand Down
19 changes: 12 additions & 7 deletions cityObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module.exports.init = function () {
/* CONSTRUCTOR */
function CityObj(bodyText, convertType) {
this._googleGeoCodes = null;
// this._yandexGeoCodes = null;//example
// this._yandexGeoCodes = null;//example another provider

this.city = {
name: null,//Moscow
description: null,//"The Empire State Building is a 102-story landmark in New York City.",
image: null,//"http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg",
//TODO включить в будущем
//description: null,//"The Empire State Building is a 102-story landmark in New York City.",
//image: null,//"http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg",
//TODOEND
geo: {
latitude: null,//"40.75",
longitude: null//"73.98"
Expand All @@ -20,12 +22,12 @@ function CityObj(bodyText, convertType) {
if (!convertType) throw "Argument 'convertType' is not found!";

if (convertType.provider && convertType.provider.toUpperCase() === 'GOOGLE') {

//TODO сделать более умную выборку
var geoObj = this.convertFromGoogleGeoCode(bodyText);

if(geoObj === null) {
return null;
} else {
//TODO сделать более умную выборку
geoObj
.setGeoLatitude(this._googleGeoCodes.results[0].geometry.location.lat)
.setGeoLongitude(this._googleGeoCodes.results[0].geometry.location.lng)
Expand All @@ -48,7 +50,8 @@ CityObj.prototype.convertFromGoogleGeoCode = function (bodyText) {
case 'status' :
if (value !== 'OK') {
error = true;
console.warn('Response status error');
console.warn('Response status error ' + value);
return null;
}

break;
Expand Down Expand Up @@ -129,6 +132,7 @@ CityObj.prototype.setGeoLongitude = function (longitude) {
return this;
};

//TODO включить в будущем
CityObj.prototype.setDescription = function(description) {
this.city.description = description;

Expand All @@ -139,4 +143,5 @@ CityObj.prototype.setImage = function(imageUrl) {
this.city.image = imageUrl;

return this;
};
};
//TODOEND

0 comments on commit 0184611

Please sign in to comment.