Skip to content

Instantly share code, notes, and snippets.

@aaronbrown1988
Created February 3, 2019 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronbrown1988/07e21842f0b0217f48b23ea38e0865ef to your computer and use it in GitHub Desktop.
Save aaronbrown1988/07e21842f0b0217f48b23ea38e0865ef to your computer and use it in GitHub Desktop.
WSPR spot service cloud Function
/**
* Super simple cloudfunction to catch WSPR spots and pass them on.
*/
var request = require('request')
const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
function validate(query) {
if (query == null || !query) {
return false
}
if (query.date == null || query.rcall == null) {
return false
}
if(query.function != "wspr") {
return false
}
return true
}
exports.wsprGET = (req, res) => {
if (validate(req.query)) {
console.log(JSON.stringify(req.query))
const key = datastore.key(['Spot']);
datastore.save({
key: key,
data: req.query
}, (err) => {
if (!err) {
// Record saved successfully.
request({
url: 'http://wsprnet.org/post?',
qs: req.query
}, (err, response, body) => {
if (err) {
res.status(500).send(err);
return console.log(err);
}
console.log(body.url);
console.log(body.explanation);
res.send(`${JSON.stringify(req.query)} -> ${JSON.stringify(response)}`);
});
} else {
res.status(500).send(JSON.stringify(err));
}
});
} else {
res.send(`${JSON.stringify(req.query)} is not valid`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment