-
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.
added two maps scrapers (and postgis)
- Loading branch information
AV
committed
Apr 5, 2023
1 parent
963b90d
commit d912fd8
Showing
11 changed files
with
317 additions
and
20 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,49 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const db = require('../_utils/db.js'); | ||
|
||
// Load the GeoJSON file | ||
const geoJSONFile = path.resolve(__dirname, './history/visited-places-detailed.geojson'); | ||
const geoJSONData = JSON.parse(fs.readFileSync(geoJSONFile, 'utf-8')); | ||
|
||
// Schema, table and unique column names | ||
const schema = 'maps'; | ||
const tableName = 'google_maps_visited_places'; | ||
const uniqueColumns = ['time','location']; | ||
|
||
// Extract the data from the GeoJSON features and format to match the data format for saveData function | ||
const data = geoJSONData.features.map((feature) => { | ||
const coordinates = `POINT(${feature.properties.lon} ${feature.properties.lat})`; | ||
return [ | ||
{ | ||
name: 'time', | ||
value: feature.properties.from_date, | ||
type: 'TIMESTAMP WITH TIME ZONE', | ||
}, | ||
{ | ||
name: 'to_date', | ||
value: feature.properties.to_date, | ||
type: 'TIMESTAMP WITH TIME ZONE', | ||
}, | ||
{ | ||
name: 'location', | ||
value: feature.properties.location, | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'coordinates', | ||
value: coordinates, | ||
type: 'GEOGRAPHY(POINT, 4326)', | ||
}, | ||
]; | ||
}); | ||
|
||
// Save the data to the database | ||
(async () => { | ||
try { | ||
await db.saveData(schema, tableName, data, uniqueColumns); | ||
console.log('Data has been successfully saved to the database.'); | ||
} catch (error) { | ||
console.error('An error occurred while saving data to the database:', error); | ||
} | ||
})(); |
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,135 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const db = require('../_utils/db.js'); | ||
|
||
const schema = 'maps'; | ||
const tableName = 'raw_visited_places'; | ||
const uniqueColumns = ['time', 'coordinates']; | ||
|
||
app.use(express.json({ limit: '10mb' })); | ||
const serverPort = process.env.SCRAPER_MAPS_PORT; | ||
|
||
app.post('/maps', (req, res) => { | ||
handleMapsEndpoint(req, res); | ||
}); | ||
|
||
app.get('/', async (req, res) => { | ||
handleRootEndpoint(req, res); | ||
}); | ||
|
||
// Start server | ||
app.listen(serverPort, () => { | ||
console.log(`Server is running on port ${serverPort}`); | ||
}); | ||
|
||
// Endpoints handling functions | ||
async function handleRootEndpoint(req, res) { | ||
res.status(200).send('Hello from the Maps scraper!'); | ||
} | ||
|
||
async function handleMapsEndpoint(req, res) { | ||
console.log(JSON.stringify(req.body, null, 2)); | ||
res.json({ result: 'ok' }); | ||
|
||
// parse location json data | ||
const locations = req.body.locations; | ||
|
||
const data = []; | ||
locations.forEach((location) => { | ||
const coordinates = `POINT(${location.geometry.coordinates[0]} ${location.geometry.coordinates[1]})`; | ||
data.push([ | ||
{ | ||
name: 'time', | ||
value: location.properties.timestamp, | ||
type: 'TIMESTAMP WITH TIME ZONE', | ||
}, | ||
{ | ||
name: 'coordinates', | ||
value: coordinates, | ||
type: 'GEOGRAPHY(POINT, 4326)', | ||
}, | ||
{ | ||
name: 'altitude', | ||
value: location.properties.altitude, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'speed', | ||
value: location.properties.speed, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'horizontal_accuracy', | ||
value: location.properties.horizontal_accuracy, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'vertical_accuracy', | ||
value: location.properties.vertical_accuracy, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'motion', | ||
value: location.properties.motion.join(','), | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'pauses', | ||
value: location.properties.pauses, | ||
type: 'BOOLEAN', | ||
}, | ||
{ | ||
name: 'activity', | ||
value: location.properties.activity, | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'desired_accuracy', | ||
value: location.properties.desired_accuracy, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'deferred', | ||
value: location.properties.deferred, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'significant_change', | ||
value: location.properties.significant_change, | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'locations_in_payload', | ||
value: location.properties.locations_in_payload, | ||
type: 'INTEGER', | ||
}, | ||
{ | ||
name: 'battery_state', | ||
value: location.properties.battery_state, | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'battery_level', | ||
value: location.properties.battery_level, | ||
type: 'DOUBLE PRECISION', | ||
}, | ||
{ | ||
name: 'device_id', | ||
value: location.properties.device_id, | ||
type: 'TEXT', | ||
}, | ||
{ | ||
name: 'wifi', | ||
value: location.properties.wifi, | ||
type: 'TEXT', | ||
}, | ||
]); | ||
}); | ||
|
||
try { | ||
// save data to timescaledb | ||
await db.saveData(schema, tableName, data, uniqueColumns); | ||
} catch (error) { | ||
console.error('Error inserting location data', error); | ||
} | ||
} |
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,19 @@ | ||
{ | ||
"name": "superscraper-maps", | ||
"version": "1.0.0", | ||
"description": "", | ||
"scripts": { | ||
"start": "node maps-scraper.js", | ||
"clean": "rm -rf node_modules", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"axios": "^1.3.3", | ||
"dotenv": "^16.0.3", | ||
"express": "^4.18.2", | ||
"pg": "^8.9.0" | ||
} | ||
} | ||
|
Oops, something went wrong.