Skip to content

Commit

Permalink
Added status, speed, and course fields to ship updater
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Sep 2, 2018
1 parent a4b8884 commit 443eb0e
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions scripts/ships.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env node

/**
* This script gathers current location, status, speed, and course data for all past
* and present SpaceX barges, support ships, and tugs, and updates it accordingly.
*/

const MongoClient = require('mongodb');
const cheerio = require('cheerio');
const request = require('request-promise-native');
Expand All @@ -8,6 +13,8 @@ const sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
};

// Created so we can use async await with requests, and
// to use async sleep function inside the IIFE
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index += 1) {
// Allow await for nested async functions
Expand All @@ -24,6 +31,7 @@ async function asyncForEach(array, callback) {
const col = db.collection('ship');
const data = await col.find({}).sort({ year_built: 1 });

// Gather individual ship mmsi numbers into array for requests
const id = [];
await data.forEach(ship => {
if (ship.mmsi != null) {
Expand All @@ -36,31 +44,54 @@ async function asyncForEach(array, callback) {
const result = await request(`https://www.marinetraffic.com/en/ais/details/ships/mmsi:${num}`);

const $ = cheerio.load(result);
const coordinates = $('#tabs-last-pos > div > div > div.table-cell.cell-full.collapse-768 > div:nth-child(4) > span:nth-child(2) > strong > a').text();

// Strip dividing slash and degrees symbol
// Get current lat/long and strip extra characters
// Raw output: 28.40871° / -80.59808°
let clean = coordinates.replace('°', '').replace('°', '');
clean = clean.split(' / ');
console.log(clean);
const coordinates = $('#tabs-last-pos > div > div > div.table-cell.cell-full.collapse-768 > div:nth-child(4) > span:nth-child(2) > strong > a').text();
const parsed_coordinates = coordinates.replace('°', '').replace('°', '').split(' / ');

// Get current ship status fx. Stopped, Moored, Underway...
// Raw output: Underway Using Engine
const ship_status = $('#tabs-last-pos > div > div > div.table-cell.cell-full.collapse-768 > div:nth-child(5) > span:nth-child(2) > strong').text();

// Get current ship speed and direction and strip extra characters
// Raw output: 17.2kn / 67°
// If the speed is zero, the course returns '-'. This is replaced with null for zero speeds
const direction = $('#tabs-last-pos > div > div > div.table-cell.cell-full.collapse-768 > div:nth-child(6) > span:nth-child(2) > strong').text();
const parsed_direction = direction.replace('kn', '').replace('°', '').split(' / ');
let ship_course;
if (parsed_direction[1] === '-') {
ship_course = null;
} else {
ship_course = parseFloat(parsed_direction[1]);
}

const update = {
latitude: parseFloat(clean[0]),
longitude: parseFloat(clean[1]),
latitude: parseFloat(parsed_coordinates[0]),
longitude: parseFloat(parsed_coordinates[1]),
status: ship_status,
speed: parseFloat(parsed_direction[0]),
course: ship_course,
};

console.log(`Updating ${num}...`);
await col.updateOne({ mmsi: num }, {
$set: {
'position.latitude': update.latitude,
'position.longitude': update.longitude,
status: update.status,
speed_kn: update.speed,
course_deg: update.course,
},
});
console.log('Updated');

// Wait 5 seconds between page fetches to be respectful of bandwidth
await sleep(5000);
});
};
await start();
console.log(`Updated ${id.length} ships`);
} catch (err) {
console.log(err.stack);
process.exit(1);
Expand Down

0 comments on commit 443eb0e

Please sign in to comment.