mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +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
@@ -16,4 +16,5 @@ export {
|
||||
createFlightSource,
|
||||
createMilitarySource,
|
||||
createVesselSource,
|
||||
fetchOpenVesselPhoto,
|
||||
} from './standalone.js';
|
||||
@@ -12,6 +12,11 @@ import {
|
||||
} from './aircraft.js';
|
||||
import { normalizeVesselTrack, vesselSnapshot } from './vessels.js';
|
||||
import { parsePlanespottersPhoto } from '../../data/planespottersPhoto.js';
|
||||
import {
|
||||
MMSI,
|
||||
openVesselPhotosUrl,
|
||||
parseOpenVesselPhoto,
|
||||
} from '../../data/openVesselPhoto.js';
|
||||
|
||||
const defaultFetch = (...args) => globalThis.fetch(...args);
|
||||
const header = (response, name) => response.headers?.get?.(name);
|
||||
@@ -42,6 +47,34 @@ async function planespottersPhoto(fetchImpl, hex, signal) {
|
||||
return parsePlanespottersPhoto(payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* A vessel's Open Vessel Photos photo, read and host-checked. The API is
|
||||
* keyless and answers with `access-control-allow-origin: *`, so the browser
|
||||
* asks it straight. An MMSI it does not know answers 404: no photo.
|
||||
* @param {Function} fetchImpl fetch.
|
||||
* @param {string} mmsi The vessel's nine-digit MMSI.
|
||||
* @param {AbortSignal} [signal] Abort signal.
|
||||
* @returns {Promise<object>} A `parseOpenVesselPhoto` result.
|
||||
*/
|
||||
export async function fetchOpenVesselPhoto(fetchImpl, mmsi, signal) {
|
||||
const id = String(mmsi || '');
|
||||
if (!MMSI.test(id))
|
||||
throw new LiveSourceError('unsupported', 'Enrichment unavailable');
|
||||
const timeout = AbortSignal.timeout(PHOTO_TIMEOUT_MS);
|
||||
const { response, payload } = await readResponse(
|
||||
fetchImpl,
|
||||
openVesselPhotosUrl(id),
|
||||
{
|
||||
signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
|
||||
credentials: 'omit',
|
||||
},
|
||||
'Open Vessel Photos',
|
||||
);
|
||||
if (response.status === 404) return parseOpenVesselPhoto(null);
|
||||
if (!response.ok) throw httpError(response, 'Open Vessel Photos');
|
||||
return parseOpenVesselPhoto(payload, id);
|
||||
}
|
||||
|
||||
function openSkyError(response) {
|
||||
const error = httpError(response, 'Flights');
|
||||
const mode = String(
|
||||
@@ -235,6 +268,11 @@ export function createVesselSource({
|
||||
status: response.status,
|
||||
};
|
||||
},
|
||||
async getEnrichment(query, { signal } = {}) {
|
||||
if (query.kind !== 'photo')
|
||||
throw new LiveSourceError('unsupported', 'Enrichment unavailable');
|
||||
return fetchOpenVesselPhoto(fetchImpl, query.id, signal);
|
||||
},
|
||||
async getTrack(reference, { signal } = {}) {
|
||||
const { response, payload } = await readResponse(
|
||||
fetchImpl,
|
||||
|
||||
Reference in new issue
Block a user