11const fetch = require ( "node-fetch" ) ;
22
3- const numberOfItemsOnOnePage = 50 ;
4- const maximumOfRequests = 50 ;
3+ const express = require ( "express" ) ;
4+ const path = require ( "path" ) ;
5+
6+ const port = process . env . PORT || 8085 ;
7+ const app = express ( ) ;
8+
9+ const numberOfItemsOnOnePage = 10 ;
10+ const maximumOfRequests = 20 ;
511const currency = "CZK" ;
12+ let saunaLinks = "" ;
13+ let numberOfRequests = 0 ;
14+
15+ app . use ( express . static ( __dirname , { dotfiles : "allow" } ) ) ;
16+ app . engine ( "html" , require ( "ejs" ) . renderFile ) ;
17+ app . get ( "/" , async function ( req , res ) {
18+ saunaLinks = "" ;
19+ numberOfRequests = 0 ;
20+ if ( req . query ) {
21+ console . log ( req . query . query ) ;
22+ await fetchResultPage ( req . query . query ) ;
23+ res . render ( __dirname + "/public/index.html" , { links : saunaLinks } ) ;
24+ }
25+ } ) ;
26+ app . listen ( port ) ;
627
728const editableParams = {
8- query : "Lofoten, Norway " ,
9- checkin : "2020-04-01" ,
10- checkout : "2020-04-17" ,
29+ // query: "Helsinki, Finland ",
30+ // checkin: "2020-04-01",
31+ // checkout: "2020-04-17",
1132 price_min : 0 ,
1233 price_max : 10000 ,
1334 adults : 6
@@ -44,8 +65,6 @@ const getParams = (itemsOffset = 0) => ({
4465 satori_version : "1.1.0"
4566} ) ;
4667
47- let numberOfRequests = 0 ;
48-
4968const filterAsync = ( array , filter ) =>
5069 Promise . all ( array . map ( entry => filter ( entry ) ) ) . then ( bits =>
5170 array . filter ( entry => bits . shift ( ) )
@@ -68,6 +87,7 @@ const fetchHome = id => {
6887 . then ( body => {
6988 if ( body ) {
7089 const descriptionObject = body . pdp_listing_detail . sectioned_description ;
90+ const photo = body . pdp_listing_detail . photos [ 0 ] . large ;
7191 const searchedInFields = [
7292 "space" ,
7393 "summary" ,
@@ -85,11 +105,11 @@ const fetchHome = id => {
85105 if ( isSaunaAvailable ) {
86106 const params = getParams ( ) ;
87107 const homeUrl = `https://www.airbnb.cz/rooms/${ id } ` ;
88- const homeUrlWithDateInterval = params . checkin
89- ? ` ${ homeUrl } ?check_in= ${ params . checkin } &check_out= ${
90- params . checkout
91- } `
92- : homeUrl ;
108+ const homeUrlWithDateInterval =
109+ params . checkin && params . checkout
110+ ? ` ${ homeUrl } ?check_in= ${ params . checkin } &check_out= ${ params . checkout } `
111+ : homeUrl ;
112+ saunaLinks = ` ${ saunaLinks } <a href=" ${ homeUrlWithDateInterval } " target="_blank"><div class="item" style="background-image: url(' ${ photo } ')"></div></a>` ;
93113 console . log ( `- ${ homeUrlWithDateInterval } ` ) ;
94114 }
95115 }
@@ -101,32 +121,37 @@ const fetchHome = id => {
101121 return request ;
102122} ;
103123
104- const fetchResultPage = ( offset = 0 ) => {
105- if ( numberOfRequests > maximumOfRequests ) return ;
124+ const fetchResultPage = async ( query = null , offset = 0 ) => {
125+ if ( numberOfRequests > maximumOfRequests || query === null ) return ;
106126 const params = getParams ( offset ) ;
107- const paramString = Object . keys ( params )
108- . map ( key => `${ key } =${ encodeURIComponent ( params [ key ] ) } ` )
109- . join ( "&" ) ;
110- // console.log(`https://api.airbnb.com/v2/explore_tabs?${paramString}`);
111- fetch ( `https://api.airbnb.com/v2/explore_tabs?${ paramString } ` , {
112- method : "get" ,
113- headers : headers
114- } )
115- . then ( body => body . json ( ) . catch ( e => null ) )
116- . then ( body => {
117- if ( body ) {
118- const ids = body . explore_tabs [ 0 ] . home_tab_metadata . remarketing_ids ;
127+ console . log ( `Query: ${ query } ` ) ;
128+ const paramString =
129+ Object . keys ( params )
130+ . map ( key => `${ key } =${ encodeURIComponent ( params [ key ] ) } ` )
131+ . join ( "&" ) + `&query=${ query } ` ;
119132
120- const saunasUrls = filterAsync ( ids , fetchHome ) ;
121- if ( body . explore_tabs [ 0 ] . pagination_metadata . has_next_page ) {
122- fetchResultPage ( offset + numberOfItemsOnOnePage ) ;
123- }
133+ console . log ( `https://api.airbnb.com/v2/explore_tabs?${ paramString } ` ) ;
134+ const request = await fetch (
135+ `https://api.airbnb.com/v2/explore_tabs?${ paramString } ` ,
136+ {
137+ method : "get" ,
138+ headers : headers
139+ }
140+ ) ;
141+ try {
142+ const body = await request . json ( ) ;
143+ if ( body ) {
144+ const ids = body . explore_tabs [ 0 ] . home_tab_metadata . remarketing_ids ;
145+ const homePromise = await filterAsync ( ids , fetchHome ) ;
146+ if ( body . explore_tabs [ 0 ] . pagination_metadata . has_next_page ) {
147+ await fetchResultPage ( query , offset + numberOfItemsOnOnePage ) ;
124148 }
125- } )
126- . catch ( e => {
127- console . log ( e ) ;
128- } ) ;
149+ }
150+ } catch ( err ) {
151+ console . log ( "error" ) ;
152+ }
153+
129154 numberOfRequests ++ ;
130155} ;
131- console . log ( `Saunas ${ getParams ( ) . query } \n============================` ) ;
156+ console . log ( `Saunas\n============================` ) ;
132157fetchResultPage ( ) ;
0 commit comments