Files
RSSHub/lib/v2/google/fonts.js
Fatpandac c144a1864b feat(route): add Google Fonts and refactor /google/** to V2 (#8927)
* Add(route): add Google Fonts and change to V2

* Fix(route): added throw error when API key is underfined

* Fix(docs): add warning container

* Fix(docs): Update docs/design.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Fix(route): sort routes

* Fix(route): Update lib/v2/google/fonts.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2022-02-03 01:18:17 +08:00

44 lines
1.3 KiB
JavaScript

const got = require('@/utils/got');
const config = require('@/config').value;
const { art } = require('@/utils/render');
const path = require('path');
const { parseDate } = require('@/utils/parse-date');
const titleMap = {
date: 'Newest',
popularity: 'Most Popular',
trending: 'Trending',
alpha: 'Name',
style: 'Number of styles',
};
module.exports = async (ctx) => {
const sort = ctx.params.sort ?? 'date';
const limit = ctx.params.limit ?? 25;
const API_KEY = config.google.fontsApiKey;
if (!API_KEY) {
throw new Error('Google Fonts API key is required.');
}
const googleFontsAPI = `https://www.googleapis.com/webfonts/v1/webfonts?sort=${sort}&key=${API_KEY}`;
const response = await got.get(googleFontsAPI);
const data = response.data.items.slice(0, limit);
ctx.state.data = {
title: `Google Fonts - ${titleMap[sort]}`,
link: 'https://fonts.google.com',
item:
data &&
data.map((item) => ({
title: item.family,
description: art(path.join(__dirname, './templates/fonts.art'), {
item,
}),
link: `https://fonts.google.com/specimen/${item.family.replace(/\s/g, '+')}`,
pubDate: parseDate(item.lastModified, 'YYYY-MM-DD'),
})),
};
};