From bb3f9cde6b31d438485ae80941e5789706a31772 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 8 Oct 2020 22:17:14 +0800 Subject: [PATCH] feat: add anki changes (#5799) Co-authored-by: Henry Wang --- docs/en/program-update.md | 6 ++++++ docs/program-update.md | 6 ++++++ lib/router.js | 3 +++ lib/routes/anki/changes.js | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 lib/routes/anki/changes.js diff --git a/docs/en/program-update.md b/docs/en/program-update.md index bf9ae400d6..d5040fd7d4 100644 --- a/docs/en/program-update.md +++ b/docs/en/program-update.md @@ -4,6 +4,12 @@ pageClass: routes # Application Updates +## Anki + +### Changes + + + ## Apkpure ### Versions diff --git a/docs/program-update.md b/docs/program-update.md index 039a93b559..1fef997e80 100644 --- a/docs/program-update.md +++ b/docs/program-update.md @@ -14,6 +14,12 @@ pageClass: routes +## Anki + +### Changes + + + ## Apkpure ### Versions diff --git a/lib/router.js b/lib/router.js index cc4d681f31..1fb3dfab72 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3260,6 +3260,9 @@ router.get('/appsales/:caty?/:time?', require('./routes/appsales/index')); // CGTN router.get('/cgtn/top', require('./routes/cgtn/top')); +// Anki +router.get('/anki/changes', require('./routes/anki/changes')); + // ABC News router.get('/abc/:site?', require('./routes/abc/index.js')); diff --git a/lib/routes/anki/changes.js b/lib/routes/anki/changes.js new file mode 100644 index 0000000000..cf6564b40d --- /dev/null +++ b/lib/routes/anki/changes.js @@ -0,0 +1,39 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const md = require('markdown-it')({ + html: true, +}); + +module.exports = async (ctx) => { + const rootUrl = 'https://changes.ankiweb.net'; + const currentUrl = `${rootUrl}/README.md`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const html = md.render(response.data.replace(/# Changes in/g, '## Changes in').replace(/### Changes in/g, '## Changes in')); + const $ = cheerio.load(html.replace(/

/g, '

')); + + const items = $('div') + .slice(0, 20) + .map((_, item) => { + item = $(item); + const h2 = item.find('h2'); + const match = item.find('p').text().match('Released (.*).'); + h2.remove(); + return { + title: h2.text(), + description: item.html(), + link: `${rootUrl}/#/?id=changes-in-${h2.text().toLowerCase().split(' ').join('-').replace(/\./g, '')}`, + pubDate: match ? new Date(match[1].split(',')[0] + ' GMT+8').toUTCString() : '', + }; + }) + .get(); + + ctx.state.data = { + title: 'Changes - Anki', + link: rootUrl, + item: items, + }; +};