|
| 1 | +const got = require('@/utils/got'); |
| 2 | + |
| 3 | +module.exports = async (ctx) => { |
| 4 | + const category = ctx.params.category ?? 'all'; |
| 5 | + |
| 6 | + const siteDomain = 'www.instructables.com'; |
| 7 | + const apiKey = 'NU5CdGwyRDdMVnVmM3l4cWNqQzFSVzJNZU5jaUxFU3dGK3J2L203MkVmVT02ZWFYeyJleGNsdWRlX2ZpZWxkcyI6WyJvdXRfb2YiLCJzZWFyY2hfdGltZV9tcyIsInN0ZXBCb2R5Il0sInBlcl9wYWdlIjo1MH0='; |
| 8 | + |
| 9 | + let pathPrefix, projectFilter; |
| 10 | + if (category === 'all') { |
| 11 | + pathPrefix = ''; |
| 12 | + projectFilter = ''; |
| 13 | + } else { |
| 14 | + pathPrefix = `${category}/`; |
| 15 | + const filterValue = `${category.charAt(0).toUpperCase()}${category.slice(1)}`; |
| 16 | + projectFilter = category !== 'teachers' ? ` && category:=${filterValue}` : `&& teachers:=${filterValue}`; |
| 17 | + } |
| 18 | + |
| 19 | + const link = `https://${siteDomain}/${pathPrefix}projects?projects=all`; |
| 20 | + |
| 21 | + const response = await got({ |
| 22 | + method: 'get', |
| 23 | + url: `https://${siteDomain}/api_proxy/search/collections/projects/documents/search`, |
| 24 | + headers: { |
| 25 | + Referer: link, |
| 26 | + Host: siteDomain, |
| 27 | + 'x-typesense-api-key': apiKey, |
| 28 | + }, |
| 29 | + searchParams: { |
| 30 | + q: '*', |
| 31 | + query_by: 'title,stepBody,screenName', |
| 32 | + page: 1, |
| 33 | + per_page: 50, |
| 34 | + sort_by: 'publishDate:desc', |
| 35 | + include_fields: 'title,urlString,coverImageUrl,screenName,publishDate,favorites,views,primaryClassification,featureFlag,prizeLevel,IMadeItCount', |
| 36 | + filter_by: `featureFlag:=true${projectFilter}`, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const data = response.data; |
| 41 | + |
| 42 | + ctx.state.data = { |
| 43 | + title: 'Instructables Projects', // 项目的标题 |
| 44 | + link, // 指向项目的链接 |
| 45 | + description: 'Instructables Projects', // 描述项目 |
| 46 | + language: 'en', // 频道语言 |
| 47 | + item: data.hits.map((item) => ({ |
| 48 | + title: item.document.title, |
| 49 | + link: `https://${siteDomain}/${item.document.urlString}`, |
| 50 | + author: item.document.screenName, |
| 51 | + pubDate: new Date(item.document.publishDate).toUTCString(), |
| 52 | + category: item.document.primaryClassification, |
| 53 | + })), |
| 54 | + }; |
| 55 | +}; |
0 commit comments