Skip to content

Commit

Permalink
feat: Create a new route for Grub Street. (DIYgod#4760)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganrockmore authored May 18, 2020
1 parent ec383ae commit 4c95164
Show file tree
Hide file tree
Showing 4 changed files with 94 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 @@ -69,6 +69,10 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more

<Route author="zoenglinghou" example="/google/news/Headlines/hl=en-US&gl=US&ceid=US:en" path="/google/news/:category/:locale" :paramsDesc="['Category Title', 'locales, could be found behind `?`, including `hl`, `gl`, and `ceid` as parameters']"/>

## Grub Street

<RouteEn author="loganrockmore" example="/grubstreet" path="/grubstreet" />

## iDownloadBlog

### iDownloadBlog
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2672,4 +2672,7 @@ router.get('/umass/amherst/csnews', require('./routes/umass/amherst/csnews'));
// 飘花电影网
router.get('/piaohua/hot', require('./routes/piaohua/hot'));

// Grub Street
router.get('/grubstreet', require('./routes/grubstreet/index'));

module.exports = router;
9 changes: 9 additions & 0 deletions lib/routes/grubstreet/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const utils = require('./utils');

module.exports = async (ctx) => {
const url = `https://www.grubstreet.com/_components/newsfeed/instances/grubstreet-index@published`;
const title = `Grub Street`;
const description = `New York Magazine's Food and Restaurant Blog`;

ctx.state.data = await utils.getData(ctx, url, title, description);
};
78 changes: 78 additions & 0 deletions lib/routes/grubstreet/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');

async function load(link) {
const response = await got.get(link);
const $ = cheerio.load(response.data);

const description = $('div.article-content');

// remove the content that we don't want to show
description.find('aside.related').remove();
description.find('aside.article-details_heading-with-paragraph').remove();
description.find('section.package-list').remove();
description.find('div.source-links h2').remove();
description.find('div.source-links svg').remove();
description.find('div.mobile-secondary-area').remove();
description.find('aside.newsletter-flex-text').remove();

return {
description: description.html(),
};
}

async function ProcessFeed(list, caches) {
return await Promise.all(
list.map(async (item) => {
const itemUrl = item.canonicalUrl;

let bylineString = '';
if (item.byline) {
const byline = item.byline[0];
const bylineNames = byline.names.map((name) => name.text);
const bylineNamesString = bylineNames.join(', ');

bylineString = 'by ' + bylineNamesString;
}

const single = {
title: item.plaintextPrimaryHeadline,
link: itemUrl,
author: bylineString,
guid: itemUrl,
pubDate: item.date,
};

const other = await caches.tryGet(itemUrl, async () => await load(itemUrl));

return Promise.resolve(Object.assign({}, single, other));
})
);
}

const getData = async (ctx, url, title, description) => {
const response = await got({
method: 'get',
url: url,
headers: {
Referer: url,
},
});

const data = response.data;

// limit the list to only 25 articles, to make sure that load times remain reasonable
const list = data.articles.slice(0, 25);
const result = await ProcessFeed(list, ctx.cache);

return {
title: title,
link: url,
description: description,
item: result,
};
};

module.exports = {
getData,
};

0 comments on commit 4c95164

Please sign in to comment.