Skip to content

Commit

Permalink
feat: add makeuseof (#6253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored Nov 30, 2020
1 parent 49e49af commit 5badeb2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/en/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Provides a better reading experience (full text articles) over the official one.

<RouteEn author="loganrockmore" example="/letterboxd/user/followingdiary/demiadejuyigbe" path="/letterboxd/user/followingdiary/:username" :paramsDesc="['username']" />

## MakeUseOf

<RouteEn author="nczitzk" example="/makeuseof" path="/makeuseof/:category?" :paramsDesc="['Category, Trending by default']"/>

## Matters

### Latest
Expand Down
4 changes: 4 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ Tag

<Route author="loganrockmore" example="/letterboxd/user/followingdiary/demiadejuyigbe" path="/letterboxd/user/followingdiary/:username" :paramsDesc="['username']" />

## MakeUseOf

<Route author="nczitzk" example="/makeuseof" path="/makeuseof/:category?" :paramsDesc="['分类,默认为 Trending']"/>

## Matataki

::: tip 提示
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3513,6 +3513,9 @@ router.get('/uisdc/news', require('./routes/uisdc/news'));
router.get('/uisdc/zt/:title?', require('./routes/uisdc/zt'));
router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic'));

// MakeUseOf
router.get('/makeuseof/:category?', require('./routes/makeuseof/index'));

// 瞬Matataki
// 热门作品
router.get('/matataki/posts/hot/:ipfsFlag?', require('./routes/matataki/site/posts/scoreranking'));
Expand Down
60 changes: 60 additions & 0 deletions lib/routes/makeuseof/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');

module.exports = async (ctx) => {
const category = ctx.params.category || '';

const rootUrl = 'https://www.makeuseof.com';
const currentUrl = `${rootUrl}${category === '' ? '' : '/category/' + category}`;
const response = await got({
method: 'get',
url: currentUrl,
});

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

const list = $('.home-secondary, .listing-content')
.find('.bc-title-link')
.slice(0, 15)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
})
.get();

const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);

content('.img-article-item')
.find('img')
.each(function () {
content(this).attr('src', content(this).prev().attr('data-srcset').split('?')[0]);
});

content('.ad-zone-container, .sharing, .sentinel-article-nextArticle, .article-tags, .w-article-author-bio, .ml-form-embedContainer').remove();

item.description = content('.article-body').html();
item.author = content('a.author').text().replace('By ', '');
item.pubDate = new Date(content('time').attr('datetime')).toUTCString();

return item;
})
)
);

ctx.state.data = {
title: `MakeUseOf - ${$('.listing-title').text() ? $('.listing-title').text() : 'Trending'}`,
link: currentUrl,
item: items,
};
};

1 comment on commit 5badeb2

@vercel
Copy link

@vercel vercel bot commented on 5badeb2 Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.