commit 297fe76f16fa0fc1a302035c223b52b3a4a61189 Author: Ben Date: Thu Oct 15 22:33:34 2020 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8ffe08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package-lock.json + diff --git a/index.js b/index.js new file mode 100644 index 0000000..e474b55 --- /dev/null +++ b/index.js @@ -0,0 +1,48 @@ +const fs = require('fs'); +const got = require('got'); +const jsdom = require('jsdom'); + +const today = new Date(); +const tomorrow = new Date(today); +tomorrow.setDate(today.getDate() + 1); + +console.log(`QHM Portsmouth Shipping Movements for ${tomorrow.getDate()}/${tomorrow.getMonth() + 1}/${tomorrow.getFullYear()}`); + +const URL = `https://www.royalnavy.mod.uk/qhm/portsmouth/shipping-movements/daily-movements?date=${tomorrow.getDate()}/${tomorrow.getMonth() + 1}/${tomorrow.getFullYear()}` + +async function main() +{ + const res = await got(URL); + const dom = new jsdom.JSDOM(res.body); + + const table = dom.window.document.getElementsByClassName('qhm-shipping-movements')[0]; + + let output = []; + + for (let i = 0, row; row = table.rows[i]; i++) + { + // Find vessel name + if (row.cells[2].innerHTML.includes('MV')) continue; + if (row.cells[2].innerHTML.includes('CLOSE')) continue; + if (row.cells[2].innerHTML.includes('OPEN')) continue; + + let out = { + time: row.cells[1].innerHTML.trim().replace(/ +(?= )/g,''), + vessel: row.cells[2].innerHTML.trim().replace(/ +(?= )/g,''), + from: row.cells[3].innerHTML.trim().replace(/ +(?= )/g,''), + to: row.cells[4].innerHTML.trim().replace(/ +(?= )/g,''), + remarks: row.cells[6].innerHTML.trim().replace(/ +(?= )/g,'') + } + + output.push(out); + } + + // format output + for (o of output) + { + console.log(`${o.vessel} movement at ${o.time}, from ${o.from} to ${o.to}`); + } + +} + +main(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..4da60e3 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "qhmscraper", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Ben Kyd (https://benkyd.co.uk)", + "license": "MIT", + "dependencies": { + "got": "^11.7.0", + "jsdom": "^16.4.0" + } +}