Skip to content

Commit

Permalink
fix(route/hbr): remove Latest from type
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Dec 4, 2024
1 parent b138698 commit cb551b4
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions lib/routes/hbr/topic.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/topic/:topic?/:type?',
categories: ['new-media'],
example: '/hbr/topic/leadership',
parameters: { topic: 'Topic, can be found in URL, Leadership by default', type: 'Type, see below, Latest by default' },
categories: ['new-media', 'popular'],
example: '/hbr/topic/Leadership/Popular',
parameters: {
topic: 'Topic, can be found in URL, Leadership by default',
type: {
description: 'Type, see below, Popular by default',
options: [
{ value: 'Popular', label: 'Popular' },
{ value: 'From the Store', label: 'From the Store' },
{ value: 'For You', label: 'For You' },
],
default: 'Popular',
},
},
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -23,30 +34,27 @@ export const route: Route = {
},
],
name: 'Topic',
maintainers: ['nczitzk'],
maintainers: ['nczitzk', 'pseudoyu'],
handler,
description: `| LATEST | POPULAR | FROM THE STORE | FOR YOU |
| ------ | ------- | -------------- | ------- |
| Latest | Popular | From the Store | For You |
description: `| POPULAR | FROM THE STORE | FOR YOU |
| ------- | -------------- | ------- |
| Popular | From the Store | For You |
:::tip
Click here to view [All Topics](https://hbr.org/topics)
:::`,
};

async function handler(ctx) {
const topic = ctx.req.param('topic') ?? 'leadership';
const type = ctx.req.param('type') ?? 'Latest';
const topic = ctx.req.param('topic') ?? 'Leadership';
const type = ctx.req.param('type') ?? 'Popular';

const rootUrl = 'https://hbr.org';
const currentUrl = `${rootUrl}/topic/${topic}`;

const response = await got({
method: 'get',
url: currentUrl,
});
const response = await ofetch(currentUrl);

const $ = load(response.data);
const $ = load(response);

const list = $(`stream-content[data-stream-name="${type}"]`)
.find('.stream-item')
Expand All @@ -65,12 +73,9 @@ async function handler(ctx) {
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const detailResponse = await ofetch(item.link);

const content = load(detailResponse.data);
const content = load(detailResponse);

item.description = content('.article-body, article[itemprop="description"]').html();
item.pubDate = parseDate(content('meta[property="article:published_time"]').attr('content'));
Expand Down

0 comments on commit cb551b4

Please sign in to comment.