49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
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
|
|
}
|