Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploration - fetch og:image for feature #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/services/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getFodyImage } from './images/getFodyImage';
import { getMapillaryImage } from './images/getMapillaryImage';
import { getWikiApiUrl, getWikiImage } from './images/getWikiImage';
import { Feature, Image } from './types';
import { getOgImage } from './images/getOgImage';

export const LOADING = null;

Expand Down Expand Up @@ -58,7 +59,22 @@ export const getFeatureImage = async (feature: Feature): Promise<Image> => {
}

// fallback to mapillary
return (
mapillaryPromise ?? (center ? await getMapillaryImage(center) : undefined)
);
if (center) {
const mapillaryImage = mapillaryPromise
? await mapillaryPromise
: await getMapillaryImage(center);
if (mapillaryImage) {
return mapillaryImage;
}
}

// lets try luck with og:image
if (tags?.website) {
const ogImage = await getOgImage(tags.website);
if (ogImage) {
return ogImage;
}
}

return undefined;
};
22 changes: 22 additions & 0 deletions src/services/images/getOgImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { fetchText } from '../fetch';

const protocol = /^\w+:\/\//;
const fixHttp = (url) => (url.match(protocol) ? url : `http://${url}`);

export const getOgImage = async (website: string) => {
const url = fixHttp(website);

// TODO in browser we need a proxy to avoid CORS
const html = await fetchText(url);
const content = html.match(/<meta property="og:image" content="([^"]+)"/);

if (content) {
return {
source: `${website} og:image`,
link: url,
thumb: content[1],
portrait: true,
};
}
return undefined;
};