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

希望大佬做个bbc英语学习的rss #17990

Open
1 task done
He-Xun opened this issue Dec 26, 2024 · 0 comments
Open
1 task done

希望大佬做个bbc英语学习的rss #17990

He-Xun opened this issue Dec 26, 2024 · 0 comments
Labels
RSS proposal New RSS wanted

Comments

@He-Xun
Copy link

He-Xun commented Dec 26, 2024

Category

Study

Website URL

https://www.bbc.co.uk/learningenglish/chinese/features/take-away-english

Website description

英语学习栏目
有这么几个分类栏目:
- take-away-english
- authentic-real-english
- lingohack
- q-and-a
- media-english
- todays-phrase
- english-in-a-minute
- storytellers
- english-quizzes
- english-at-work

What content should be included?

音频、图片、文章、重点词汇

Additional description

自己写了一些代码 水平有限 没有调试好 烦请大佬帮忙写一个
import { Route } from '@/types';
import cache from '@/utils/cache';
import parser from '@/utils/rss-parser';
import { load } from 'cheerio';
import utils from './utils';
import ofetch from '@/utils/ofetch';

export enum Category {
TraditionalMedia = 'traditional-media',
Technology = 'technology',
Education = 'education',
LanguageLearning = 'language-learning', // 新增
}

export const route: Route = {
path: '/:site?/:channel?',
name: 'BBC News and Learning English',
maintainers: ['HenryQW', 'DIYgod', 'pseudoyu', 'your-github-username'],
handler,
example: '/bbc/learningenglish/take-away-english',
parameters: {
site: '语言,简体或繁体中文,或者学习栏目 learningenglish',
channel: '新闻频道(如 world-asia)或学习栏目(如 take-away-english)',
},
categories: [Category.TraditionalMedia, Category.LanguageLearning], // 新增类别
description: Provides RSS feeds for BBC News and BBC Learning English. Learning English supports categories like: - take-away-english - authentic-real-english - lingohack - q-and-a - media-english - todays-phrase - english-in-a-minute - storytellers - english-quizzes - english-at-work.,
};

async function handler(ctx) {
const { site, channel } = ctx.req.param();

if (site === 'learningenglish' && channel) {
    return handleLearningEnglish(channel);
} else {
    return handleNews(ctx);
}

}

// 抽离 BBC Learning English 逻辑
async function handleLearningEnglish(channel: string) {
const baseUrl = https://www.bbc.co.uk/learningenglish/chinese/features/${channel};
const response = await cache.tryGet(baseUrl, () => ofetch(baseUrl));
const $ = load(response);

const items = utils.ProcessLearningContent($);

return {
    title: `BBC Learning English - ${channel.replace(/-/g, ' ').toUpperCase()}`,
    link: baseUrl,
    description: `Learn English with BBC Learning English - ${channel.replace(/-/g, ' ')}`,
    item: items,
};

}

// 抽离新闻频道逻辑
async function handleNews(ctx) {
let feed, title, link;

const { site, channel } = ctx.req.param();

if (site) {
    switch (site.toLowerCase()) {
        case 'chinese':
            title = 'BBC News 中文网';
            feed = await (channel
                ? parser.parseURL(`https://www.bbc.co.uk/zhongwen/simp/${channel}/index.xml`)
                : parser.parseURL('https://www.bbc.co.uk/zhongwen/simp/index.xml'));
            break;

        case 'traditionalchinese':
            title = 'BBC News 中文網';
            feed = await (channel
                ? parser.parseURL(`https://www.bbc.co.uk/zhongwen/trad/${channel}/index.xml`)
                : parser.parseURL('https://www.bbc.co.uk/zhongwen/trad/index.xml'));
            link = 'https://www.bbc.com/zhongwen/trad';
            break;

        default:
            feed = await parser.parseURL(`https://feeds.bbci.co.uk/news/${site.split('-').join('/')}/rss.xml`);
            title = `BBC News ${site}`;
            link = `https://www.bbc.co.uk/news/${site.split('-').join('/')}`;
            break;
    }
} else {
    feed = await parser.parseURL('https://feeds.bbci.co.uk/news/rss.xml');
    title = 'BBC News Top Stories';
    link = 'https://www.bbc.co.uk/news';
}

const items = await Promise.all(
    feed.items
        .filter((item) => item && item.link)
        .map((item) =>
            cache.tryGet(item.link, async () => {
                try {
                    const linkURL = new URL(item.link);
                    if (linkURL.hostname === 'www.bbc.com') {
                        linkURL.hostname = 'www.bbc.co.uk';
                    }

                    const response = await ofetch(linkURL.href, {
                        retryStatusCodes: [403],
                    });

                    const $ = load(response);
                    const path = linkURL.pathname;

                    let description;

                    switch (true) {
                        case path.startsWith('/sport'):
                            description = item.content;
                            break;
                        case path.startsWith('/sounds/play'):
                            description = item.content;
                            break;
                        case path.startsWith('/news/live'):
                            description = item.content;
                            break;
                        default:
                            description = utils.ProcessFeed($);
                    }

                    return {
                        title: item.title || '',
                        description: description || '',
                        pubDate: item.pubDate || new Date().toUTCString(),
                        link: item.link,
                    };
                } catch {
                    return {} as Record<string, any>;
                }
            })
        )
);

return {
    title,
    link,
    image: 'https://www.bbc.com/favicon.ico',
    description: title,
    item: items.filter((item) => Object.keys(item).length > 0),
};

}

// utils.ts 修改部分
export function ProcessLearningContent($: cheerio.Root) {
return $('div.promo-item')
.map((_, el) => {
const element = $(el);
return {
title: element.find('h2').text().trim(),
link: element.find('a').attr('href')?.startsWith('http') ? element.find('a').attr('href') : https://www.bbc.co.uk${element.find('a').attr('href')},
description: element.find('p').text().trim(),
};
})
.get();
}

This is not a duplicated rss request

@He-Xun He-Xun added the RSS proposal New RSS wanted label Dec 26, 2024
@He-Xun He-Xun changed the title https://www.bbc.co.uk/learningenglish/chinese/features/take-away-english 希望大佬做个bbc英语学习的rss Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RSS proposal New RSS wanted
Projects
None yet
Development

No branches or pull requests

1 participant