diff --git a/lib/routes/theblock/index.ts b/lib/routes/theblock/index.ts index 2004d27fb..e34b53c83 100644 --- a/lib/routes/theblock/index.ts +++ b/lib/routes/theblock/index.ts @@ -9,7 +9,7 @@ export const route: Route = { path: '/category/:category', categories: ['finance'], example: '/theblock/category/crypto-ecosystems', - parameters: { category: '`category` is the category of theblock' }, + parameters: { category: 'News category' }, features: { requireConfig: false, requirePuppeteer: false, @@ -27,7 +27,7 @@ export const route: Route = { target: '/category/:category', }, ], - description: `Get latest news from TheBlock by category. Note that due to website limitations, only article summaries may be available.`, + description: 'Get latest news from TheBlock by category. Note that due to website limitations, only article summaries may be available.', }; async function handler(ctx): Promise { @@ -36,14 +36,8 @@ async function handler(ctx): Promise { const apiUrl = `https://www.theblock.co/api/category/${category}`; - const browserHeaders = { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36', - }; - try { - const response = await ofetch(apiUrl, { - headers: browserHeaders, - }); + const response = await ofetch(apiUrl); // Extract articles from the nested data structure const articles = response.data?.articles || []; @@ -57,35 +51,31 @@ async function handler(ctx): Promise { cache.tryGet(`theblock:article:${article.url}`, async () => { try { // Try to get the full article - const articleResponse = await ofetch(article.url, { - headers: browserHeaders, - retry: 1, - }); + const articleResponse = await ofetch(`https://www.theblock.co/api/post/${article.id}/`); - const $ = load(articleResponse); - const articleContent = $('#articleContent'); + const post = articleResponse.post; + const $ = load(post.body, null, false); // If we successfully got the article content - if (articleContent.length) { + if (post.body.length) { // Remove unwanted elements - articleContent.find('div.copyright').remove(); + $('.copyright').remove(); - // Extract specific elements, similar to the Python implementation - const contentElements = articleContent.find('p, li, h2, h3'); let fullText = ''; - contentElements.each((_, element) => { - fullText += $(element).html() + '
'; - }); + if (article.thumbnail) { + fullText += `

${article.title}

`; + } + fullText += post.intro + $.html(); if (fullText) { return { title: article.title, link: article.url, - pubDate: parseDate(article.publishedFormatted, 'MMMM D, YYYY, h:mmA [EST]'), + pubDate: parseDate(post.published), description: fullText, - author: article.author?.name || 'TheBlock', - category: article.categories?.map((cat) => cat.name) || [], + author: article.authors?.map((a) => a.name).join(', ') || 'TheBlock', + category: [...new Set([post.categories.name, ...post.categories.map((cat) => cat.name), ...post.tags.map((tag) => tag.name)])], guid: article.url, image: article.thumbnail, }; @@ -144,8 +134,8 @@ function createSummaryItem(article: any) { link: article.url, pubDate: parseDate(article.publishedFormatted, 'MMMM D, YYYY, h:mmA [EST]'), description, - author: article.author?.name || 'TheBlock', - category: article.categories?.map((cat) => cat.name) || [], + author: article.authors?.map((a) => a.name).join(', ') || 'TheBlock', + category: article.primaryCategory?.name || [], guid: article.url, image: article.thumbnail, }; diff --git a/package.json b/package.json index cfa7e508c..e701d5683 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "ip-regex": "5.0.0", "jsdom": "26.0.0", "json-bigint": "1.0.0", - "jsonpath-plus": "^10.3.0", + "jsonpath-plus": "10.3.0", "jsrsasign": "10.9.0", "lru-cache": "11.0.2", "lz-string": "1.5.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ed54a02d..40427f877 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -153,7 +153,7 @@ importers: specifier: 1.0.0 version: 1.0.0 jsonpath-plus: - specifier: ^10.3.0 + specifier: 10.3.0 version: 10.3.0 jsrsasign: specifier: 10.9.0