-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-sitemapgen.js
183 lines (135 loc) · 4.49 KB
/
app-sitemapgen.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//generate sitemap.xml from active DB & copy it to server
var config = require('./config'),
Q = require('q'),
mysql = require('mysql'),
moment = require('moment'),
zlib = require('zlib'),
mkdirp = require('mkdirp'),
fs = require('fs'),
_ = require('underscore');
//dev modules
var inspect = require('eyes').inspector();
// init
var dbConProd1 = mysql.createConnection(config.mysqlConn_1),
domains = require('./config/domains.json').production,
domainsQ = [];
domains = _.omit(domains, ['default']);
//Promises supported xml generator
var sm = {
generateXml: function(options) {
var deferred = Q.defer(),
url_list = options.data;
options = _.defaults({ //defaults
rootPath: 'http://' + options.domain + '/',
priority: 0.5,
freq: 'monthly',
lastmod: moment().format("YYYY-MM-DD")
}, options);
//XML template
var xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
_.each(url_list, function(element, index, list) {
xml += '<url>';
xml += '<loc>' + options.rootPath + element.url + '</loc>';
xml += '<changefreq>' + (element.freq || options.freq) + '</changefreq>';
xml += '<priority>' + (element.priority || options.priority) + '</priority>';
xml += '<lastmod>' + (element.lastmod || options.lastmod) + '</lastmod>';
xml += '</url>';
});
xml += '</urlset>';
options.data = xml;
options.filename = options.domain + '-sitemap.xml';
deferred.resolve(options);
return deferred.promise;
},
gzip: function(options) {
var deferred = Q.defer(),
buffer = new Buffer(options.data, 'utf8');
zlib.gzip(buffer, function(err, data) {
if (err) {
deferred.reject(err);
}
options.data = data;
deferred.resolve(options);
});
options.filename = options.domain + '-sitemap.xml.gz';
return deferred.promise;
},
saveToFile: function(options) {
var deferred = Q.defer(),
path = options.dist + '/' + options.filename;
mkdirp(options.dist, function(err) {
if (err) return console.error(err);
fs.writeFile(path, options.data, function(err) {
if (err) {
if (err.code === 'ENOENT') {
err = ('No such file or directory ' + path + '!');
}
deferred.reject(err);
} else {
var cprs = '';
console.info("The file: " + options.filename + " was saved!");
deferred.resolve(options);
}
});
});
return deferred.promise;
},
printErr: function(err) {
var nl = 100;
var err_txt = err.substr(0, nl);
console.error(err_txt);
}
};
// **************
// APP
// **************
// get urls from DB
var getUrls = function(options) {
if (!options.lang) {
return;
}
var deferred = Q.defer();
var queryStr = 'SELECT url, ' +
'CASE ' +
' WHEN path LIKE "accommodation.%" THEN 0.8 ' +
' WHEN path LIKE "resort.%" THEN 0.9 ' +
' WHEN path LIKE "resortfinder.%" THEN 0.7 ' +
' else 0.5 ' +
'END as priority ' +
'FROM routing ' +
'WHERE lang = ? ' +
'AND active = 1 ' +
'AND redirect = 0 ' +
'ORDER BY url ';
dbConProd1.query(queryStr, [options.lang], function(err, rows, fields) {
if (err) deferred.reject(err);
options.data = rows;
options.fields = fields;
options.dist = config.baseSitemapsExportPath;
deferred.resolve(options);
});
return deferred.promise;
};
_.each(domains, function(attrs, domain) {
attrs.domain = domain;
var domainQ = getUrls(attrs)
.then(sm.generateXml)
.then(sm.gzip)
.then(sm.saveToFile);
domainsQ.push(domainQ);
});
// Q.allSettled(domainsQ)
Q.all(domainsQ)
.then(function(results, err) {
if (err) {
console.error(err);
process.exit(code = 1);
} else {
console.info('All sitemaps build-ed successfully!');
process.exit(code = 0);
}
})
.fail(function(err) {
console.error(err);
process.exit(code = 1);
});