File size: 2,294 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { load } from 'cheerio';

import type { Data, Route } from '@/types';
import ofetch from '@/utils/ofetch';

import { processList } from './utils';

export const route: Route = {
    path: '/category/:category?/:subCategory?',
    categories: ['programming'],
    example: '/category/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/: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;
}