Skip to content

Commit

Permalink
Distance calculations, Fuel prices scraping+updating and local places…
Browse files Browse the repository at this point in the history
… to visit added
  • Loading branch information
AnnasSalman committed Dec 6, 2020
1 parent d5d4391 commit 75fcfec
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 80 deletions.
17 changes: 17 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ var passport = require('passport');
var config = require('./config');
var cors = require('cors');
var authenticate = require('./middlewares/authenticate');
var CronJob = require('cron').CronJob;
var Resources = require('./classes/Resources')

const ResourceUpdates = new Resources()

const updateFuelPrices = new CronJob('00 00 00 * * *', async () => {
try{
await ResourceUpdates.updatePetrolPrice()
console.log('Updated Petrol Price')
}
catch(e){
console.log('error updating petrol prices')
}
}, null, true, 'Asia/Karachi');
updateFuelPrices.start();

if (process.env.NODE_ENV !== 'production') {
require('dotenv').config({path: path.resolve( __dirname,'secrets.env')});
Expand Down Expand Up @@ -46,6 +61,8 @@ app.use(function(req, res, next) {
next(createError(404));
});



// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
Expand Down
42 changes: 42 additions & 0 deletions classes/Coordinates.js
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
27 changes: 27 additions & 0 deletions classes/Hotel.js
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
92 changes: 92 additions & 0 deletions classes/Interests.js
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
26 changes: 26 additions & 0 deletions classes/Resources.js
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
15 changes: 14 additions & 1 deletion classes/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ class Route{

//set of string coordinates separated by |
//33.693852,73.065305|33.591368,73.053589|33.916725,73.396740|34.072142,73.386269
//First one are the source coordinates
//First ones are the source coordinates
constructor(coordinates) {
this._coordinates = coordinates
console.log(coordinates)
}

async getDistance(){
const distance = await axios.request({
url:'https://maps.googleapis.com/maps/api/distancematrix/json',
method: 'get',
params: {
origins: this._coordinates,
destinations: this._coordinates,
key: process.env.mapsKey
}
})
return distance.data.rows[0].elements[1].distance.value
}

async calculateShortestTrip(){
try{
const distances = await axios.request({
Expand Down
16 changes: 16 additions & 0 deletions models/resourceConstants.js
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);
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"axios": "^0.20.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"cron": "^1.8.2",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"expo-secure-store": "^9.0.1",
Expand All @@ -27,6 +28,7 @@
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.0.1",
"path-to-regexp": "^6.1.0"
"path-to-regexp": "^6.1.0",
"puppeteer": "^5.5.0"
}
}
Loading

0 comments on commit 75fcfec

Please sign in to comment.