feat(route/capitalmind): add capital mind (#19601)
* init * remove podcast details * update podcast details * remove temp string * Update lib/routes/capitalmind/utils.ts Co-authored-by: Tony <TonyRL@users.noreply.github.com> * add image
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { Data, Route } from '@/types';
|
||||
import { baseUrl, fetchArticles } from './utils';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/insights',
|
||||
example: '/capitalmind/insights',
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['capitalmind.in/insights'],
|
||||
target: '/insights',
|
||||
},
|
||||
],
|
||||
name: 'Insights',
|
||||
maintainers: ['Rjnishant530'],
|
||||
handler,
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const items = await fetchArticles('insights');
|
||||
|
||||
return {
|
||||
title: 'Capitalmind Insights',
|
||||
link: `${baseUrl}/insights`,
|
||||
description: 'Financial insights and analysis from Capitalmind',
|
||||
language: 'en',
|
||||
item: items,
|
||||
allowEmpty: false,
|
||||
image: `${baseUrl}/favicons/favicon.ico`,
|
||||
icon: `${baseUrl}/favicons/favicon.ico`,
|
||||
logo: `${baseUrl}/favicons/favicon.ico`,
|
||||
} as Data;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'Capitalmind',
|
||||
url: 'capitalmind.in',
|
||||
lang: 'en',
|
||||
categories: ['finance'],
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Data, Route } from '@/types';
|
||||
import { baseUrl, fetchArticles } from './utils';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/podcasts',
|
||||
example: '/capitalmind/podcasts',
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: true,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['capitalmind.in/podcasts'],
|
||||
target: '/podcasts',
|
||||
},
|
||||
],
|
||||
name: 'Podcasts',
|
||||
maintainers: ['Rjnishant530'],
|
||||
handler,
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const items = await fetchArticles('podcasts');
|
||||
|
||||
return {
|
||||
title: 'Capitalmind Podcasts',
|
||||
link: `${baseUrl}/podcasts`,
|
||||
description: 'Podcasts from Capitalmind on investing and finance',
|
||||
language: 'en',
|
||||
item: items,
|
||||
allowEmpty: false,
|
||||
itunes_author: 'Capitalmind',
|
||||
image: `${baseUrl}/favicons/apple-touch-icon.png`,
|
||||
icon: `${baseUrl}/favicons/favicon.ico`,
|
||||
logo: `${baseUrl}/favicons/favicon.ico`,
|
||||
} as Data;
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import { load } from 'cheerio';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import cache from '@/utils/cache';
|
||||
import { DataItem } from '@/types';
|
||||
|
||||
export const baseUrl = 'https://www.capitalmind.in';
|
||||
|
||||
export async function fetchArticles(path) {
|
||||
const url = `${baseUrl}/${path}/page/1`;
|
||||
const response = await ofetch(url);
|
||||
const $ = load(response);
|
||||
|
||||
const articlePromises = $('.article-wrapper a.article-card-wrapper')
|
||||
.toArray()
|
||||
.map(async (element) => {
|
||||
const $element = $(element);
|
||||
const link = baseUrl + $element.attr('href');
|
||||
return await cache.tryGet(link, async () => {
|
||||
const title = $element.find('h3').text().trim();
|
||||
const author = $element
|
||||
.find(String.raw`div.text-[16px]`)
|
||||
.text()
|
||||
.trim();
|
||||
const image = $element.find('img').attr('src');
|
||||
const imageUrl = image?.startsWith('/_next/image') ? image.split('url=')[1].split('&')[0] : image;
|
||||
const decodedImageUrl = imageUrl ? decodeURIComponent(imageUrl) : '';
|
||||
|
||||
// Fetch full article content
|
||||
const articleResponse = await ofetch(link);
|
||||
const $articlePage = load(articleResponse);
|
||||
const $article = $articlePage('article').clone();
|
||||
|
||||
// Extract tags from footer
|
||||
const tags: string[] = $article
|
||||
.find('footer div')
|
||||
.toArray()
|
||||
.map((el) => {
|
||||
const $el = $articlePage(el);
|
||||
$el.find('.sr-only').remove();
|
||||
const tag = $el.text().trim();
|
||||
return tag;
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
// Extract publication date from header
|
||||
let pubDate = '';
|
||||
const $header = $article.find('header');
|
||||
const $time = $header.find('time');
|
||||
if ($time.length) {
|
||||
pubDate = $time.attr('datetime') || $time.text().trim();
|
||||
}
|
||||
|
||||
const $content = $article.find('section[aria-label="Post content"]').clone();
|
||||
|
||||
// Remove footer
|
||||
$content.find('footer').remove();
|
||||
|
||||
// Process Libsyn podcast iframe (assuming only one)
|
||||
let podcastData: { mediaUrl?: string; itunes_duration?: number; image?: string } = {};
|
||||
|
||||
const $iframe = $content.find('iframe[src*="libsyn.com/embed/episode/id/"]');
|
||||
if ($iframe.length) {
|
||||
const src = $iframe.attr('src');
|
||||
if (src) {
|
||||
const idMatch = src.match(/\/id\/(\d+)\//);
|
||||
if (idMatch && idMatch[1]) {
|
||||
const episodeId = idMatch[1];
|
||||
try {
|
||||
const episodeData = await ofetch(`https://html5-player.libsyn.com/api/episode/id/${episodeId}`);
|
||||
if (episodeData && episodeData._item && episodeData._item._primary_content) {
|
||||
podcastData = {
|
||||
mediaUrl: episodeData._item._primary_content._download_url,
|
||||
image: `https://assets.libsyn.com/item/${episodeId}`,
|
||||
itunes_duration: episodeData._item._primary_content.duration,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
logger.info(`Failed to fetch podcast data for episode ID ${episodeId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert relative image URLs to absolute URLs only in figure tags
|
||||
// and remove srcset attribute
|
||||
$content.find('figure img').each((_, img) => {
|
||||
const $img = $articlePage(img);
|
||||
const src = $img.attr('src');
|
||||
|
||||
// Remove srcset attribute
|
||||
$img.removeAttr('srcset');
|
||||
|
||||
if (src && src.startsWith('/_next/image')) {
|
||||
// Extract the original URL from the Next.js image URL
|
||||
const urlMatch = src.match(/url=([^&]+)/);
|
||||
if (urlMatch && urlMatch[1]) {
|
||||
const originalUrl = decodeURIComponent(urlMatch[1]);
|
||||
$img.attr('src', originalUrl);
|
||||
} else if (src.startsWith('/')) {
|
||||
// Handle other relative URLs
|
||||
$img.attr('src', baseUrl + src);
|
||||
}
|
||||
}
|
||||
});
|
||||
return {
|
||||
title,
|
||||
link,
|
||||
author,
|
||||
description: $content.html() || `<p><img src="${decodedImageUrl}" alt="${title}"></p><p>Author: ${author}</p>`,
|
||||
guid: link,
|
||||
itunes_item_image: podcastData?.image || decodedImageUrl,
|
||||
category: tags,
|
||||
pubDate,
|
||||
enclosure_url: podcastData?.mediaUrl || null,
|
||||
itunes_duration: podcastData?.itunes_duration || null,
|
||||
enclosure_type: podcastData?.mediaUrl ? 'audio/mpeg' : null,
|
||||
} as DataItem;
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(articlePromises);
|
||||
}
|
||||
Reference in New Issue
Block a user