UNPKG

39.1 kBJavaScriptView Raw
1/*
2Name: Weslley De Souza
3Date: 16.08.2017
4Version: 1.0
5Notes: STRICT MODE
6
7CHANGELOG:
8
9 */
10'use strict';
11
12//Libraries
13const _driver = require('selenium-webdriver');
14const webdriver = require('selenium-webdriver');
15const By = require('selenium-webdriver').By;
16const until = require('selenium-webdriver').until;
17
18const moment = require('moment');
19var request = require('request');
20
21//Spooky Library
22try {
23 var Spooky = require('spooky');
24}
25catch (e) {
26 //npm i tiny-jsonrpc
27 var Spooky = require('../lib/spooky');
28}
29
30//Database connection
31/*
32const mysql = require('mysql2');
33const connection = mysql.createConnection({
34 host: '192.168.254.75',
35 user: 'wds',
36 password:'SAS7Ukku',
37 database: 'hotelpartner'
38});
39*/
40const connection = null;
41
42//###Variables
43var ISFETCHING = false;
44const joburls = {
45 "Booking":[]
46};
47const skipjobs = {
48 "Booking":[]
49};
50const hotellist = {
51 "Trivago":[],
52 "Booking":[],
53 "Hrs":[],
54 "expedia":[],
55 "compared":{
56 "Booking":[],
57 }
58};
59const bewertung = ["Gut","Fabelhaft","Hervorragend"]
60
61
62//###Classes
63class Hotel{
64
65 constructor(name=null, price1=null,price2=null, place=null, stars=null, rating=null, people=null, cancellation=null, peopleview=null, roomleft=null, inclusive=null, start=null, end=null, site=null) {
66
67 //ref
68 this.name = name;
69 this.place = place;
70 this.rating = rating;
71 this.price1 = price1;
72 this.price2 = price2;
73 this.people = people;
74 this.stars = stars;
75 this.roomleft = roomleft;
76 this.peopleview = peopleview;
77 this.cancellation = cancellation;
78 this.inclusive = inclusive;
79 this.site = site;
80 this.datestart = start;
81 this.dateend = end;
82 this.img = null;
83
84 //as obj
85 const obj = { };
86 Object.setPrototypeOf(obj, Hotel.prototype);
87 obj.name = name;
88 obj.place = place;
89 obj.stars = stars;
90 obj.rating = rating;
91 obj.price1 = price1;
92 obj.price2 = price2;
93 obj.people = people;
94 obj.roomleft = roomleft;
95 obj.peopleview = peopleview;
96 obj.cancellation = cancellation;
97 obj.inclusive = inclusive;
98 obj.site = site;
99 obj.datestart = start;
100 obj.dateend = end;
101 obj.img = null;
102
103 return obj
104
105 }
106
107 toString() {
108 return `${this.name} | ${this.place} | ${this.rate} | ${this.price} | ${this.site}`
109 }
110
111 print () {
112 console.log( this.toString() );
113 }
114
115 printToURL() {
116 let _url = `{"Name":"${this.name}","Street":"","City":"${this.place}","Stars":"${this.stars}","Rating":"${this.rating}","Preferred":"0","LastBooking":"0","Success":"0","Values":{"${this.datestart}":"${this.price1};${this.price2};0;0;0;0"}}`;
117 return _url;
118 }
119 }
120
121
122//###Functions
123
124
125/**
126 * Fetch BookingCOM
127 *
128 * return hotellist.Booking
129 */
130function getBooking(destid,place,start,end,room=1,adlt=1,child=0,nextpage=false,job,dbid) {
131
132 let adult = adlt;
133 const startdate = moment(start);
134 const enddate = moment(end);
135
136 let totadd = 0;
137 var pageoffset = 0;
138 let page = 0;
139
140
141 const dt= startdate.format('YYYY-MM-DD');
142 const city = {
143 name:place,
144 id:destid,
145 values:[]
146 }
147
148 //add new destID
149 let cityindex = hotellist.Booking.findIndex(b=>b.id==destid);
150
151 if(cityindex==-1)
152 hotellist.Booking.push(city);
153
154 //add new date
155 cityindex = hotellist.Booking.findIndex(b=>b.id==destid)
156 const dateindex = hotellist.Booking[cityindex].values.findIndex(b=>b.date==dt)
157
158 if(dateindex>=0)
159 hotellist.Booking[cityindex].values[dateindex].values = [];
160 else
161 hotellist.Booking[cityindex].values.push({
162 'date':dt,
163 values:[]
164 })
165
166
167 console.log(`https://www.booking.com/searchresults.de.html?src=index&dest_type=city&dest_id=${destid}&ssne=${place}&ssne_untouched=${place}&ss=${place}&checkin_monthday=${startdate.format('DD')}&checkin_year_month=${startdate.format('YYYY-M')}&checkout_monthday=${enddate.format('DD')}&checkout_year_month=${enddate.format('YYYY-M')}&ci_date=${startdate.format('YYYY-MM-DD')}&co_date=${enddate.format('YYYY-MM-DD')}&room=${room}&no_rooms=${room}&group_adults=${adult}&group_children=${child};unchecked_filter=out_of_stock%20;nflt=;unchecked_filter=out_of_stock&selected_currency=hotel_currency&changed_currency=1`)
168 return new Promise(function (resolve) {
169 // Prevent multiple Fetch
170 //if(ISFETCHING){resolve(false);return};
171 //ISFETCHING= true;
172
173 //**recursive Function**
174 //function will be called while there is a next button on the page
175 function getData() {
176
177 //let url = `https://www.booking.com/searchresults.de.html?l&checkin_monthday=${startdate.format('DD')}&checkin_year_month=${startdate.format('YYYY-M')}&checkout_monthday=${enddate.format('DD')}&checkout_year_month=${enddate.format('YYYY-M')}&ci_date=${startdate.format('YYYY-MM-DD')}&co_date=${enddate.format('YYYY-MM-DD')}&room1=A%2CA&no_rooms=1&group_adults=${adult}&group_children=${child}&ss_raw=${place}&ac_position=0&dest_id=${destid}&dest_type=city&search_pageview_id=b70c6c0a3fea0249&search_selected=true&search_pageview_id=b70c6c0a3fea0249&ac_suggestion_list_length=5&ac_suggestion_theme_list_length=0&nflt=;unchecked_filter=out_of_stock&selected_currency=hotel_currency&changed_currency=1`
178 let url = `https://www.booking.com/searchresults.de.html?src=index&dest_type=city&dest_id=${destid}&ssne=${place}&ssne_untouched=${place}&ss=${place}&checkin_monthday=${startdate.format('DD')}&checkin_year_month=${startdate.format('YYYY-M')}&checkout_monthday=${enddate.format('DD')}&checkout_year_month=${enddate.format('YYYY-M')}&ci_date=${startdate.format('YYYY-MM-DD')}&co_date=${enddate.format('YYYY-MM-DD')}&room=${room}&no_rooms=${room}&group_adults=${adult}&group_children=${child};unchecked_filter=out_of_stock%20;nflt=;unchecked_filter=out_of_stock&selected_currency=hotel_currency&changed_currency=1`
179 if(nextpage)
180 {
181 pageoffset+=20;
182 url.replace(`&rows=16&offset=${pageoffset}`,'')
183 url += `&rows=16&offset=${pageoffset}`
184 }
185
186 //Create new Instance of Webdrvier and pass CasperJS parameters
187 //Spooky does not support ES6!
188 const spooky = new Spooky({
189
190 pageSettings: {
191 loadImages: false,
192 loadPlugins: false,
193 userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1588.0 Safari/537.36'
194 },
195 verbose: true,
196 logLevel: "debug",
197 },
198 function (err) {
199 //Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1 Iceweasel/14.0.1
200 //Chrome/37.0.2062.120
201 spooky.userAgent('Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1 Iceweasel/14.0.1');
202 spooky.start(url);
203 //Show current page
204 //check for pagination
205 spooky.then(function () {
206 this.emit('currentpage',this.getCurrentUrl())
207 this.scrollTo(1000, 1200);
208 this.emit('pagination', this.evaluate(function () {
209 //get elements
210 var next = document.getElementsByClassName("paging-next");
211 return next[0].innerText
212
213 }));
214
215 });
216 //Get Elements
217 //TODO: If booking changes their classname : change it here
218 spooky.then(function() {
219 //wait for elements to be loaded
220 this.capture('page.png');
221 this.scrollTo(100, 100);
222 this.emit('parse', this.evaluate(function () {
223 var items =null
224 var mystring = "";
225 items = document.getElementsByClassName("sr_item_default");
226 var imgs = document.querySelectorAll('.sr_item_default img');
227 if(items)
228 {
229 for(var i=0; i<items.length;i++)
230 {
231 //add divider to split
232 mystring += items[i].innerText + " \n"+ imgs[i].src+ " "+ "myH0TeL";
233 }
234 }
235 return mystring;
236 }));
237 })
238 //start
239 spooky.run();
240 });
241 spooky.on('pagination', function (text) {
242 if(text=='Nächste Seite')
243 {
244 page++;
245 //console.info("page offset "+ pageoffset)
246 console.info("========================= page: "+ page+" Adult: "+adult+ " Datum:"+ startdate.format('DD.MM.YYYY')+" ========================" )
247 nextpage =true
248 }
249 else {
250 console.info("========================= page: "+ (page+1)+" Adult: "+adult+" ========================" )
251 nextpage = false;
252 }
253
254 });
255 spooky.on('currentpage', function (text) {
256 console.log(" ")
257 // console.info(text)
258
259 });
260 spooky.on('parse', function (text) {
261 let totaddpage = 0;
262
263 if(text)
264 {
265 const hotels = text.split("myH0TeL");
266 console.log("Booking array length: "+hotels.length);
267 hotels.forEach(items=>{
268 const words = items.split("\n");
269 //rabattpreis
270 let lastprice = true;
271
272 const parsedhotel = parseBooking(words/*array*/,lastprice/*aktion price*/,dt,enddate.format('YYYY-MM-DD'),'Booking.com',adult)
273
274 //const
275 const cindex = hotellist.Booking.findIndex(b=>b.id==destid)
276 const dindex = hotellist.Booking[cindex].values.findIndex(b=>b.date==dt)
277 const idx = hotellist.Booking[cindex].values[dindex].values.findIndex(i=>i.name == parsedhotel.name);
278
279
280 if( parsedhotel.name && parsedhotel.price1 && idx == -1)
281 {
282
283 console.log("adding " + parsedhotel.name + " p1:"+parsedhotel.price1)
284 hotellist.Booking[cindex].values[dindex].values.push(parsedhotel);
285
286 totadd++;
287 totaddpage++;
288 }
289 else
290 {
291 if(idx >-1 && parsedhotel.name && adult==2)
292 {
293 hotellist.Booking[cindex].values[dindex].values[idx].price2 = parsedhotel.price2;
294 console.log("update "+parsedhotel.name +" p2:"+parsedhotel.price2)
295 }
296 }
297 });
298 console.log("pagetot: "+totaddpage);
299 console.log("tot: " +totadd);
300 //go to next page or add adult
301 //then exit
302 if(nextpage )
303 {
304 setTimeout(()=>{
305 getData();
306 },200);
307 spooky.destroy();
308 return;
309 }
310 else {
311 if(adult==1)
312 {
313 setTimeout(()=>{
314 nextpage=true;
315 page = 0;
316 adult=2;
317 pageoffset=-20
318 getData();
319 },200);
320
321 spooky.destroy();
322 }
323 else
324 {
325 console.log(">>DONE<<")
326 setTimeout(()=>{
327 ISFETCHING= false;
328 resolve(JSON.stringify(hotellist))
329 },200)
330 spooky.destroy();
331 }
332 }
333 }
334 else
335 {
336 //add adult
337 if(adult==1)
338 {
339 setTimeout(()=>{
340 nextpage=true
341 page = 0;
342 pageoffset=-20;
343 adult=2;
344 getData();
345 },200);
346 spooky.destroy();
347 }
348 //finish
349 else {
350 ISFETCHING= false;
351 console.log("no elements found")
352 console.log(">>DONE<<")
353 resolve(JSON.stringify(hotellist))
354 spooky.destroy();
355 }
356 }
357 });
358 spooky.on('error', function(msg,backtrace) {
359 console.log(msg)
360 });
361 }
362 getData();
363 })
364}
365
366/*bellow getgunctions not testet*/
367function getTrivago(place,room='1',start,end) {
368 let startdate =moment(start)
369 let endtdate =moment(end)
370
371 if(room=='1'){
372 room='RoomType=1'
373 }
374 else {
375 room='RoomType=7'
376 }
377
378 return new Promise(function (resolve) {
379
380 resolve([])
381 return
382 // open selenium
383 var driver = new webdriver.Builder().forBrowser('phantomjs').build();
384 // maximize the window
385 driver.manage().window().maximize();
386 // delete all cookies
387 driver.manage().deleteAllCookies();
388
389 // navigate to Trivago
390 driver.get(`https://www.trivago.ch/`).then(()=> {
391
392 setTimeout(() => {
393 driver.wait(until.titleIs('trivago.ch - Hotelpreisvergleich | Günstige Hotels finden | Hotelsuche')).then(function () {
394 const input = driver.findElement(By.xpath("//*[@id='horus-querytext']"));
395 input.sendKeys(place);
396 input.sendKeys(' ');
397 input.sendKeys(_driver.Key.DOWN);
398 input.sendKeys(_driver.Key.ENTER);
399 }).then(() => {
400 setTimeout(() => {
401 driver.getCurrentUrl().then(function () {
402 return driver.getCurrentUrl();
403 }).then(function (currentUrl) {
404
405 let url = currentUrl.replace('Barr%5D=', 'Barr%5D=' + startdate.format('YYYY-MM-DD')+"&n=").replace('dep%5D=2', 'dep%5D=' + endtdate.format('YYYY-MM-DD')+'nn=').replace('RoomType=',room+'nnn=')
406
407 driver.get(`url`).then(() => {
408
409 driver.wait(until.titleIs('trivago.ch - Hotelpreisvergleich | Günstige Hotels finden | Hotelsuche')).then(function () {
410 setTimeout(()=>{
411 let a;
412 // a = driver.findElements(By.className("item"))
413 // if (a.length == 0)
414 a = driver.findElements(By.className("hotel"))
415
416 a.then(items => {
417 hotellist.Trivago = [];
418 items.forEach(element => {
419 element.getText().then(text => {
420
421 let words = text.split("\n");
422 const myhotel = parseTravago(words,startdate.format('YYYY-MM-DD'),endtdate.format('YYYY-MM-DD'),'Trivago')
423 hotellist.Trivago.push(myhotel)
424
425 });
426
427 })
428 }).then(() => {
429 resolve(JSON.stringify(hotellist))
430 })
431 },1000)
432 })
433
434 });
435 }, 2000)
436 })
437 }, 1000)
438 })
439 })
440
441 })
442}
443function getHrs(place,start,end) {
444 let startdate =moment(start)
445 let endtdate =moment(end)
446 return new Promise(function (resolve) {
447 try {
448 // open selenium
449 var driver = new webdriver.Builder().forBrowser('phantomjs').build();
450 // maximize the window
451 driver.manage().window().maximize();
452 // delete all cookies
453 driver.manage().deleteAllCookies();
454
455 // navigate to hrs
456 driver.get("https://www.hrs.de/");
457
458
459
460 driver.getTitle().then(function(title) {
461 console.log(title)
462 });
463 driver.wait(until.titleIs('HRS - Hotel Reservation Service | Hotel finden und günstige Hotels buchen')).then(function () {
464
465
466 const input = driver.findElement(By.xpath("//*[@id='destiny']"));
467 const von = driver.findElement(By.xpath("//input[@id='start_stayPeriod']"));
468 const bis = driver.findElement(By.xpath("//input[@id='end_stayPeriod']"));
469
470 input.sendKeys(place);
471 input.sendKeys(' ');
472 input.sendKeys(_driver.Key.DOWN);
473 input.sendKeys(_driver.Key.ENTER);
474
475 setTimeout(()=>{
476 von.sendKeys(startdate.format('YYYY.MM.DD'));
477 bis.sendKeys(endtdate.format('YYYY.MM.DD'));
478
479 //Close Calendar
480 input.click();
481 console.log("->")
482
483 const btn = driver.findElement(By.xpath("//*[@id='searchForm']/fieldset[3]/input"));
484 btn.click();
485 },290)
486
487 }).then(()=>{
488
489 setTimeout(()=>{
490 console.log("get hotelTeaserContainer")
491 let a= driver.findElements(By.className("hotelTeaserContainer"));
492 a.then(items=>{
493 console.error(items)
494 hotellist.Hrs=[];
495 let i = 0;
496 items.forEach(element=>{
497 element.getText().then(text=>{
498 driver.executeScript("arguments[0].scrollIntoView()", element);
499 console.log("HRS:")
500 console.log(text)
501
502 //HRS:
503 /*
504 [0]
505 [2] Hotelname
506 [3]
507 [3]
508 [4]
509 [5]
510 [5]
511
512 */
513 try{
514 let words = text.split("\n");
515 let a = new _HP.classes.Hotel(words[2], words[3], words[7], _HP.methods.getPrice(words), "Hrs")
516 hotellist.Hrs.push(a)
517 }catch (UnsupportedOperationError){
518 //super error Handler
519 console.log("ERR <>")
520 }
521
522 });
523 })
524
525 }).then(()=>{
526 resolve(JSON.stringify(hotellist))
527 })
528 },4000)
529 })
530
531 }catch (NoSuchElementError){
532 console.log("HRS Element not Found")
533 resolve(null)
534
535 }
536
537 })
538}
539function getExpedia(place,date=null) {
540 return new Promise(function (resolve) {
541 try {
542 // open selenium
543 var driver = new webdriver.Builder().forBrowser('firefox').build();
544 // maximize the window
545 driver.manage().window().maximize();
546 // delete all cookies
547 driver.manage().deleteAllCookies();
548
549
550 // navigate to Trivago
551 driver.open()
552 driver.get(`https://www.expedia.de/`);
553
554
555 driver.getTitle().then(function(title) {
556 console.log(title)
557 });
558
559 driver.wait(until.titleIs('Billige Flüge, Hotels & Mietwagen buchen | Expedia.de')).then(function () {
560
561 setTimeout(()=>{
562
563 let a= driver.findElements(By.className("listing"))
564 a.then(items=>{
565 hotellist.expedia=[];
566 items.forEach(element=>{
567 element.getText().then(text=>{
568 console.log(text)
569
570 //TRIVAGO:
571 /*
572 [0]
573 [1] Hotelname
574 [2] Ort + Distanz
575 [3] Bewertung 100
576 [4] Kommentartbewertungen
577 [5] Other details
578 [5] Site
579
580 [9] first price
581
582 */
583 let words = text.split("\n");
584 let a = new _HP.classes.Hotel(words[0], words[5], words[4], _HP.methods.getPrice(words), "expedia")
585 hotellist.expedia.push(a)
586 });
587 })
588 }).then(()=>{
589 resolve(JSON.stringify(hotellist))
590 })
591
592 },2900)
593 })
594
595 }catch (NoSuchElementError){
596 console.log(NoSuchElementError)
597 console.log("Element not Found")
598 }
599
600 })
601}
602
603
604
605/**
606 * Parse BookingCOM
607 * @param arrayOfWords required {string array} of all innerHTML Elements
608 * @param firstOrLastPrice *optional {boolean} || true == first price
609 *
610 * return new Hotel Obj
611 */
612function parseBooking(arrayOfWords,firstOrLastPrice=false,start,end,site=null, adult) {
613
614 let counter =0;
615 let i =-2;
616
617 let hotel = new Hotel();
618 hotel.site = site;
619 hotel.datestart = start;
620 hotel.dateend = end;
621 hotel.img = arrayOfWords[arrayOfWords.length-1]
622
623 try{
624 JSON.parse(JSON.stringify(arrayOfWords)).forEach(item=>{
625
626 //Price
627 if( item.toUpperCase().indexOf("CHF")!=-1 ||item.toUpperCase().indexOf("€")!=-1 ){
628 if(! hotel.price1 || firstOrLastPrice || ! hotel.price2){
629 if(adult==2 )
630 {
631 if(item.toUpperCase().indexOf("€")!=-1){
632 hotel.price2 = item.substr(item.indexOf('€')+2,item.length);
633 }
634 else if(item.toUpperCase().indexOf("CHF")!=-1){
635 hotel.price2 = item.substr(item.indexOf('CHF')+4,item.length);
636 }
637 hotel.price2 = hotel.price2.replace(/ /g,'')
638 return;
639 }
640 if(adult==1) {
641 if(item.toUpperCase().indexOf("€")!=-1){
642 hotel.price1 = item.substr(item.indexOf('€')+2,item.length);
643 }
644 else if(item.toUpperCase().indexOf("CHF")!=-1){
645 hotel.price1 = item.substr(item.indexOf('CHF')+4,item.length);
646 }
647 hotel.price1 = hotel.price1.replace(/ /g,'')
648 }
649 }
650 }
651
652
653 //Hotelname
654 let nameindex = item.toUpperCase().indexOf("-STERNE")
655
656 if( nameindex >= 0 && !hotel.name )
657 {
658 hotel.stars = item.substr(nameindex -1, 8).replace('Wird in neuem Fenster geöffnet','').replace('-Sterne','');
659 hotel.name = item.substring(0,nameindex - 2).replace(' Wird in neuem Fenster geöffnet','').replace('"',"").replace('"',"");
660 while(/\s+$/.test(hotel.name))
661 {
662
663 hotel.name = hotel.name.substring(0,hotel.name.length-1)
664 }
665
666 //next item will be place
667 i=counter;
668 }
669
670 /*2 Hotelname*/
671 if(!hotel.name && item.indexOf('Wird in neuem Fenster geöffnet') >=3 )
672 {
673 if(item.length>=20)
674 hotel.name = item.substr(0,item.indexOf(' Wird in neuem Fenster')).replace('"',"").replace('"',"")
675 if(/\s+$/.test(hotel.name))
676 while(/\s+$/.test(hotel.name)){
677 hotel.name = hotel.name.substring(0,hotel.name-1)
678 }
679 }
680
681 //Place
682 if(counter==i+1 || item.indexOf('Auf der Karte anzeigen')>=0 || item.indexOf('In U-Bahn-Nähe')>0 )
683 {
684
685 hotel.place = item.replace(' – Auf der Karte anzeigen','');
686 hotel.place = hotel.place.replace(' – In U-Bahn-Nähe','');
687 hotel.place = hotel.place.replace('– In U-Bahn-Nähe','');
688 if(hotel.place.indexOf('(')>=0)
689 {
690 hotel.place = hotel.place.substr(0, hotel.place.indexOf(' ('))
691 }
692
693 }
694
695 //rating
696 if(!hotel.rating && item.indexOf(',')>=0)
697 {
698 bewertung.forEach(i=>{
699 if(item.toUpperCase().indexOf(i.toUpperCase())>=0)
700 hotel.rating = item.substr(item.length-3,item.length);
701 })
702 }
703
704 //amount left
705 if(!hotel.roomleft && item.indexOf('Nur noch')>=0)
706 {
707 let str = item.indexOf('Zimmer')-2
708 hotel.roomleft = item.substring(str, str+8)
709 }
710
711 //Inclusive
712 if(item.indexOf('inbegriffen')>=0){
713 hotel.inclusive = item;
714 }
715
716 //cancellation
717 if(item.indexOf('Stornierung')>=0){
718 hotel.cancellation = item;
719 }
720
721 //Personenzahl (DOPPELZIMMER)
722 if(item.indexOf('Personenzahl')>=0 || item.indexOf('Personenzahl')>=0){
723 hotel.people = item;
724 }
725
726
727 //remove false elements
728 if(hotel.name)
729 if(hotel.name.indexOf('verfügbaren Zimmer')>=0){
730 hotel.name=null
731 }
732
733
734 counter++;
735
736 })
737 }catch (TypeError){
738 console.error(TypeError)
739 }
740
741 return hotel;
742}
743function parseMobileBooking(arry,adult,start,end,place,destid) {
744 console.log(" ");
745 console.log("start parse id: "+destid+" date: "+start+" ADULT:" + adult)
746
747 const dt= start;
748 const city = {
749 name:place.replace('+'," "),
750 id:destid,
751 values:[]
752 };
753
754
755 //add new destID
756 let cityindex = hotellist.Booking.findIndex(b=>b.id==destid);
757
758 if(cityindex==-1)
759 hotellist.Booking.push(city);
760
761 //add new date
762 cityindex = hotellist.Booking.findIndex(b=>b.id==destid)
763 const dateindex = hotellist.Booking[cityindex].values.findIndex(b=>b.date==dt)
764
765 if(dateindex==-1)
766 hotellist.Booking[cityindex].values.push({
767 'date':dt,
768 values:[]
769 })
770
771
772 return new Promise((resolve)=>{
773 let addedtot = 0;
774 let updatettot = 0
775 try{
776 arry.forEach(hotel=> {
777
778 const parsedhotel = new Hotel();
779 parsedhotel.price = '0.00';
780 parsedhotel.price2 = '0.00';
781 parsedhotel.name = '';
782
783 parsedhotel.name = hotel.hotel_name;
784 parsedhotel.datestart = start;
785 parsedhotel.dateend = end;
786 parsedhotel.stars = hotel.class;
787 parsedhotel.people = adult;
788 parsedhotel.place = hotel.district;
789 parsedhotel.city = hotel.city;
790 parsedhotel.address = hotel.address;
791 parsedhotel.rating = hotel.facilities_review_score['rating'];
792 parsedhotel.ranking = hotel.ranking;
793 parsedhotel.peopleview = hotel.last_reservation_text;
794 parsedhotel.roomleft = hotel.available_rooms
795 parsedhotel.id = hotel.hotel_id;
796 parsedhotel.district_id = hotel.district_id;
797 parsedhotel.currency_code = hotel.currency_code;
798 parsedhotel.zip = hotel.zip;
799 parsedhotel.hotel_include_breakfast = hotel.hotel_include_breakfast
800 parsedhotel.description = hotel.short_description
801 parsedhotel.preferred = hotel.preferred
802 parsedhotel.latitude = hotel.latitude
803 parsedhotel.longitude = hotel.longitude
804 parsedhotel.geodata = hotel.latitude+','+hotel.longitude
805 parsedhotel.site = 'Booking.com';
806 if(hotel.last_reservation_text){
807 parsedhotel.last_reservation = hotel.last_reservation_text.replace('Letzte Buchung: vor ','').replace(' Stunden','').replace(' Stunde','').replace(' gestern','');;
808 }
809
810 if(parsedhotel.last_reservation){
811
812 if(hotel.last_reservation_text.indexOf('Stunde')>=0)
813 parsedhotel.last_reservation = moment().subtract(parseInt(parsedhotel.last_reservation),'hours').unix();
814
815 if(hotel.last_reservation_text.indexOf('gestern')>=0)
816 parsedhotel.last_reservation = moment().subtract(24,'hours').unix();
817
818 if(hotel.last_reservation_text.indexOf('.')>=0){
819 const startidx = hotel.last_reservation_text.indexOf('.');
820 const monthname = hotel.last_reservation_text.substr(startidx+2,hotel.last_reservation_text.length)
821 const daynr = hotel.last_reservation_text.substr(startidx-2,2)
822 const monthnr = moment().locale("de").month(monthname).format("MM")
823 const year = moment().format('YYYY')
824 const string = `${year}-${monthnr}-${daynr}`
825 parsedhotel.last_reservation = moment(string,'YYYY-MM-DD').unix()
826 }
827
828 }
829
830
831 //const
832 const cindex = hotellist.Booking.findIndex(b=>b.id==destid)
833 const dindex = hotellist.Booking[cindex].values.findIndex(b=>b.date==dt)
834 const idx = hotellist.Booking[cindex].values[dindex].values.findIndex(i=>i.name == parsedhotel.name);
835
836
837 if(!parsedhotel.name)return;
838
839 //new Hotel Price 1
840 if( idx == -1 && adult == 1)
841 {
842 parsedhotel.price1 = hotel.min_total_price;
843 hotellist.Booking[cindex].values[dindex].values.push(parsedhotel);
844 addedtot++;
845 }
846 //new Hotel Price 2
847 else if(idx == -1 && adult == 2)
848 {
849
850 parsedhotel.price2 = hotel.min_total_price;
851 hotellist.Booking[cindex].values[dindex].values.push(parsedhotel);
852 addedtot++;
853 }
854 //add Price 2 to Hotel
855 else if(idx >-1 && adult == 2)
856 {
857 hotellist.Booking[cindex].values[dindex].values[idx].price2 = hotel.min_total_price;
858 updatettot++;
859 }
860
861 });
862
863
864 console.log("========================")
865 console.log("total added "+addedtot)
866 console.log("updatet added "+updatettot)
867 console.log("date " +start)
868 console.log("destid "+destid)
869 console.log("total " +arry.length)
870 console.log(" ")
871 if(arry.length==0){
872 resolve(0);
873 }
874
875 resolve(adult);
876 }catch (TypeError){
877 console.log("no result")
878 resolve(0);
879 }
880 })
881
882}
883function parseTravago(arrayOfWords,start,end,site=null){
884
885function getObjectKey(obj, keyToFind) {
886 if(keyToFind.indexOf('CHF')>=0)return null;
887 var i = 0, key;
888
889 for (key in obj) {
890 if (key.toUpperCase().indexOf(keyToFind.toUpperCase())>=0) {
891 return key;
892 }
893
894 i++;
895 }
896 return null;
897 }
898
899 //TRIVAGO:
900 /*
901 [0]
902 [1] name
903 [2] Ort + Distanz
904 [3] Bewertung 100
905 [4] Kommentartbewertungen
906
907 */
908
909const array = JSON.parse(JSON.stringify(arrayOfWords))
910
911 const hotel = new Hotel();
912 hotel.name = array[1];
913 hotel.place = array[2];
914
915
916 hotel.site = site;
917 hotel.datestart = start;
918 hotel.dateend = end;
919
920 let previtem;
921 let counter =0;
922 let i =0;
923 let prices = {
924 "mainprice":null,
925 "Hotelwebseite":"",
926 "Elvoline":"",
927 "Expedia":"",
928 "Booking.com":"",
929 "Hotels.com":"",
930 "HRS.com":"",
931 "Hotel.info":"",
932 "otel.com":"",
933 "Tui.ch":"",
934 "Hotelopia":"",
935 };
936
937 array.forEach(item=>{
938
939 //Price
940 if( item.toUpperCase().indexOf("CHF")!=-1 ){
941
942
943 const index = getObjectKey(prices, previtem.toString().replace(" ","") )
944 if(index!=null)
945 prices[index] = item;
946
947 if(prices.mainprice==null)
948 prices.mainprice = item;
949
950 hotel.price = prices;
951
952 }
953
954 //rating
955 if(item){
956 if(item.length == 2 ){
957 hotel.rating = item;
958 }
959 }
960
961
962 //Inclusive
963 if(item.indexOf('inbegriffen')>=0 ||item.indexOf('Gratis')>=0 ||item.indexOf('Gratis')>=0 ){
964 hotel.inclusive = item;
965 }
966
967
968 counter++;
969 previtem = item;
970
971 })
972
973 return hotel;
974}
975
976
977function generateUrl(array,job,dbid,date,slave=null,send=true){
978return new Promise(resovle=>{
979
980
981 let bodyjob={
982 "linkcity":dbid,
983 "destid":array[0].id,
984 "job":{
985 id:job,
986 ez:-1,
987 dz:-1
988 },
989 "cityname":array[0].name,
990 "date":date,
991 "data":array[0].values,
992 "dbid":dbid,
993 "slave":slave
994 };
995
996 sendResult(bodyjob,send).then(r=>{
997 resovle(r)
998 })
999 return;
1000
1001
1002 let starturl = '';
1003 let endurl = `]&job={"JobID":"${job.toString()}","foundEZ":"-1","foundDZ":"-1""pages":"0"}`;
1004
1005 /*
1006 try{
1007 array.forEach(city=>{
1008 const cname =city.name;
1009 const destid =city.id;
1010 city.values.forEach(dates=>{
1011
1012 const cdate =dates.date;
1013 if(skipjobs.Booking.indexOf(cdate)==-1)
1014 {
1015 skipjobs.Booking.push(cdate)
1016 starturl = `city=${dbid}&datumList=["${cdate}"]&los=1&data=[`
1017 dates.values.forEach(h=>{
1018 let hotel = new Hotel();
1019 hotel = h;
1020
1021 values += hotel.printToURL()+`,`
1022 })
1023 }
1024
1025 })
1026
1027 let str = starturl +values.substr(0,values.length-1)+ endurl
1028 sendResult(str,true)
1029
1030
1031 })
1032 }
1033 catch (TypeError)
1034 {
1035 console.error(TypeError)
1036 }
1037*/
1038 return joburls
1039})
1040}
1041function sendResult(_body, active=true){
1042 return new Promise(resolve=>{
1043
1044
1045 if(!active)return;
1046 //&gateway='+_SERVER.os.hostname().replace('slave','')
1047 console.log("sending data to host")
1048 const reciverurl='http://192.168.56.1:88/sendtestdb';
1049 request.post({
1050 headers: {'content-type' : 'application/x-www-form-urlencoded'},
1051 url: reciverurl,
1052 json: true,
1053 form: {
1054 data: JSON.stringify(_body)
1055 }
1056 },
1057 function(error, response, body){
1058 resolve(body);
1059 });
1060
1061
1062 return;
1063 request.post({
1064 headers: {'content-type' : 'application/x-www-form-urlencoded'},
1065 url: 'http://192.168.56.1:202/api/v1',
1066 body: JSON.stringify(_body)
1067 },
1068 function(error, response, body){
1069 console.log(body);
1070 });
1071
1072 })
1073
1074}
1075
1076//###Export
1077module.exports ={
1078 "db" :connection,
1079 "hotels" :hotellist,
1080 "jobs" :{
1081 "urls":joburls,
1082 "skip":skipjobs
1083 },
1084 "classes":{
1085 "Hotel":Hotel
1086 },
1087 "methods":{
1088 "general": {
1089 generateUrl:generateUrl
1090 },
1091 "Booking":{
1092 "get": getBooking,
1093 "parse": parseBooking,
1094 "parsemobile":parseMobileBooking
1095
1096 },
1097 "Trivago":{
1098 "get":getTrivago,
1099 "parse":parseTravago
1100 }
1101 },
1102};
1103
1104//###Error Output
1105process.on('uncaughtException', function (error) {
1106 console.log(error.stack);
1107});
1108process.on('TypeError', function (error) {
1109 console.log(error.stack);
1110});
\No newline at end of file