diff --git a/backend/models/getPlace.js b/backend/models/getPlace.js index 8846c94..81cc74b 100644 --- a/backend/models/getPlace.js +++ b/backend/models/getPlace.js @@ -1,12 +1,13 @@ const axios = require("axios") const getPlacePhoto = require("./getPlacePhoto.js") +const fields = "photos,place_id,name,rating,geometry" + module.exports = async (q) => { const res = await axios.get( - `https://maps.googleapis.com/maps/api/place/findplacefromtext/json?inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&input=${q}&key=${process.env.GOOGLE_CLOUD_APIS}`) - console.log(res.data) + `https://maps.googleapis.com/maps/api/place/findplacefromtext/json?inputtype=textquery&fields=${fields}&input=${q}&key=${process.env.GOOGLE_CLOUD_APIS}`) - // Photo url is not returned since it overuses Google Place API + // Photo url is not returned by default since it overuses Google Place API /* for (let candidate of res.data.candidates) { for (let photo of candidate.photos) { diff --git a/backend/models/getPlaceNearby.js b/backend/models/getPlaceNearby.js index 1f370e2..5df760a 100644 --- a/backend/models/getPlaceNearby.js +++ b/backend/models/getPlaceNearby.js @@ -3,12 +3,13 @@ const getPlacePhoto = require("./getPlacePhoto.js") const radius = 20000 const rankby = "prominence" +const types = "tourist_attraction" module.exports = async (lat, lng) => { const res = await axios.get( - `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},${lng}&radius=${radius}&rankby=${rankby}&key=${process.env.GOOGLE_CLOUD_APIS}` + `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},${lng}&radius=${radius}&type=${types}&rankby=${rankby}&key=${process.env.GOOGLE_CLOUD_APIS}` ); - // Photo url is not returned since it overuses Google Place API + // Photo url is not returned by default since it overuses Google Place API /* for (let result of res.data.results) { for (let photo of result.photos) { diff --git a/backend/models/getRegionById.js b/backend/models/getRegionById.js index 07cb2a1..29677d7 100644 --- a/backend/models/getRegionById.js +++ b/backend/models/getRegionById.js @@ -6,6 +6,8 @@ module.exports = async (dbConn, id) => { regions.region AS name, countries.country AS country, regions.description AS description, + regions.lon AS lon, + regions.lat AS lat, rcma.temperature_mean AS temperature_mean, rcma.temperature_mean_min AS temperature_mean_min, rcma.temperature_mean_max AS temperature_mean_max, @@ -54,6 +56,7 @@ module.exports = async (dbConn, id) => { region.sun_hours = arrayFormatting(region.sun_hours); region.humidity = arrayFormatting(region.humidity); + const emptyArr = Array.from({length: 12}, () => null) if (region.avg_price_relative === null) region.avg_price_relative = emptyArr if (region.temperature_mean === null) region.temperature_mean = emptyArr diff --git a/backend/models/getRegionNearbyById.js b/backend/models/getRegionNearbyById.js new file mode 100644 index 0000000..ee7ead8 --- /dev/null +++ b/backend/models/getRegionNearbyById.js @@ -0,0 +1,21 @@ +const arrayFormatting = require("../util/databaseArrayFormatting.js") + +module.exports = async (dbConn, id) => { + const region_nearby = await dbConn.query( + `SELECT id as place_id, + region_id as region_id, + name as place_name, + lon as lon, + lat as lat, + rating as rating, + vicinity as vicinity, + photo_reference as photo_reference, + img_url as img_url + FROM regions_nearby + WHERE region_id = ?`, + [id] + ); + + return region_nearby; +}; + diff --git a/backend/models/getRegions.js b/backend/models/getRegions.js index 1b47e67..7ceca88 100644 --- a/backend/models/getRegions.js +++ b/backend/models/getRegions.js @@ -7,6 +7,8 @@ module.exports = async (dbConn) => { regions.region AS name, countries.country AS country, regions.description AS description, + regions.lon AS lon, + regions.lat AS lat, rcma.temperature_mean AS temperature_mean, rcma.temperature_mean_min AS temperature_mean_min, rcma.temperature_mean_max AS temperature_mean_max, diff --git a/backend/models/handleRegionLonLat.js b/backend/models/handleRegionLonLat.js new file mode 100644 index 0000000..bfdd374 --- /dev/null +++ b/backend/models/handleRegionLonLat.js @@ -0,0 +1,34 @@ +const axios = require("axios") +const getRegions = require("../models/getRegions.js") + +const fields = "geometry" + +module.exports = async (dbConn) => { + const regions = await getRegions(dbConn) + + for (let region of regions) { + try { + const q = region.name + + const place = await axios.get( + `https://maps.googleapis.com/maps/api/place/findplacefromtext/json?inputtype=textquery&fields=geometry&input=${q}&key=${process.env.GOOGLE_CLOUD_APIS}`) + + const region_id = region.region_id + const lon = parseFloat(place.data.candidates[0].geometry.location.lng) + const lat = parseFloat(place.data.candidates[0].geometry.location.lat) + + await dbConn.query( + `UPDATE regions + SET lon=${lon}, lat=${lat} + WHERE id = ${region_id}` + ); + + console.log("Updating coordinates for region: ", region_id, q, " ", "lon:", lon, "lat", lat) + } catch (e) { + console.log(e) + } + } + + const res = "lon lat update finished" + return res +} diff --git a/backend/models/handleUpdateRegionNearby.js b/backend/models/handleUpdateRegionNearby.js new file mode 100644 index 0000000..0f931c0 --- /dev/null +++ b/backend/models/handleUpdateRegionNearby.js @@ -0,0 +1,48 @@ +const axios = require("axios") +const getRegions = require("../models/getRegions.js") +const getPlaceNearby = require("../models/getPlaceNearby.js") + +module.exports = async (dbConn) => { + const regions = await getRegions(dbConn) + + for (let region of regions) { + try { + const region_id = region.region_id + const region_lon = region.lon + const region_lat = region.lat + + console.log("Updating nearby for region: ", region_id, region.name) + + const places = await getPlaceNearby(region_lat, region_lon) + + for (let result of places.results) { + const name = result.name + const rating = result.rating === undefined ? null : result.rating + const lon = result.geometry.location.lng + const lat = result.geometry.location.lat + const photo_ref = result.photos[0].photo_reference + const vicinity = result.vicinity + + console.log("# New tourist attraction:", region_id, region.name, name) + + await dbConn.query( + `INSERT INTO regions_nearby + (region_id,name,lon,lat,rating,vicinity,photo_reference) + VALUES + (${region_id},"${name}",${lon},${lat},${rating},"${vicinity}","${photo_ref}") + ON DUPLICATE KEY UPDATE + lon = ${lon}, + lat = ${lat}, + rating = ${rating}, + vicinity = "${vicinity}", + photo_reference = "${photo_ref}"` + ); + } + } catch (e) { + console.log(e) + } + } + + const res = "region nearby update finished" + return res +} diff --git a/backend/models/handleUpdateRegionNearbyById.js b/backend/models/handleUpdateRegionNearbyById.js new file mode 100644 index 0000000..d946993 --- /dev/null +++ b/backend/models/handleUpdateRegionNearbyById.js @@ -0,0 +1,51 @@ +const axios = require("axios") +const getRegionById = require("../models/getRegionById.js") +const getPlaceNearby = require("../models/getPlaceNearby.js") + +module.exports = async (dbConn, id) => { + const region = await getRegionById(dbConn, id) + + try { + const region_id = region.region_id + const region_lon = region.lon + const region_lat = region.lat + + console.log("Updating nearby for region: ", region_id, region.name) + + const places = await getPlaceNearby(region_lat, region_lon) + + for (let result of places.results) { + try { + const name = result.name + const rating = result.rating === undefined ? null : result.rating + const lon = result.geometry.location.lng + const lat = result.geometry.location.lat + const photo_ref = result.photos[0].photo_reference + const vicinity = result.vicinity + + console.log("# New tourist attraction:", region_id, region.name, name) + + await dbConn.query( + `INSERT INTO regions_nearby + (region_id,name,lon,lat,rating,vicinity,photo_reference) + VALUES + (${region_id},"${name}",${lon},${lat},${rating},"${vicinity}","${photo_ref}") + ON DUPLICATE KEY UPDATE + lon = ${lon}, + lat = ${lat}, + rating = ${rating}, + vicinity = "${vicinity}", + photo_reference = "${photo_ref}"` + ); + } catch (e) { + console.log(e) + } + } + } catch (e) { + console.log(e) + } + + + const res = "region nearby by id update finished" + return res +} diff --git a/backend/models/handleUpdateRegionNearbyImgUrl.js b/backend/models/handleUpdateRegionNearbyImgUrl.js new file mode 100644 index 0000000..82f1d74 --- /dev/null +++ b/backend/models/handleUpdateRegionNearbyImgUrl.js @@ -0,0 +1,32 @@ +const getRegionNearbyById = require("../models/getRegionNearbyById.js") +const getPlacePhoto = require("../models/getPlacePhoto.js") + +module.exports = async (dbConn) => { + try { + const region_ids = await dbConn.query(` + SELECT distinct region_id + FROM regions_nearby + ORDER BY region_id`) + + + for (let region_id of region_ids) { + const nearby = await getRegionNearbyById(dbConn, region_id.region_id) + + for (let place of nearby) { + const url = await getPlacePhoto(place.photo_reference) + + console.log("# Setting image Url:", region_id, place.place_name, url) + + await dbConn.query(` + UPDATE regions_nearby + SET img_url = "${url}" + WHERE id = ${place.place_id}`) + } + } + } catch (e) { + console.log(e) + } + + const res = "region nearby img url update finished" + return res +} diff --git a/backend/routes/regions.js b/backend/routes/regions.js index a76dd3d..9a8ecee 100644 --- a/backend/routes/regions.js +++ b/backend/routes/regions.js @@ -1,6 +1,11 @@ const router = require("express").Router(); const getRegions = require("../models/getRegions.js"); const getRegionById = require("../models/getRegionById.js"); +const handleRegionLonLat = require("../models/handleRegionLonLat.js") +const getRegionNearbyById = require("../models/getRegionNearbyById.js") +const handleUpdateRegionNearby = require("../models/handleUpdateRegionNearby.js") +const handleUpdateRegionNearbyById = require("../models/handleUpdateRegionNearbyById.js") +const handleUpdateRegionNearbyImgUrl = require("../models/handleUpdateRegionNearbyImgUrl.js") const path = require("path"); const fs = require("fs"); const _ = require('lodash') @@ -14,6 +19,7 @@ module.exports = dbConn => { res.json(data); } }); + router.get('/api/v1/regions/:id/image', (req, res) => { if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) { res.sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`)) @@ -21,9 +27,30 @@ module.exports = dbConn => { res.sendFile(path.join(__dirname, `../data/regions/images/x.png`)) } }) + router.get("/api/v1/regions/:id", async (req, res) => { const id = req.params.id; res.json(await getRegionById(dbConn, id)) }); + + router.patch("/api/v1/regions/lonlat/update", async (req,res) => { + res.json(await handleRegionLonLat(dbConn)) + }); + + router.get("/api/v1/regions/nearby/:id", async (req,res) => { + res.json(await getRegionNearbyById(dbConn,req.params.id)) + }); + + router.patch("/api/v1/regions/nearby/update", async (req,res) => { + res.json(await handleUpdateRegionNearby(dbConn)) + }); + + router.patch("/api/v1/regions/nearby/update/:id", async (req,res) => { + res.json(await handleUpdateRegionNearbyById(dbConn, req.params.id)) + }); + + router.patch("/api/v1/regions/nearby/imgurl/update", async (req,res) => { + res.json(await handleUpdateRegionNearbyImgUrl(dbConn)) + }); return router; };