Skip to content

Commit

Permalink
feat(route/geocaching): add lang param, update response structure (DI…
Browse files Browse the repository at this point in the history
…Ygod#18148)

* feat(route/geocaching): add lang param, update response structure

* fix(route/geocaching): update searchParams type definition to include additional fields

* fix(route/geocaching): enhance language handling in searchParams with error handling for unsupported languages

* fix(route/geocaching): update _fields type to string

* add common parameter: limit & mode=fulltext

* fix(route/geocaching): remove mode handling and ensure description always uses full content
  • Loading branch information
Konano authored Jan 21, 2025
1 parent a53ab54 commit f8ba9fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
105 changes: 83 additions & 22 deletions lib/routes/geocaching/blogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/blogs',
path: '/blogs/:language?',
categories: ['blog'],
example: '/geocaching/blogs',
parameters: {},
example: '/geocaching/blogs/en',
parameters: {
language: {
description: 'language',
default: 'en',
options: [
{ value: 'en', label: 'English' },
{ value: 'de', label: 'Deutsch' },
{ value: 'fr', label: 'Français' },
{ value: 'es', label: 'Español' },
{ value: 'nl', label: 'Nederlands' },
{ value: 'cs', label: 'Čeština' },
{ value: 'all', label: 'Not Specified' },
],
},
},
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -21,35 +35,82 @@ export const route: Route = {
},
],
name: 'Official Blogs',
maintainers: ['HankChow'],
maintainers: ['HankChow', 'Konano'],
handler,
url: 'geocaching.com/blog/',
};

const languageToCategory = { de: 140, fr: 138, es: 702, nl: 737, cs: 1404 };
const languageToLabel = { de: 'Deutsch', fr: 'Français', es: 'Español', nl: 'Nederlands', cs: 'Čeština' };

async function handler(ctx) {
const baseUrl = 'https://www.geocaching.com';
const { data: response } = await got(`${baseUrl}/blog/wp-json/wp/v2/posts`, {
searchParams: {
per_page: ctx.req.query('limit') ?? 100,
_embed: 1,
},
});
const language = ctx.req.param('language') ?? 'en';
const searchParams: {
per_page: number;
_embed: number;
_fields: string;
categories_exclude?: string;
categories?: number;
} = {
per_page: ctx.req.query('limit') ?? 20,
_embed: 1,
_fields: ['id', 'title', 'link', 'guid', 'content', 'date_gmt', 'modified_gmt', '_embedded', '_links'].join(','),
};

if (language === 'en') {
searchParams.categories_exclude = Object.values(languageToCategory).join(',');
} else if (language in languageToCategory) {
searchParams.categories = languageToCategory[language];
} else if (language === 'all') {
// do nothing
} else {
throw new Error(`Unsupported language: ${language}`);
}

const items = response.map((item) => ({
title: item.title.rendered,
link: item.link,
guid: item.guid.rendered,
description: item.content.rendered,
pubDate: parseDate(item.date_gmt),
author: item._embedded.author[0].name,
category: item._embedded['wp:term'][0].map((category) => category.name),
}));
// console.log(searchParams);

const { data: response } = await got(`${baseUrl}/blog/wp-json/wp/v2/posts`, { searchParams });
const items = response.map((item) => {
const media = item._embedded['wp:featuredmedia'][0];
const mediaDetails = media?.media_details;
return {
title: item.title.rendered.trim(),
link: item.link,
guid: item.guid.rendered,
description: item.content.rendered,
pubDate: parseDate(item.date_gmt),
updated: parseDate(item.modified_gmt),
author: item._embedded.author[0].name,
category: item._embedded['wp:term'][0].map((category) => category.name.trim()),
media: media
? {
content: {
url: media.source_url,
type: media.mime_type,
height: mediaDetails.height,
width: mediaDetails.width,
fileSize: mediaDetails.filesize,
},
thumbnail: {
url: mediaDetails.sizes.large.source_url,
height: mediaDetails.sizes.large.height,
width: mediaDetails.sizes.large.width,
},
}
: undefined,
};
});

return {
title: 'Geocaching Blog',
title: language in languageToLabel ? `Geocaching Blog - ${languageToLabel[language]}` : 'Geocaching Blog',
link: `${baseUrl}/blog/`,
image: `${baseUrl}/blog/favicon.ico`,
description: 'Geocaching 博客更新',
language: language in languageToCategory ? language : 'en',
image: 'https://i.ytimg.com/vi_webp/G28VxvBoSLQ/maxresdefault.webp',
icon: `${baseUrl}/blog/favicon.ico`,
logo: `${baseUrl}/blog/favicon.ico`,
description: 'Geocaching Official Blog',
item: items,
allowEmpty: true,
};
}
6 changes: 0 additions & 6 deletions lib/routes/geocaching/templates/blogs.art

This file was deleted.

0 comments on commit f8ba9fc

Please sign in to comment.