const got = require('@/utils/got'); const config = require('@/config').value; const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); const TOKEN = 'Basic YW5vbnltb3VzOkdpQ2VMRWp4bnFCY1ZwbnA2Y0xzVXZKaWV2dlJRY0FYTHY='; module.exports = async (ctx) => { // model是channel/tag/etc.,而type是latest/feature/quest-academy这些一级栏目/标签/作者名的slug名。如果是追踪的话,那就是model是follow,type是articles。 const model = ctx.params.model ?? 'channel'; const type = ctx.params.type ?? 'latest'; const language = ctx.params.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; } const key = { email: config.initium.username, password: config.initium.password, iapReceipt: config.initium.iap_receipt, }; const body = JSON.stringify(key); let token; const cache = await ctx.cache.get('initium:token'); if (cache) { token = cache; } else if (config.initium.bearertoken) { token = config.initium.bearertoken; ctx.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, Origin: `https://theinitium.com/`, Referer: `https://theinitium.com/`, 'X-Client-Name': 'Web', }, body, }); /* const devices = login.data.access.devices; for (const key in devices) { const device = devices[key]; if (device.status === 'logged_in' && !device.logout_at && device.platform === 'web') { token = 'Bearer ' + device.device_id; break; } } */ token = 'Bearer ' + login.data.token; ctx.cache.set('initium:token', token); } const headers = { Accept: '*/*', Connection: 'keep-alive', Authorization: token, 'X-IAP-Receipt': key.iapReceipt || '', }; const response = await got(listUrl, { headers, }); 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.results : response.data.digests; // 如果model=author,那就是avatar;否则都是cover,要么就没封面 const image = response.data[model] && (response.data[model].cover || response.data[model].avatar); const getFullText = (slug) => ctx.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('