import { load } from 'cheerio'; import type { Context } from 'hono'; import { FetchError } from 'ofetch'; import { config } from '@/config'; import InvalidParameterError from '@/errors/types/invalid-parameter'; import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; const TOKEN = 'Basic YW5vbnltb3VzOkdpQ2VMRWp4bnFCY1ZwbnA2Y0xzVXZKaWV2dlJRY0FYTHY='; export const processFeed = async (model: string, ctx: Context) => { // model是channel/tag/etc.,而type是latest/feature/quest-academy这些一级栏目/标签/作者名的slug名。如果是追踪的话,那就是model是follow,type是articles。 const type = ctx.req.param('type') ?? 'latest'; const language = ctx.req.param('language') ?? 'zh-hans'; let listUrl; let listLink; switch (model) { case 'author': listUrl = `https://api.theinitium.com/api/v2/author/?language=${language}&slug=${type}`; listLink = `https://theinitium.com/author/${type}/`; break; case 'follow': listUrl = `https://api.theinitium.com/api/v2/user/follows/${type}/?language=${language}`; listLink = `https://theinitium.com/follow/`; break; case 'channel': listUrl = `https://api.theinitium.com/api/v2/channel/articles/?language=${language}&slug=${type}`; listLink = `https://theinitium.com/channel/${type}/`; break; case 'tags': listUrl = `https://api.theinitium.com/api/v2/tag/articles/?language=${language}&slug=${type}`; listLink = `https://theinitium.com/tags/${type}/`; break; default: throw new InvalidParameterError('wrong model'); } const key = { email: config.initium.username, password: config.initium.password, }; const body = JSON.stringify(key); let token; const cacheIn = await cache.get('initium:token'); if (cacheIn) { token = cacheIn; } else if (config.initium.bearertoken) { token = config.initium.bearertoken; cache.set('initium:token', config.initium.bearertoken); } else if (key.email === undefined) { token = TOKEN; } else { const login = await got.post(`https://api.theinitium.com/api/v2/auth/login/?language=${language}`, { headers: { 'Content-Type': 'application/json', Accept: 'application/json', Connection: 'keep-alive', Authorization: TOKEN, }, body, }); token = 'token ' + login.data.token; cache.set('initium:token', token); } const headers = { Accept: '*/*', Connection: 'keep-alive', Authorization: token, }; let response; try { response = await got(listUrl, { headers, }); } catch (error) { if (error instanceof FetchError && error.statusCode === 401) { // 401 说明 token 过期了,将它删掉 await cache.set('initium:token', ''); } throw error; } const name = response.data.name || (response.data[model] && response.data[model].name) || '追踪'; // 从v1直升的channel和tags里面是digests,v2新增的author和follow出来都是results const articles = response.data.results ?? response.data.digests; // 如果model=author,那就是avatar;否则都是cover,要么就没封面 const image = response.data[model] && (response.data[model].cover || response.data[model].avatar); const getFullText = (slug) => cache.tryGet(`theinitium:${slug}:${language}`, async () => { let content = ''; const { data } = await got(`https://api.theinitium.com/api/v2/article/detail/?language=${language}&slug=${slug}`, { headers, }); if (data.lead.length) { content += '

「' + data.lead + '」

'; } if (data.byline.length) { content += '

' + data.byline + '

'; } if (data.content) { content += data.content.replace('