Merge branch 'feature/place' into 'develop'
Feature/place See merge request tjohn/cc-data!24
This commit is contained in:
commit
5b4ef3c794
@ -1,12 +1,13 @@
|
|||||||
const axios = require("axios")
|
const axios = require("axios")
|
||||||
const getPlacePhoto = require("./getPlacePhoto.js")
|
const getPlacePhoto = require("./getPlacePhoto.js")
|
||||||
|
|
||||||
|
const fields = "photos,place_id,name,rating,geometry"
|
||||||
|
|
||||||
module.exports = async (q) => {
|
module.exports = async (q) => {
|
||||||
const res = await axios.get(
|
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}`)
|
`https://maps.googleapis.com/maps/api/place/findplacefromtext/json?inputtype=textquery&fields=${fields}&input=${q}&key=${process.env.GOOGLE_CLOUD_APIS}`)
|
||||||
console.log(res.data)
|
|
||||||
|
|
||||||
// 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 candidate of res.data.candidates) {
|
||||||
for (let photo of candidate.photos) {
|
for (let photo of candidate.photos) {
|
||||||
|
|||||||
@ -3,12 +3,13 @@ const getPlacePhoto = require("./getPlacePhoto.js")
|
|||||||
|
|
||||||
const radius = 20000
|
const radius = 20000
|
||||||
const rankby = "prominence"
|
const rankby = "prominence"
|
||||||
|
const types = "tourist_attraction"
|
||||||
|
|
||||||
module.exports = async (lat, lng) => {
|
module.exports = async (lat, lng) => {
|
||||||
const res = await axios.get(
|
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 result of res.data.results) {
|
||||||
for (let photo of result.photos) {
|
for (let photo of result.photos) {
|
||||||
|
|||||||
@ -6,6 +6,8 @@ module.exports = async (dbConn, id) => {
|
|||||||
regions.region AS name,
|
regions.region AS name,
|
||||||
countries.country AS country,
|
countries.country AS country,
|
||||||
regions.description AS description,
|
regions.description AS description,
|
||||||
|
regions.lon AS lon,
|
||||||
|
regions.lat AS lat,
|
||||||
rcma.temperature_mean AS temperature_mean,
|
rcma.temperature_mean AS temperature_mean,
|
||||||
rcma.temperature_mean_min AS temperature_mean_min,
|
rcma.temperature_mean_min AS temperature_mean_min,
|
||||||
rcma.temperature_mean_max AS temperature_mean_max,
|
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.sun_hours = arrayFormatting(region.sun_hours);
|
||||||
region.humidity = arrayFormatting(region.humidity);
|
region.humidity = arrayFormatting(region.humidity);
|
||||||
|
|
||||||
|
|
||||||
const emptyArr = Array.from({length: 12}, () => null)
|
const emptyArr = Array.from({length: 12}, () => null)
|
||||||
if (region.avg_price_relative === null) region.avg_price_relative = emptyArr
|
if (region.avg_price_relative === null) region.avg_price_relative = emptyArr
|
||||||
if (region.temperature_mean === null) region.temperature_mean = emptyArr
|
if (region.temperature_mean === null) region.temperature_mean = emptyArr
|
||||||
|
|||||||
21
backend/models/getRegionNearbyById.js
Normal file
21
backend/models/getRegionNearbyById.js
Normal file
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
@ -7,6 +7,8 @@ module.exports = async (dbConn) => {
|
|||||||
regions.region AS name,
|
regions.region AS name,
|
||||||
countries.country AS country,
|
countries.country AS country,
|
||||||
regions.description AS description,
|
regions.description AS description,
|
||||||
|
regions.lon AS lon,
|
||||||
|
regions.lat AS lat,
|
||||||
rcma.temperature_mean AS temperature_mean,
|
rcma.temperature_mean AS temperature_mean,
|
||||||
rcma.temperature_mean_min AS temperature_mean_min,
|
rcma.temperature_mean_min AS temperature_mean_min,
|
||||||
rcma.temperature_mean_max AS temperature_mean_max,
|
rcma.temperature_mean_max AS temperature_mean_max,
|
||||||
|
|||||||
34
backend/models/handleRegionLonLat.js
Normal file
34
backend/models/handleRegionLonLat.js
Normal file
@ -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
|
||||||
|
}
|
||||||
48
backend/models/handleUpdateRegionNearby.js
Normal file
48
backend/models/handleUpdateRegionNearby.js
Normal file
@ -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
|
||||||
|
}
|
||||||
51
backend/models/handleUpdateRegionNearbyById.js
Normal file
51
backend/models/handleUpdateRegionNearbyById.js
Normal file
@ -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
|
||||||
|
}
|
||||||
32
backend/models/handleUpdateRegionNearbyImgUrl.js
Normal file
32
backend/models/handleUpdateRegionNearbyImgUrl.js
Normal file
@ -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
|
||||||
|
}
|
||||||
@ -1,6 +1,11 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
const getRegions = require("../models/getRegions.js");
|
const getRegions = require("../models/getRegions.js");
|
||||||
const getRegionById = require("../models/getRegionById.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 path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
@ -14,6 +19,7 @@ module.exports = dbConn => {
|
|||||||
res.json(data);
|
res.json(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/api/v1/regions/:id/image', (req, res) => {
|
router.get('/api/v1/regions/:id/image', (req, res) => {
|
||||||
if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) {
|
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`))
|
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`))
|
res.sendFile(path.join(__dirname, `../data/regions/images/x.png`))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get("/api/v1/regions/:id", async (req, res) => {
|
router.get("/api/v1/regions/:id", async (req, res) => {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
res.json(await getRegionById(dbConn, 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;
|
return router;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user