From 02645435aa4cb236ab324186cb0097b3ddf7f198 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 23 Jun 2025 05:56:09 +0800 Subject: [PATCH] feat(route): add makerworld (#19434) --- lib/routes/makerworld/contest.ts | 51 ++++++++++++++++++++++++++ lib/routes/makerworld/namespace.ts | 7 ++++ lib/routes/makerworld/trending.ts | 45 +++++++++++++++++++++++ lib/routes/makerworld/user-upload.ts | 54 ++++++++++++++++++++++++++++ lib/routes/makerworld/utils.ts | 18 ++++++++++ 5 files changed, 175 insertions(+) create mode 100644 lib/routes/makerworld/contest.ts create mode 100644 lib/routes/makerworld/namespace.ts create mode 100644 lib/routes/makerworld/trending.ts create mode 100644 lib/routes/makerworld/user-upload.ts create mode 100644 lib/routes/makerworld/utils.ts diff --git a/lib/routes/makerworld/contest.ts b/lib/routes/makerworld/contest.ts new file mode 100644 index 000000000..60f477047 --- /dev/null +++ b/lib/routes/makerworld/contest.ts @@ -0,0 +1,51 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { baseUrl, getNextBuildId } from './utils'; +import { parseDate } from '@/utils/parse-date'; +import { config } from '@/config'; + +export const route: Route = { + path: '/contests', + categories: ['design'], + example: '/makerworld/contests', + name: 'Contests', + maintainers: ['TonyRL'], + handler, + radar: [ + { + source: ['makerworld.com/:lang/contests'], + }, + ], +}; + +async function handler() { + const nextBuildId = await getNextBuildId(); + const response = await ofetch(`${baseUrl}/_next/data/${nextBuildId}/en/contests.json`, { + headers: { + 'User-Agent': config.trueUA, + }, + }); + const { listConst, previewList } = response.pageProps; + + const items = [ + ...listConst.map((c) => ({ + title: c.contestName, + link: `${baseUrl}/en/contests/${c.id}?name=${c.contestName}`, + description: c.themeDesc, + pubDate: parseDate(c.startTime), + })), + ...previewList.map((p) => ({ + title: p.contestTheme, + description: p.themeDesc, + pubDate: parseDate(p.contestTime), + })), + ]; + + return { + title: 'Contest - MakerWorld', + description: 'Join the contest to showcase your creativity and win substantial rewards', + link: `${baseUrl}/en/contests`, + image: `${baseUrl}/favicon_new.png`, + item: items, + }; +} diff --git a/lib/routes/makerworld/namespace.ts b/lib/routes/makerworld/namespace.ts new file mode 100644 index 000000000..254599295 --- /dev/null +++ b/lib/routes/makerworld/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'MakerWorld', + url: 'makerworld.com', + lang: 'en', +}; diff --git a/lib/routes/makerworld/trending.ts b/lib/routes/makerworld/trending.ts new file mode 100644 index 000000000..cd01d2656 --- /dev/null +++ b/lib/routes/makerworld/trending.ts @@ -0,0 +1,45 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { baseUrl, getNextBuildId } from './utils'; +import { parseDate } from '@/utils/parse-date'; +import { config } from '@/config'; + +export const route: Route = { + path: '/trending', + categories: ['design'], + example: '/makerworld/trending', + name: 'Trending Models', + maintainers: ['TonyRL'], + handler, + radar: [ + { + source: ['makerworld.com/:lang'], + }, + ], +}; + +async function handler() { + const nextBuildId = await getNextBuildId(); + const response = await ofetch(`${baseUrl}/_next/data/${nextBuildId}/en.json`, { + headers: { + 'User-Agent': config.trueUA, + }, + }); + + const items = response.pageProps.popularDesignsData.map((d) => ({ + title: d.title, + link: `${baseUrl}/en/models/${d.id}-${d.slug}`, + author: d.designCreator.name, + category: d.tags, + pubDate: parseDate(d.startTime), + description: d.designExtension.design_pictures.map((i) => `
${d.name}
${i.name}
`).join(''), + })); + + return { + title: 'Trending Models - MakerWorld', + description: 'Leading 3D printing model community for designers and makers. Download thousands of 3D models and stl models for free, and your No.1 option for multicolor 3D models', + link: `${baseUrl}/en`, + image: `${baseUrl}/favicon_new.png`, + item: items, + }; +} diff --git a/lib/routes/makerworld/user-upload.ts b/lib/routes/makerworld/user-upload.ts new file mode 100644 index 000000000..96a074cbc --- /dev/null +++ b/lib/routes/makerworld/user-upload.ts @@ -0,0 +1,54 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { baseUrl, getNextBuildId } from './utils'; +import { parseDate } from '@/utils/parse-date'; +import { config } from '@/config'; + +export const route: Route = { + path: '/user/:handle/upload', + categories: ['design'], + example: '/makerworld/@Wcad00/upload', + parameters: { + handle: 'User handle', + }, + name: 'User Uploads', + maintainers: ['TonyRL'], + handler, + radar: [ + { + source: ['makerworld.com/:lang/:handle/upload', 'makerworld.com/:lang/:handle'], + }, + ], +}; + +async function handler(ctx) { + const { handle } = ctx.req.param(); + + const nextBuildId = await getNextBuildId(); + const response = await ofetch(`${baseUrl}/_next/data/${nextBuildId}/en/${handle}/upload.json`, { + headers: { + 'User-Agent': config.trueUA, + }, + query: { + handle, + }, + }); + const { userInfo, designs } = response.pageProps; + + const items = designs.map((d) => ({ + title: d.title, + link: `${baseUrl}/en/models/${d.id}-${d.slug}`, + pubDate: parseDate(d.createTime), + author: d.designCreator.name, + category: d.tags, + description: d.instances.map((i) => `
${d.title}
${i.title}
`).join(''), + })); + + return { + title: `${userInfo.name} | Published - MakerWorld`, + link: `${baseUrl}/en/${handle}/upload`, + description: userInfo.personal.bio, + image: userInfo.avatar, + item: items, + }; +} diff --git a/lib/routes/makerworld/utils.ts b/lib/routes/makerworld/utils.ts new file mode 100644 index 000000000..a9b02819a --- /dev/null +++ b/lib/routes/makerworld/utils.ts @@ -0,0 +1,18 @@ +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import { config } from '@/config'; + +export const baseUrl = 'https://makerworld.com'; + +export const getNextBuildId = () => + cache.tryGet('makerworld:nextBuildId', async () => { + const response = await ofetch(`${baseUrl}/en`, { + headers: { + 'User-Agent': config.trueUA, + }, + }); + const $ = load(response); + const nextData = JSON.parse($('script#__NEXT_DATA__').text()); + return nextData.buildId; + });