Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Route for 30secondsofcode #18045

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/routes/30secondsofcode/category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Data, Route } from '@/types';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';
import { processList } from './utils';
export const route: Route = {
path: '/:category?/:subCategory?',
categories: ['programming', 'popular'],
Rjnishant530 marked this conversation as resolved.
Show resolved Hide resolved
example: '/css/interactivity',
parameters: {
category: {
description: 'Main Category. For Complete list visit site "https://www.30secondsofcode.org/collections/p/1/"',
options: [
{ value: 'js', label: 'Javascript' },
{ value: 'css', label: 'CSS' },
{ value: 'algorithm', label: 'JavaScript Algorithms' },
{ value: 'react', label: 'React' },
],
},
subCategory: {
description: 'Filter within Category. Visit Individual Category site for subCategories',
},
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['30secondsofcode.org/:category/:subCategory/', '30secondsofcode.org/:category/'],
target: '/:category/:subCategory',
},
],
name: 'Category and Subcategory',
maintainers: ['Rjnishant530'],
handler,
};

async function handler(ctx) {
const category = ctx.req.param('category') ?? '';
const subCategory = ctx.req.param('subCategory') ?? '';

const rootUrl = 'https://www.30secondsofcode.org';
const currentUrl = `${rootUrl}${category ? `/${category}` : ''}${subCategory ? `/${subCategory}` : ''}${category || subCategory ? '/p/1/' : ''}`;

const response = await ofetch(currentUrl);
const $ = load(response);
const heroElement = $('section.hero');
const heading = heroElement.find('div > h1').text();
const description = heroElement.find('div > p').text();
const image = heroElement.find('img').attr('src');

const fullList = $('section.preview-list > ul > li').toArray();
const items = await processList(fullList);
return {
title: heading,
description,
image: `${rootUrl}${image}`,
link: rootUrl,
item: items,
} as Data;
}
7 changes: 7 additions & 0 deletions lib/routes/30secondsofcode/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '30 Seconds of code',
url: '30secondsofcode.org',
Rjnishant530 marked this conversation as resolved.
Show resolved Hide resolved
lang: 'en',
};
41 changes: 41 additions & 0 deletions lib/routes/30secondsofcode/new-and-popular.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Data, Route } from '@/types';
import { load } from 'cheerio';
import { processList, rootUrl } from './utils';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/',
Rjnishant530 marked this conversation as resolved.
Show resolved Hide resolved
categories: ['programming', 'popular'],
Rjnishant530 marked this conversation as resolved.
Show resolved Hide resolved
example: '/',
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['30secondsofcode.org'],
target: '/',
},
],
name: 'New & Popular Snippets',
maintainers: ['Rjnishant530'],
handler,
};

async function handler() {
const response = await ofetch(rootUrl);

const $ = load(response);
const fullList = $('section.preview-list > ul > li').toArray();
const items = await processList(fullList);
return {
title: 'New & Popular Snippets',
description: 'Discover short code snippets for all your development needs.',
link: rootUrl,
item: items,
} as Data;
}
54 changes: 54 additions & 0 deletions lib/routes/30secondsofcode/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DataItem } from '@/types';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';

export const rootUrl = 'https://www.30secondsofcode.org';

export async function processList(listElements) {
const items = await Promise.allSettled(
listElements.map((item) => {
const $ = load(item);
const link = $(' article > h3 > a').attr('href');
const date = $(' article > small > time').attr('datetime');
return processItem({ link, date });
})
);
return items.map((item) => (item.status === 'fulfilled' ? item.value : ({ title: 'Error Reading Item' } as DataItem)));
}

async function processItem({ link: articleLink, date }) {
return await cache.tryGet(`30secondsofcode:${articleLink}`, async () => {
const finalLink = `${rootUrl}${articleLink}`;
const response = await ofetch(finalLink);
const $ = load(response);
const tags = $.root()
.find('body > main > nav > ol > li:not(:first-child):not(:last-child)')
.toArray()
.map((tag) => $(tag).find('a').text());
const article = $('main > article');
const title = article.find('h1').text();
article.find('img').each((_, element) => {
const img = $(element);
const src = img.attr('src');
if (src?.startsWith('/')) {
img.attr('src', `${rootUrl}${src}`);
}
});
const image = article.find('img').attr('src');
const description = article.clone().find('script').remove().end().html();
Rjnishant530 marked this conversation as resolved.
Show resolved Hide resolved

return {
title,
link: finalLink,
pubDate: parseDate(date),
description,
author: '30 Seconds of Code',
category: tags,
image: `${rootUrl}${image}`,
banner: `${rootUrl}${image}`,
language: 'en-us',
} as DataItem;
});
}
Loading