mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(vessels): a selected vessel shows its Open Vessel Photos photo and MMSI
Selecting a vessel asks Open Vessel Photos (a free, keyless API) for its
photo by MMSI, once per MMSI. The CONTACTS view shows it under the vessel's
name and MMSI with the photographer, the photo's open licence and a link to
the vessel's page; only images on Open Vessel Photos' own host are shown, and
an answer about another MMSI is no photo. The vessel source exposes the lookup
as getEnrichment({kind: 'photo'}) and fetchOpenVesselPhoto for applications
with their own vessel source.
This commit is contained in:
1 parent
c02ede3bb4
commit
347e77d1f6
31 files changed
+591
-19
No files matched your search
@@ -141,6 +141,13 @@ export const DATA_CREDITS = [
|
||||
'<a href="https://www.planespotters.net" target="_blank" rel="noopener">Planespotters.net</a> ' +
|
||||
'· each photo is © its photographer, credited beside it',
|
||||
},
|
||||
{
|
||||
key: 'openvesselphotos',
|
||||
html:
|
||||
'Vessel photos: ' +
|
||||
'<a href="https://openvesselphotos.org" target="_blank" rel="noopener">Open Vessel Photos</a> ' +
|
||||
"· each photo keeps its author's open licence, credited beside it",
|
||||
},
|
||||
{
|
||||
key: 'aisstream',
|
||||
html:
|
||||
|
||||
@@ -51,6 +51,13 @@ test('Planespotters is credited with a plain followable link', () => {
|
||||
assert.doesNotMatch(credit.html, /nofollow/);
|
||||
});
|
||||
|
||||
test('Open Vessel Photos is credited with a plain followable link', () => {
|
||||
const credit = DATA_CREDITS.find((entry) => entry.key === 'openvesselphotos');
|
||||
assert.ok(credit, 'the selected vessel photos come from Open Vessel Photos');
|
||||
assert.match(credit.html, /href="https:\/\/openvesselphotos\.org"/);
|
||||
assert.doesNotMatch(credit.html, /nofollow/);
|
||||
});
|
||||
|
||||
test('the UCS Satellite Database is acknowledged with its version and webpage', () => {
|
||||
// UCS database manual, section 3: references name the version (the Excel
|
||||
// file's name) and link www.ucsusa.org/satellite_database.
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @module openVesselPhoto
|
||||
* @description Reads an Open Vessel Photos answer into the one photo every
|
||||
* surface shows. Open Vessel Photos (https://openvesselphotos.org) serves a
|
||||
* free, keyless JSON and image API by MMSI. Every photo keeps the open licence
|
||||
* its author chose (CC BY, CC BY-SA, CC0 or public domain), so it is shown
|
||||
* with its credit and licence and links back to the vessel's page. A photo
|
||||
* whose image is not on Open Vessel Photos' own host, or whose answer names
|
||||
* no licence, is "no photo": nothing arbitrary is ever shown or linked.
|
||||
*
|
||||
* Accepts the upstream answer (`GET https://openvesselphotos.org/v1/mmsi/{mmsi}`,
|
||||
* `{mmsi, photos: [...]}`) and an already-read photo
|
||||
* (`{found, src, width, height, photographer, licence, link}`).
|
||||
* Pure: no fetch, no DOM.
|
||||
*/
|
||||
|
||||
/** The only host a photo may come from or link to. */
|
||||
const HOST = 'openvesselphotos.org';
|
||||
/** The API's per-vessel answer. */
|
||||
const API = `https://${HOST}/v1/mmsi/`;
|
||||
/** The image width asked for: sharp in the panel on a 2× screen, ~60 KB. */
|
||||
const IMAGE_SIZE = '800';
|
||||
|
||||
const NO_PHOTO = Object.freeze({ found: false });
|
||||
|
||||
/** A vessel's MMSI: nine digits. */
|
||||
export const MMSI = /^\d{9}$/;
|
||||
|
||||
/** The API answer for one MMSI. */
|
||||
export function openVesselPhotosUrl(mmsi) {
|
||||
return API + mmsi;
|
||||
}
|
||||
|
||||
/** The vessel's page on Open Vessel Photos. */
|
||||
function vesselPage(mmsi) {
|
||||
return `https://${HOST}/vessels/mmsi/${mmsi}`;
|
||||
}
|
||||
|
||||
/** The parsed `https:` URL on the Open Vessel Photos host, or null. */
|
||||
function ownUrl(value) {
|
||||
if (typeof value !== 'string' || !value) return null;
|
||||
let url;
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return url.protocol === 'https:' &&
|
||||
!url.username &&
|
||||
!url.password &&
|
||||
!url.port &&
|
||||
url.hostname === HOST
|
||||
? url
|
||||
: null;
|
||||
}
|
||||
|
||||
function dimension(value) {
|
||||
const number = Number(value);
|
||||
return value != null && Number.isFinite(number) && number > 0
|
||||
? Math.round(number)
|
||||
: null;
|
||||
}
|
||||
|
||||
function text(value) {
|
||||
return typeof value === 'string' ? value.trim() : '';
|
||||
}
|
||||
|
||||
/** One upstream photo of the vessel `mmsi`, checked. */
|
||||
function checkedUpstream(photo, mmsi) {
|
||||
if (!photo || typeof photo !== 'object') return null;
|
||||
const image = ownUrl(photo.urls?.mmsi);
|
||||
const licence = text(photo.licenceName) || text(photo.licence);
|
||||
if (!image || !licence) return null;
|
||||
image.search = '';
|
||||
image.hash = '';
|
||||
image.searchParams.set('size', IMAGE_SIZE);
|
||||
// The answer gives the full image's size; the 800 px one keeps its shape.
|
||||
const width = dimension(photo.width);
|
||||
const height = dimension(photo.height);
|
||||
const scale =
|
||||
width && width > Number(IMAGE_SIZE) ? Number(IMAGE_SIZE) / width : 1;
|
||||
return {
|
||||
found: true,
|
||||
src: image.href,
|
||||
width: width ? Math.round(width * scale) : null,
|
||||
height: height ? Math.round(height * scale) : null,
|
||||
photographer: text(photo.credit),
|
||||
licence,
|
||||
link: vesselPage(mmsi),
|
||||
};
|
||||
}
|
||||
|
||||
/** An already-read photo, checked again. */
|
||||
function checkedRead(photo) {
|
||||
const image = ownUrl(photo.src);
|
||||
const page = ownUrl(photo.link);
|
||||
const licence = text(photo.licence);
|
||||
if (!image || !page || !licence) return null;
|
||||
return {
|
||||
found: true,
|
||||
src: image.href,
|
||||
width: dimension(photo.width),
|
||||
height: dimension(photo.height),
|
||||
photographer: text(photo.photographer),
|
||||
licence,
|
||||
link: page.href,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an Open Vessel Photos answer into the first photo that passes the
|
||||
* checks.
|
||||
* @param {*} payload Upstream answer or already-read photo.
|
||||
* @param {?string} [requestedMmsi] The MMSI asked for; an upstream answer
|
||||
* naming another is no photo.
|
||||
* @returns {{found: true, src: string, width: ?number, height: ?number,
|
||||
* photographer: string, licence: string, link: string} | {found: false}}
|
||||
*/
|
||||
export function parseOpenVesselPhoto(payload, requestedMmsi = null) {
|
||||
if (!payload || typeof payload !== 'object') return NO_PHOTO;
|
||||
if (Array.isArray(payload.photos)) {
|
||||
const mmsi = String(payload.mmsi ?? '');
|
||||
if (!MMSI.test(mmsi)) return NO_PHOTO;
|
||||
// An answer about another vessel is no photo of this one.
|
||||
if (requestedMmsi != null && mmsi !== String(requestedMmsi))
|
||||
return NO_PHOTO;
|
||||
for (const photo of payload.photos) {
|
||||
const checked = checkedUpstream(photo, mmsi);
|
||||
if (checked) return checked;
|
||||
}
|
||||
return NO_PHOTO;
|
||||
}
|
||||
if (payload.found !== true) return NO_PHOTO;
|
||||
return checkedRead(payload) || NO_PHOTO;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// src/data/openVesselPhoto.test.mjs — an Open Vessel Photos answer is read
|
||||
// into one credited, licensed photo, and nothing outside Open Vessel Photos'
|
||||
// own host is ever shown or linked.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
openVesselPhotosUrl,
|
||||
parseOpenVesselPhoto,
|
||||
} from './openVesselPhoto.js';
|
||||
|
||||
const UPSTREAM = Object.freeze({
|
||||
mmsi: '366999709',
|
||||
name: '01 USCGC Sockeye WPB87337',
|
||||
photos: [
|
||||
{
|
||||
n: 1,
|
||||
urls: {
|
||||
cfr: null,
|
||||
mmsi: 'https://openvesselphotos.org/v1/mmsi/366999709/photos/1.jpg',
|
||||
},
|
||||
credit: 'Gordon Leggett',
|
||||
licence: 'CC-BY-4.0',
|
||||
licenceName: 'CC BY 4.0',
|
||||
width: 1600,
|
||||
height: 1200,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const PHOTO = Object.freeze({
|
||||
found: true,
|
||||
src: 'https://openvesselphotos.org/v1/mmsi/366999709/photos/1.jpg?size=800',
|
||||
width: 800,
|
||||
height: 600,
|
||||
photographer: 'Gordon Leggett',
|
||||
licence: 'CC BY 4.0',
|
||||
link: 'https://openvesselphotos.org/vessels/mmsi/366999709',
|
||||
});
|
||||
|
||||
const withPhoto = (overrides) => ({
|
||||
...UPSTREAM,
|
||||
photos: [{ ...UPSTREAM.photos[0], ...overrides }],
|
||||
});
|
||||
|
||||
test('the upstream answer maps to the 800 px image, its credit, licence and page', () => {
|
||||
assert.deepEqual(parseOpenVesselPhoto(UPSTREAM), PHOTO);
|
||||
assert.equal(
|
||||
openVesselPhotosUrl('366999709'),
|
||||
'https://openvesselphotos.org/v1/mmsi/366999709',
|
||||
);
|
||||
});
|
||||
|
||||
test('an image narrower than 800 px keeps its own size', () => {
|
||||
const photo = parseOpenVesselPhoto(withPhoto({ width: 640, height: 480 }));
|
||||
assert.equal(photo.width, 640);
|
||||
assert.equal(photo.height, 480);
|
||||
});
|
||||
|
||||
test('an already-read photo passes through its checks again', () => {
|
||||
assert.deepEqual(parseOpenVesselPhoto(PHOTO), PHOTO);
|
||||
assert.deepEqual(parseOpenVesselPhoto({ ...PHOTO, licence: '' }), {
|
||||
found: false,
|
||||
});
|
||||
});
|
||||
|
||||
test('a photo off the Open Vessel Photos host, or without a licence, is no photo', () => {
|
||||
for (const overrides of [
|
||||
{ urls: { mmsi: 'https://example.com/1.jpg' } },
|
||||
{
|
||||
urls: {
|
||||
mmsi: 'http://openvesselphotos.org/v1/mmsi/366999709/photos/1.jpg',
|
||||
},
|
||||
},
|
||||
{ urls: { mmsi: 'https://openvesselphotos.org.evil.com/1.jpg' } },
|
||||
{ urls: { mmsi: 'https://[email protected]/1.jpg' } },
|
||||
{ licence: '', licenceName: '' },
|
||||
])
|
||||
assert.deepEqual(parseOpenVesselPhoto(withPhoto(overrides)), {
|
||||
found: false,
|
||||
});
|
||||
assert.deepEqual(
|
||||
parseOpenVesselPhoto({ ...PHOTO, link: 'https://example.com/' }),
|
||||
{ found: false },
|
||||
);
|
||||
});
|
||||
|
||||
test('a vessel without photos, or an answer with a bad MMSI, is no photo', () => {
|
||||
assert.deepEqual(parseOpenVesselPhoto({ ...UPSTREAM, photos: [] }), {
|
||||
found: false,
|
||||
});
|
||||
assert.deepEqual(parseOpenVesselPhoto({ ...UPSTREAM, mmsi: '12/../x' }), {
|
||||
found: false,
|
||||
});
|
||||
assert.deepEqual(parseOpenVesselPhoto(null), { found: false });
|
||||
});
|
||||
|
||||
test('the next photo stands in when the first fails its checks', () => {
|
||||
const second = {
|
||||
...UPSTREAM.photos[0],
|
||||
n: 2,
|
||||
urls: {
|
||||
mmsi: 'https://openvesselphotos.org/v1/mmsi/366999709/photos/2.jpg',
|
||||
},
|
||||
};
|
||||
const photo = parseOpenVesselPhoto({
|
||||
...UPSTREAM,
|
||||
photos: [{ ...UPSTREAM.photos[0], licenceName: '', licence: '' }, second],
|
||||
});
|
||||
assert.equal(
|
||||
photo.src,
|
||||
'https://openvesselphotos.org/v1/mmsi/366999709/photos/2.jpg?size=800',
|
||||
);
|
||||
});
|
||||
|
||||
test('an answer about another vessel than the one asked for is no photo', () => {
|
||||
assert.deepEqual(parseOpenVesselPhoto(UPSTREAM, '366999709'), PHOTO);
|
||||
assert.deepEqual(parseOpenVesselPhoto(UPSTREAM, '227000101'), {
|
||||
found: false,
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user