-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distance calculations, Fuel prices scraping+updating and local places…
… to visit added
- Loading branch information
1 parent
d5d4391
commit 75fcfec
Showing
9 changed files
with
338 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const axios = require('axios') | ||
|
||
function arePointsNear(checkPoint, centerPoint, km) { | ||
let ky = 40000 / 360; | ||
let kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky; | ||
let dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx; | ||
let dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky; | ||
return Math.sqrt(dx * dx + dy * dy) <= km; | ||
} | ||
|
||
class Coordinates { | ||
|
||
constructor(coordinates) { | ||
this.coordinates = coordinates | ||
} | ||
|
||
async getNearbyPlaces(query, radius, type){ | ||
const places = await axios.request({ | ||
method: 'get', | ||
url: 'https://maps.googleapis.com/maps/api/place/textsearch/json', | ||
params: { | ||
query: query, | ||
key: process.env.mapsKey, | ||
location: this.coordinates.lat + ',' + this.coordinates.lng, | ||
radius: 3500, | ||
type: type | ||
} | ||
}) | ||
const data = places.data.results | ||
const placeResults = [] | ||
data.forEach((searchResult)=>{ | ||
const checkPoint = searchResult.geometry.location | ||
const currentPoint = this.coordinates | ||
if(arePointsNear(checkPoint, currentPoint, radius)){ | ||
placeResults.push(searchResult) | ||
} | ||
}) | ||
return placeResults | ||
} | ||
} | ||
|
||
module.exports = Coordinates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const hotelSearch = require('../models/hotel') | ||
const Booking = require('../models/booking') | ||
|
||
class Hotel{ | ||
constructor() { | ||
|
||
} | ||
|
||
async searchHotel(){ | ||
const hotels = await hotelSearch.find().getNearbyHotels(33.906528,73.393692,15000) | ||
const hotelIdArray = hotels.map((hotelObject)=>( | ||
hotelObject._id | ||
)) | ||
const available = await Booking.find({ | ||
guestlimit: {$gt: 2}, | ||
price: {$gte: 0, $lte: 10000}, | ||
// bookings: { | ||
// $not: { | ||
// $elemMatch: {from: {$lt: req.query.to.substring(0,10)}, to: {$gt: req.query.from.substring(0,10)}} | ||
// } | ||
// } | ||
}).where('hotelid').in(hotelIdArray).exec(); | ||
return {leength: available.length} | ||
} | ||
} | ||
|
||
module.exports = Hotel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const Coordinates = require('./Coordinates') | ||
|
||
const availableInterests = { | ||
sightseeing: { | ||
searchQuery: 'sightseeing', | ||
searchType: 'tourist_attraction', | ||
validTypes: ['tourist_attraction'], | ||
invalidTypes: [], | ||
}, | ||
hiking: { | ||
searchQuery: 'hiking trails', | ||
searchType: '', | ||
validTypes: ['point_of_interest', 'park'], | ||
invalidTypes: ['tourist_attraction'] | ||
}, | ||
shopping: { | ||
searchQuery: 'shopping mall', | ||
searchType: 'shopping_mall', | ||
validTypes: ['shopping_mall'], | ||
invalidTypes: [], | ||
}, | ||
boating: { | ||
searchQuery: 'boating', | ||
searchType: 'point_of_interest', | ||
validTypes: ['point_of_interest'], | ||
invalidTypes: ['travel_agency', 'mosque'], | ||
}, | ||
historical: { | ||
searchQuery: 'historical', | ||
searchType: '', | ||
validTypes: ['point_of_interest'], | ||
invalidTypes: [], | ||
}, | ||
entertainment: { | ||
searchQuery: 'entertainment', | ||
searchType: '', | ||
validTypes: ['point_of_interest'], | ||
invalidTypes: [], | ||
}, | ||
wildlife: { | ||
searchQuery: 'zoo', | ||
searchType: '', | ||
validTypes: ['zoo'], | ||
invalidTypes: [], | ||
}, | ||
museums: { | ||
searchQuery: 'museums', | ||
searchType: 'museum', | ||
validTypes: ['museum'], | ||
invalidTypes: [], | ||
}, | ||
lakes: { | ||
searchQuery: 'lakes', | ||
searchType: '', | ||
validTypes: ['natural_feature', 'park'], | ||
invalidTypes: [] | ||
} | ||
} | ||
|
||
class Interests{ | ||
|
||
constructor(hobbies) { | ||
this.hobbies = hobbies | ||
} | ||
|
||
async getPlaceRecommendations(coordinates){ | ||
const coordinate = new Coordinates(coordinates) | ||
const recommendedPlaces = [] | ||
// Find places of interest according to each hobby | ||
for(const hobby of this.hobbies){ | ||
const places = await coordinate.getNearbyPlaces(availableInterests[hobby].searchQuery, | ||
15, availableInterests[hobby].searchType) | ||
const filteredPlaces = [] | ||
places.forEach((place, index)=>{ | ||
// Filter out places according to relevant place types. | ||
if(place.types.some(r=> availableInterests[hobby].validTypes.indexOf(r) >= 0) && | ||
!place.types.some(r=> availableInterests[hobby].invalidTypes.indexOf(r) >= 0) && | ||
place.user_ratings_total > 3 && place.rating>2){ | ||
filteredPlaces.push({...place, score: place.user_ratings_total*place.rating*(1/(index+1)), matches: hobby}) //Calculate places score by total ratings * rating. | ||
} | ||
}) | ||
recommendedPlaces.push(...filteredPlaces) | ||
} | ||
// Sort Places according to their score. | ||
recommendedPlaces.sort((a, b)=>{ | ||
return b.score - a.score | ||
}) | ||
return recommendedPlaces | ||
} | ||
} | ||
|
||
module.exports = Interests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const puppeteer = require('puppeteer') | ||
const ResourceConstants = require('../models/resourceConstants') | ||
|
||
class Resources{ | ||
|
||
constructor() { | ||
} | ||
|
||
async updatePetrolPrice () { | ||
const browser = await puppeteer.launch() | ||
const page = await browser.newPage() | ||
await page.goto('https://psopk.com/en/product-and-services/product-prices/pol') | ||
|
||
const [el] = await page.$x('/html/body/div[2]/div[4]/div/div/div/div[2]/div/div[1]/table/tbody/tr[2]/td[2]') | ||
const src = await el.getProperty('textContent') | ||
const srcText = await src.jsonValue() | ||
await ResourceConstants.updateOne( { resourceName : 'petrolPrice'}, {resourceName: 'petrolPrice', value : srcText, updatedAt: new Date() }, { upsert : true }) | ||
} | ||
|
||
async getPetrolPrices () { | ||
const price = await ResourceConstants.findOne({ resourceName: 'petrolPrice' }, 'value').exec(); | ||
return parseFloat(price.value) | ||
} | ||
} | ||
|
||
module.exports = Resources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
let mongoose = require('mongoose'); | ||
let Schema = mongoose.Schema; | ||
|
||
let resourceConstants = new Schema({ | ||
resourceName: { | ||
type: 'String', | ||
}, | ||
value: { | ||
type: 'String' | ||
}, | ||
updatedAt:{ | ||
type: 'String' | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('resourceSources', resourceConstants); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.