Skip to content

Added sanitize function to check the author's name on snow observations to insert into the database properly. #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ const importObservations = async function (observations) {
}
try {
await pgPool.query('BEGIN');
observations = observations.map(
(o) =>
`(ST_SetSRID(ST_MakePoint(${o.long}, ${o.lat}), 4326), '${o.id}', '${
o.author_name
}', ${o.depth}, TIMESTAMP '${o.timestamp.toISOString()}', '${
o.source
}', ${o.elevation})`
);
const query = `
INSERT INTO observations(location, id, author, depth, timestamp, source, elevation)
VALUES ${observations}
ON CONFLICT DO NOTHING
`;
await pgPool.query(query);
await observations.forEach(async (o) => {
const obArr = [
o.long,
o.lat,
o.id,
o.author_name,
o.depth,
o.timestamp.toISOString(),
o.source,
o.elevation
];
const query = `
INSERT INTO observations(location, id, author, depth, timestamp, source, elevation)
VALUES (ST_SetSRID(ST_MakePoint($1, $2),4326), $3, $4, $5, $6, $7, $8)
ON CONFLICT DO NOTHING
`;
await pgPool.query(query, obArr)
.catch(err => console.error(err.stack));
});
await pgPool.query('COMMIT');
} catch (error) {
await pgPool.query('ROLLBACK');
Expand Down
3 changes: 2 additions & 1 deletion src/import/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const MountainHub = require('./mountainhub');
const SnowPilot = require('./snowpilot');
const RegObs = require('./regobs');

// 604800000
const ONE_WEEK = 604800000;

const retrieveObservation = async function (
Expand All @@ -13,7 +14,7 @@ const retrieveObservation = async function (
try {
startDate =
parseDate(startDate) || new Date(new Date().getTime() - ONE_WEEK);
endDate = parseDate(endDate) || new Date();
endDate = parseDate(endDate) || new Date(new Date().getTime());
const rawData = await provider.rawData(startDate, endDate);
let data = rawData.map(provider.parseData).filter((x) => x);
data = await withElevation(data);
Expand Down
1 change: 1 addition & 0 deletions src/import/providers/mountainhub.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const parseData = (record) => {
return format;
}
catch (error) {
console.error(error);
return null;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/import/providers/regobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const parseData = (record) => {
return format;
}
catch (error) {
console.error(error);
return null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/import/providers/snowpilot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { generateId } = require("../utils")
const request = require('request-promise');
const parseString = require('xml2js').parseString;
const escape = require('pg-escape');

const LOGIN_URL = 'https://snowpilot.org/user/login'
const LOGIN_HEADERS = { 'User-Agent': 'script login', withCredentials:true }
Expand Down Expand Up @@ -58,6 +59,7 @@ const parseData = (record) => {
if (format.lat == 0 && format.long == 0) throw new Error("No Location Data");
return format;
} catch (error) {
console.error(error);
return null;
}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

validator@^13.5.2:
version "13.5.2"
resolved "https://registry.yarnpkg.com/validator/-/validator-13.5.2.tgz#c97ae63ed4224999fb6f42c91eaca9567fe69a46"
integrity sha512-mD45p0rvHVBlY2Zuy3F3ESIe1h5X58GPfAtslBjY7EtTqGquZTj+VX/J4RnHWN8FKq0C9WRVt1oWAcytWRuYLQ==

[email protected]:
version "1.10.0"
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"
Expand Down