From 45cf28a6c311dfb5beaadcfdb58897b797461978 Mon Sep 17 00:00:00 2001 From: Kota <91359399+Skota11@users.noreply.github.com> Date: Sun, 1 Feb 2026 02:08:27 +0900 Subject: [PATCH] feat(route): add Scratch routes (#21025) * add scratch routes * add users'projects route * refactor(route): remove old Scratch routes and add new MIT routes for user comments and projects --- lib/routes/mit/scratch/user-comments.ts | 67 +++++++++++++++++++++++++ lib/routes/mit/scratch/user-projects.ts | 58 +++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 lib/routes/mit/scratch/user-comments.ts create mode 100644 lib/routes/mit/scratch/user-projects.ts diff --git a/lib/routes/mit/scratch/user-comments.ts b/lib/routes/mit/scratch/user-comments.ts new file mode 100644 index 000000000..e6602916e --- /dev/null +++ b/lib/routes/mit/scratch/user-comments.ts @@ -0,0 +1,67 @@ +import { load } from 'cheerio'; + +import type { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/scratch/user-comments/:username', + categories: ['social-media'], + example: '/mit/scratch/user-comments/skota11', + parameters: { username: 'Scratch username' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['scratch.mit.edu/users/:username/'], + target: '/scratch/user-comments/:username', + }, + ], + name: 'Scratch User Comments', + maintainers: ['Skota11'], + handler: async (ctx) => { + const { username } = ctx.req.param(); + const profileUrl = `https://scratch.mit.edu/users/${username}/`; + const apiUrl = `https://scratch.mit.edu/site-api/comments/user/${username}/`; + + const response = await ofetch(apiUrl, { + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }); + + const $ = load(response); + + const items = $('.comment') + .toArray() + .map((el) => { + const comment = $(el); + const author = comment.find('.name a').first().text().trim(); + const contentHtml = comment.find('.content').html()?.trim() || ''; + const textContent = comment.find('.content').text().trim(); + const commentId = comment.attr('data-comment-id'); + const timestamp = comment.find('.time').attr('title'); + + return { + title: textContent, + author, + description: contentHtml, + pubDate: timestamp ? parseDate(timestamp) : undefined, + link: `${profileUrl}#comments-${commentId}`, + guid: `scratch-comment-${commentId}`, + }; + }); + + return { + title: `Scratch User Comments - ${username}`, + link: profileUrl, + item: items, + }; + }, +}; diff --git a/lib/routes/mit/scratch/user-projects.ts b/lib/routes/mit/scratch/user-projects.ts new file mode 100644 index 000000000..80af9ddd1 --- /dev/null +++ b/lib/routes/mit/scratch/user-projects.ts @@ -0,0 +1,58 @@ +import type { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/scratch/user-projects/:username', + categories: ['social-media'], + example: '/mit/scratch/user-projects/abee', + parameters: { username: 'Scratch username' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['scratch.mit.edu/users/:username/projects/'], + target: '/scratch/user-projects/:username', + }, + ], + name: 'Scratch User Projects', + maintainers: ['Skota11'], + handler: async (ctx) => { + const { username } = ctx.req.param(); + + const limitQuery = ctx.req.query('limit') ?? '40'; + const limit = Math.min(Number.parseInt(limitQuery, 10) || 40, 40); + + const apiUrl = `https://api.scratch.mit.edu/users/${username}/projects/?limit=${limit}`; + + const data = await ofetch(apiUrl); + + const items = data.map((item) => { + const parts = [item.instructions ? `Instructions:
${item.instructions}` : '', item.description ? `Notes and Credits:
${item.description}` : ''].filter(Boolean); + const descriptionText = parts.join('

').replaceAll('\n', '
'); + + return { + title: item.title, + description: descriptionText, + // 公開日(共有日時) + pubDate: parseDate(item.history.shared), + link: `https://scratch.mit.edu/projects/${item.id}/`, + guid: `scratch-project-${item.id}`, + image: item.image, + author: username, + }; + }); + + return { + title: `Scratch User Projects - ${username}`, + link: `https://scratch.mit.edu/users/${username}/projects/`, + item: items, + }; + }, +};