const got = require('@/utils/got'); const config = require('@/config').value; const cheerio = require('cheerio'); const resolve_url = require('url').resolve; 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 = 'Basic YW5vbnltb3VzOkdpQ2VMRWp4bnFCY1ZwbnA2Y0xzVXZKaWV2dlJRY0FYTHY='; } else { const login = await got({ method: 'post', url: `https://api.theinitium.com/api/v2/auth/login/?language=${language}`, headers: { 'Content-Type': 'application/json', Accept: 'application/json', Connection: 'keep-alive', Authorization: 'Basic YW5vbnltb3VzOkdpQ2VMRWp4bnFCY1ZwbnA2Y0xzVXZKaWV2dlJRY0FYTHY=', Origin: `https://theinitium.com/`, Referer: `https://theinitium.com/`, 'X-Client-Name': 'Web', }, body: 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({ method: 'get', url: listurl, headers: 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 = async (slug) => { let content = await ctx.cache.get(slug + language); if (content) { return content; } content = ''; const response = await got({ method: 'get', url: `https://api.theinitium.com/api/v2/article/detail/?language=${language}&slug=${slug}`, headers: headers, }); const data = response.data; if (data.lead.length) { content += '

「' + data.lead + '」

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

' + data.byline + '

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