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