From b9e01258a582fb9c9a6a9108545dd7f17019758c Mon Sep 17 00:00:00 2001 From: Dwemer <70470685+dwemerx@users.noreply.github.com> Date: Sun, 18 Oct 2020 18:34:03 +0300 Subject: [PATCH] fear: add dev.to top posts route (#5930) Co-authored-by: dwemerx --- docs/en/social-media.md | 20 ++++++++++++---- docs/social-media.md | 12 ++++++++++ lib/router.js | 3 +++ lib/routes/dev.to/top.js | 52 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 lib/routes/dev.to/top.js diff --git a/docs/en/social-media.md b/docs/en/social-media.md index 5000aee1a6..346b5a1a2e 100644 --- a/docs/en/social-media.md +++ b/docs/en/social-media.md @@ -10,6 +10,18 @@ pageClass: routes +## Dev.to + +### Top Posts + + + +| dev.to weekly top | dev.to monthly top | dev.to yearly top | dev.to top posts of all time | +| ----------------- | ------------------ | ----------------- | ---------------------------- | +| week | month | year | infinity | + + + ## Disqus ### Comment @@ -36,9 +48,10 @@ pageClass: routes ::: tip -Official user RSS: - - RSS: `https://**:instance**/users/**:username**.rss` ([Example](https://pawoo.net/users/pawoo_support.rss)) - - Atom: ~~`https://**:instance**/users/**:username**.atom`~~ (Only for pawoo.net, [example](https://pawoo.net/users/pawoo_support.atom)) +Official user RSS: + +- RSS: `https://**:instance**/users/**:username**.rss` ([Example](https://pawoo.net/users/pawoo_support.rss)) +- Atom: ~~`https://**:instance**/users/**:username**.atom`~~ (Only for pawoo.net, [example](https://pawoo.net/users/pawoo_support.atom)) These feed do not include boosts (a.k.a. reblogs). RSSHub provides a feed for user timeline based on the Mastodon API, but to use that, you will need to create application on a Mastodon instance, and configure your RSSHub instance. Check the [Deploy Guide](/en/install/#route-specific-configurations) for route-specific configurations. @@ -60,7 +73,6 @@ These feed do not include boosts (a.k.a. reblogs). RSSHub provides a feed for us - ## piapro ### User latest works diff --git a/docs/social-media.md b/docs/social-media.md index 1fb3b85162..eab0f8ef3c 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -286,6 +286,18 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 +## Dev.to + +### 最高职位 + + + +| 开发到每周最高 | 开发至每月最高 | 开发年度最高 | 开发到有史以来最高职位 | +| -------------- | -------------- | ------------ | ---------------------- | +| week | month | year | infinity | + + + ## Disqus ### 评论 diff --git a/lib/router.js b/lib/router.js index ec1b532fd3..00499ea341 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3356,4 +3356,7 @@ router.get('/allnow/column/:id', require('./routes/allnow/column')); // 证券时报网 router.get('/stcn/news/:id?', require('./routes/stcn/news')); +// dev.to +router.get('/dev.to/top/:period', require('./routes/dev.to/top')); + module.exports = router; diff --git a/lib/routes/dev.to/top.js b/lib/routes/dev.to/top.js new file mode 100644 index 0000000000..f681acf631 --- /dev/null +++ b/lib/routes/dev.to/top.js @@ -0,0 +1,52 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const period = ctx.params.period; + const link = `https://dev.to/top/${period}`; + + const getHTML = async () => { + const response = await got({ + method: 'get', + url: link, + }); + + return response.data; + }; + + const html = await ctx.cache.tryGet(link, getHTML); + const posts = []; + + const $ = cheerio.load(html); + $('div.crayons-story__body').each(function () { + const post = {}; + + const author = $('.crayons-story__secondary', this).text().trim().replace(/\s\s+/g, ', '); + const title = $('.crayons-story__title a', this).text().trim(); + const link = `https://dev.to${$('.crayons-story__title a', this).attr('href')}`; + const description = $('.crayons-story__tags', this).text().trim().replace(/\s\s+/g, ' '); + const pubDate = new Date($('.time-ago-indicator-initial-placeholder', this).attr('data-seconds') * 1000).toUTCString(); + + post.author = author; + post.title = title; + post.link = link; + post.description = description; + post.pubDate = pubDate; + + posts.push(post); + }); + + ctx.state.data = { + title: `dev.to top (${period})`, + link, + description: 'Top dev.to posts', + language: 'en-us', + item: posts.map((post) => ({ + title: post.title, + description: post.description, + link: post.link, + author: post.author, + pubDate: post.pubDate, + })), + }; +};