From cc562aa1ada5ee582e400f0f215f5dbcf8e47677 Mon Sep 17 00:00:00 2001 From: James Chen-Smith <15643597+jameschensmith@users.noreply.github.com> Date: Tue, 12 Jul 2022 08:47:06 -0500 Subject: [PATCH] fix(route): add Open Graph image to GitHub trending (#10189) * fix(route): add Open Graph image to GitHub trending Adds Open Graph image for each repository to the GitHub trending feed. * chore(route): address pull request feedback - Refactors page request call to make it more terse - Adds art template for trending description - Updates maintainers for '/trending/:since/:language?/:spoken_language?' --- lib/v2/github/maintainer.js | 2 +- .../github/templates/trending-description.art | 5 ++ lib/v2/github/trending.js | 48 ++++++++++++------- 3 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 lib/v2/github/templates/trending-description.art diff --git a/lib/v2/github/maintainer.js b/lib/v2/github/maintainer.js index a286a5e2bc..5a7e0d6132 100644 --- a/lib/v2/github/maintainer.js +++ b/lib/v2/github/maintainer.js @@ -10,6 +10,6 @@ module.exports = { '/starred_repos/:user': ['LanceZhu'], '/stars/:user/:repo': ['HenryQW'], '/topics/:name/:qs?': ['queensferryme'], - '/trending/:since/:language?/:spoken_language?': ['DIYgod'], + '/trending/:since/:language?/:spoken_language?': ['DIYgod', 'jameschensmith'], '/user/followers/:user': ['HenryQW'], }; diff --git a/lib/v2/github/templates/trending-description.art b/lib/v2/github/templates/trending-description.art new file mode 100644 index 0000000000..4f80467814 --- /dev/null +++ b/lib/v2/github/templates/trending-description.art @@ -0,0 +1,5 @@ + +
{{@ desc }} +

Language: {{@ lang }} +
Stars: {{@ stars }} +
Forks: {{@ forks }} diff --git a/lib/v2/github/trending.js b/lib/v2/github/trending.js index 9c0a6caed5..ca2ba42c08 100644 --- a/lib/v2/github/trending.js +++ b/lib/v2/github/trending.js @@ -1,5 +1,7 @@ const got = require('@/utils/got'); +const { art } = require('@/utils/render'); const cheerio = require('cheerio'); +const path = require('path'); module.exports = async (ctx) => { const since = ctx.params.since; @@ -20,24 +22,38 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('article'); + const items = await Promise.all( + list.map((_, item) => { + item = $(item); + const endpoint = item.find('h1 a').attr('href'); + const link = `https://github.com${endpoint}`; + return ctx.cache.tryGet(`github:trending:${endpoint}`, async () => { + const response = await got(link); + + const $ = cheerio.load(response.data); + const cover = $('meta[property="og:image"]'); + + const single = { + title: item.find('h1').text(), + author: item.find('h1').text().split('/')[0].trim(), + description: art(path.join(__dirname, 'templates/trending-description.art'), { + cover: cover.attr('content'), + desc: item.find('p').text(), + lang: item.find('span[itemprop="programmingLanguage"]').text() || 'Unknown', + stars: item.find('.Link--muted').eq(0).text().trim(), + forks: item.find('.Link--muted').eq(1).text().trim(), + }), + link, + }; + + return single; + }); + }) + ); + ctx.state.data = { title: $('title').text(), link: url, - item: - list && - list - .map((_, item) => { - item = $(item); - return { - title: item.find('h1').text(), - author: item.find('h1').text().split('/')[0].trim(), - description: `${item.find('.pr-4').text()}
-
Language: ${item.find('span[itemprop="programmingLanguage"]').text() ?? 'unknown'} -
Star: ${item.find('.Link--muted').eq(0).text().trim()} -
Fork: ${item.find('.Link--muted').eq(1).text().trim()}`, - link: `https://github.com${item.find('h1 a').attr('href')}`, - }; - }) - .get(), + item: items, }; };