UNPKG

17 kBJavaScriptView Raw
1/* Libraries */
2
3/*JUST TEST PURPOSE*/
4
5var fetch = require('node-fetch');
6var moment = require('moment');
7
8//Hotelpartner
9const _HP = require("./classHotels.js");
10
11const keys=[];
12const nodes = ["NAME","STARS","ADRESSE"];
13var preventdouble =[]
14var sameItems =[]
15var notsameitems =[]
16var arrayofelements =[]
17
18
19function startBooking(array,city) {
20 return new Promise(resolve=>{
21 let i =0;
22 array.forEach(t=>{
23 t.source = 'Booking.com'
24 t.id = i
25 i++;
26 })
27
28 arrayofelements =array.slice();
29
30 console.log('Done Booking.com fetching found: ' + arrayofelements.length)
31 fetchDBData(city).then((r)=>{
32 let obj={
33 same:sameItems,
34 notsame:notsameitems
35 }
36
37 resolve(obj)
38 })
39 })
40}
41
42function init() {
43return new Promise(resolve=>{
44
45 if(keys.length>0)
46 {
47 foundIndex=[];
48 sameItems = [];
49
50 for(let skip=0;skip<arrayofelements.length;skip++){
51 keys.forEach(treeitem=>{
52
53 if(treeitem.index == 0)
54 compareHotel(treeitem,skip,arrayofelements)
55
56 })
57 }
58 }
59 else
60 {
61 console.error("no Array")
62 }
63
64 console.log("");
65 console.log("Total same found: " +sameItems.length);
66 findNotSameHotels().then(res=>{
67 resolve(res)
68 });
69})
70}
71
72function generateTree(obj) {
73 return new Promise(resolve=>{
74 let i = 0, key;
75 for (key in obj)
76 {
77 nodes.forEach(matchNode=>{
78 if(key.toUpperCase().indexOf(matchNode)>=0){
79
80 firstnode = {
81 index:i,
82 match: `${matchNode}`,
83 "key":key
84 }
85 keys.push(firstnode)
86 }
87 })
88 i++;
89 }
90 resolve(keys)
91 })
92
93}
94
95function compareHotel(item,skipitem,array) {
96
97 let i = skipitem; //prevent compare item it self
98 const word = parser(array[i][item.key]); //hotelname
99
100 item.points = 0; //reset
101 let currentitem = 0; //starts at 0
102
103 array.forEach(inarray =>{
104
105 //Skip Current item
106 if(skipitem==currentitem || inarray.source == arrayofelements[i].source|| inarray.id == arrayofelements[i].id ){
107 currentitem++;
108 return;
109 }
110
111 if(preventdouble.indexOf(`${inarray.source}${inarray.id}${arrayofelements[i].source}${arrayofelements[i].id}`)>=0){
112 currentitem++;
113 return;
114 }
115
116
117 //Search for same values in db || set to 1 if values are not form db
118 const geo = 1;//compareGeo(inarray,array[i])
119 const email = 1;//compareEmail(inarray,array[i])
120 const street = 1;//compareStreet(inarray,array[i])
121
122
123 //Name 1:1 is equal
124 if ( parser(inarray[item.key]) == word || word.indexOf(parser(inarray[item.key]).substr(2,inarray[item.key].length-2))>=0){
125 /*
126 console.log(" ")
127 console.log("Found by fullname 0")
128 console.log(inarray[item.key] +" || " +word)
129 console.log(`where id =${inarray.id } or id = ${arrayofelements[i].id}`)
130 console.log(`source =${inarray.source } or source = ${arrayofelements[i].source}`)
131 */
132
133 let equals = {
134 0:inarray,
135 1:arrayofelements[i],
136 id:`${inarray.id};${arrayofelements[i].id}`
137 };
138 inarray.founddb = true;
139 arrayofelements[i].founddb = true
140
141 preventdouble.push(`${arrayofelements[i].source}${arrayofelements[i].id}${inarray.source}${inarray.id}`)
142 sameItems.push(equals)
143
144 currentitem++;
145 return
146
147 }
148
149
150 //exclude short names
151 if(exclude(inarray[item.key]) == null || exclude(word) == null )
152 { currentitem++; return; }
153
154 //split by space
155 const compare0 = word.split(" ");
156 const compare1 = parser(inarray[item.key]).split(" ");
157
158 //word length must be longer than 3
159 try{
160
161 if(compare0[0].length <= 3 || compare0[1].length <=3 || compare1[0].length<=3|| compare1[1].length<=3)
162 {
163 currentitem++;
164 return;
165 }
166 }
167 catch (TypeError)
168 {
169 currentitem++;
170 return;
171 }
172
173 /// Check if the shorter name contains in longer name
174 let machedItems =0;
175 let passedmachedItems = false;
176 const notmacheditems = {
177 "compare0": [],
178 "compare1": [],
179 }
180 compare0.forEach(item0=>{
181 compare1.forEach(item1=>{
182 if(item0.length >= item1.length)
183 {
184
185 if(item0.indexOf(item1)>=0){
186 machedItems++;
187 return;
188 }
189 else {
190 if(notmacheditems.compare1.indexOf(item1)==-1 && notmacheditems.compare0.indexOf(item0)==-1)
191 {
192 notmacheditems.compare0.push(item0)
193 notmacheditems.compare1.push(item1)
194 }
195 }
196 }
197 else
198 {
199 if(item1.indexOf(item0)>=0 && item1.length>3){
200 machedItems++
201 return;
202 }
203 else
204 {
205 if(notmacheditems.compare1.indexOf(item1)==-1 && notmacheditems.compare0.indexOf(item0)==-1 && item1.length>3 )
206 {
207 notmacheditems.compare0.push(item0)
208 notmacheditems.compare1.push(item1)
209 }
210 }
211 }
212 })
213 })
214
215 /*Check if still needed*/
216 if(compare0.length == machedItems )
217 {
218 passedmachedItems=true;
219 }
220
221 //Sort and compare Char
222 compare0.sort();
223 compare1.sort();
224 notmacheditems.compare0.sort();
225 notmacheditems.compare1.sort();
226
227 let charpoints = 0;
228 let minpoints = 0
229 notmacheditems.compare0.forEach(item0=>{
230 notmacheditems.compare1.forEach(item1=>{
231
232
233 if(item0.length<=3)return;
234 if(item1.length<=3)return;
235
236 if(item0.indexOf('IBIS')>=0 &&item1.indexOf('IBIS')>=0){minpoints=minpoints+2}
237
238 const length = item0.length >= item1.length ?item1.length : item0.length;
239
240 const charby1=[];
241 const charby2=[];
242 let matched2char =0;
243 let matched1char =0;
244
245 //Create Array of Char
246 for(let i=0;i<length-1;i++)
247 {
248 charby2.push(`${item0.charAt(i)}${item0.charAt(i+1)} `)
249 charby1.push(item0.charAt(i))
250 }
251
252 //check if char conatins
253 for(let i=0;i<length;i++)
254 {
255 if(charby2.indexOf(`${item1.charAt(i)}${item1.charAt(i+1)}`)>=0)
256 {
257 matched2char++;
258 }
259 else
260 {
261 minpoints= minpoints-0.3;
262 }
263
264 if(charby1.indexOf(item1.charAt(i))>=0)
265 {
266 matched1char++;
267 }
268 else
269 {
270 minpoints= minpoints -1.2;
271 }
272 }
273
274 //Add points
275 if( matched1char <= length+1 &&matched1char >= length-1 )
276 {
277 charpoints++
278 }
279 })
280 })
281
282
283 //Result of CharPoints
284 let length1 = inarray[item.key].length;
285 let length2 = array[i].length;
286 lengthsub = length1>=length2 ?length1- length2: length2-length1; // Longer word - shorter word
287 if(passedmachedItems && minpoints>=-18 && charpoints>=2 && lengthsub<10){
288
289
290 if(email==1 || street == 1)
291 {
292
293 console.log(" ")
294 console.log("Found by points 0 ");
295 let equals = {
296 0:inarray,
297 1:arrayofelements[i],
298 id:`${inarray.id};${arrayofelements[i].id}`
299 };
300 inarray.founddb = true;
301 arrayofelements[i].founddb = true
302
303 preventdouble.push(`${arrayofelements[i].source}${arrayofelements[i].id}${inarray.source}${inarray.id}`)
304 sameItems.push(equals)
305
306 currentitem++;
307 return
308
309 }
310
311 }
312
313
314 //3 first char is same
315 let firstword =false;
316 let secondword =false;
317 try{
318
319 firstword = compare1[1].substr(0,3) == compare0[1].substr(0,3);
320 secondword = compare1[0].substr(0,3) == compare0[0].substr(0,3);
321 }
322 catch (TypeError) {
323 firstword = false
324 secondword = false
325 }
326
327
328 //Result
329 if(charpoints == compare0.length && compare0.length>1&&minpoints / compare0.length>=-5) {
330
331 if(inarray.stars == arrayofelements[i].stars){
332 let equals = {
333 0:inarray,
334 1:arrayofelements[i],
335 id:`${inarray.id};${arrayofelements[i].id}`
336 };
337 inarray.founddb = true;
338 arrayofelements[i].founddb = true
339
340 preventdouble.push(`${arrayofelements[i].source}${arrayofelements[i].id}${inarray.source}${inarray.id}`)
341 sameItems.push(equals)
342
343 currentitem++;
344 return
345 }
346 }
347
348 else if(charpoints == compare0.length && minpoints / compare0.length>=-8.5 && firstword && compare1.length>1||
349 charpoints == compare0.length && minpoints / compare0.length>=-8.5 && secondword && compare1.length>1) {
350 inarray.points = 80;
351 if(inarray.stars && arrayofelements[i].stars){
352 inarray.points = inarray.stars == arrayofelements[i].stars ? 95 : 80
353 }
354
355 if(inarray.points >80 && (email == 1 || geo == 1))
356 {
357
358 let equals = {
359 0:inarray,
360 1:arrayofelements[i],
361 id:`${inarray.id};${arrayofelements[i].id}`
362 };
363 inarray.founddb = true;
364 arrayofelements[i].founddb = true
365
366 preventdouble.push(`${arrayofelements[i].source}${arrayofelements[i].id}${inarray.source}${inarray.id}`)
367 sameItems.push(equals)
368
369 currentitem++;
370 return
371 }
372
373
374 }
375
376 else if(word.length>15 && inarray[item.key].length>15 && firstword && secondword && minpoints / compare0.length >-11 ) {
377
378 if(inarray.stars && arrayofelements[i].stars ){
379 inarray.points = inarray.stars == arrayofelements[i].stars ? 95 : 80;
380
381 if(inarray.points>=95 ){
382 let equals = {
383 0:inarray,
384 1:arrayofelements[i],
385 id:`${inarray.id};${arrayofelements[i].id}`
386 };
387 inarray.founddb = true;
388 arrayofelements[i].founddb = true
389
390 preventdouble.push(`${arrayofelements[i].source}${arrayofelements[i].id}${inarray.source}${inarray.id}`)
391 sameItems.push(equals)
392
393 currentitem++;
394 return
395 }
396 }
397
398 }
399
400 })
401
402}
403
404function compareGeo(value1, value2) {
405
406 if(value1.geovalue && value2.geovalue){
407 let a;
408
409 if(value1.geovalue< value2.geovalue)
410 a = value2.geovalue - value1.geovalue
411 else
412 a = value1.geovalue - value2.geovalue
413
414 if (a.toString().length<5)
415 {
416 return 1;
417 }
418 }
419
420 return 0
421
422
423}
424
425function compareEmail(value1, value2) {
426
427 if(!value1.manualEmail || !value2.manualEmail)return 0;
428
429 if(value1.manualEmail == 'NN' || value2.manualEmail == 'NN') return 0;
430
431 if(value1.manualEmail && value2.manualEmail){
432
433 // go for Domain
434 // let d1 = value1.manualEmail.indexOf('@');
435 // let d2 = value2.manualEmail.indexOf('@');
436
437 let email1 = value1.manualEmail.replace(" ","").substr(1,value1.manualEmail.length-2).replace('.','').replace('.','')
438 let email2 = value2.manualEmail.replace(" ","").substr(1,value2.manualEmail.length).replace('.','').replace('.','')
439
440
441 if(email2.indexOf(email1)>=0)
442 return 1
443
444 }
445 return 0;
446}
447
448function compareStreet(value1,value2) {
449
450 if(value1.adressStreet) {
451 if (value1.adressStreet == value2.adressStreet) {
452 return 1;
453 }
454 }
455return 0
456}
457
458function parser(w) {
459 let word = w.toUpperCase().replace('APARTHOTEL ','').replace('HOTEL','').replace('-STERNE').replace(/[^\w\s]/gi, '')
460
461 //Remove First Spaces
462 if(word.indexOf(" ")== 0)
463 while(word.indexOf(" ")== 0){
464 word = word.substring(1,word.length)
465 }
466
467 if(/\s+$/.test(word))
468 while(/\s+$/.test(word)){
469 word = word.substring(0,word.length-1)
470 }
471
472
473 return word;
474}
475
476function exclude(name) {
477
478
479 /* if(name.toUpperCase().indexOf("ST ")!=-1 ||
480 name.toUpperCase().indexOf("ST.")!=-1 ||
481 name.toUpperCase().indexOf("KREUZ B")!=-1 |)
482 return null;
483 else
484 */
485 return name;
486}
487
488function findNotSameHotels() {
489
490 return new Promise(resolve=>{
491 let i=0;
492 notsameitems = [];
493 console.log("===================================")
494 console.log('findNotSameHotels:')
495 arrayofelements.forEach(h=>{
496
497 if(h.founddb == false && h.source != 'db'){
498 console.log(h.name)
499 notsameitems.push(h)
500 i++;
501 }
502 });
503
504 if(i==0)console.log("--none--")
505 resolve(true);
506})
507
508}
509
510function fetchDBData(city) {
511 return new Promise(resolve=>{
512 _HP.db.execute(
513 ` SELECT H.name , H.id, H.adressRaw, H.geodata, H.stars, H.manualEmail
514 FROM CO2Hotels as H
515 LEFT JOIN CO2CityConfig as C
516 ON H.linkCity = C.linkCity
517 WHERE C.name = ? and H.isDead = 0; `,[city],
518 function(err, rows, fields)
519 {
520 if(rows){
521 rows.forEach(item=>{
522 let geo =null;
523 if(item.geodata)
524 {
525 geo = item.geodata.split(',');
526 geo =parseInt(geo[0].replace('.',''))+parseInt(geo[1].replace('.',''))
527 }
528
529 const hotel={
530 id: item.id,
531 name: item.name.replace('&amp;','&').replace('&#39;',"\'"),
532 adressRaw: item.adressRaw,
533 geodata: item.geodata,
534 geovalue: geo,
535
536 stars: item.stars,
537 manualEmail: item.manualEmail,
538 adressStreet:item.adressStreet,
539 founddb: 'db',
540 source: "db",
541 points:0,
542 }
543 arrayofelements.push(hotel)
544 })
545 generateTree(arrayofelements[0]).then(keys=> {
546
547 /*TEST*/
548 console.log(" ")
549 console.log("Looking for equal Hotels. Place "+city)
550 console.log("........................................")
551 console.log(" ")
552 init().then(res=>{
553 resolve(res)
554 })
555 });
556 }else {
557 console.log(err)
558 }
559 }
560 );
561 })
562}
563
564
565//###Export
566module.exports ={
567 "methods":{
568 startBookingCompare:startBooking
569 },
570
571}
572