From 19bd1d4efc7c912928dc31d57b58518ead9111f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Wed, 20 May 2020 10:51:04 +0200 Subject: [PATCH] badge(opam): refactor opam web scraping (#393) --- api/opam.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/api/opam.ts b/api/opam.ts index beac3f5..864c41e 100644 --- a/api/opam.ts +++ b/api/opam.ts @@ -58,20 +58,21 @@ async function handler ({ topic, pkg }: PathArgs) { } function getPackageInfo(html: string): PackageInfo { + const info: PackageInfo = { version: '', license: '', downloads: NaN } + const $ = cheerio.load(html) - const info: PackageInfo = { version: '', license: '', downloads: 0 } - info.version = $('.package-version').first().text() - return $('.package-info th').get().reduce((acc, el) => { + const text = (selector: any) => $(selector).text().trim() + + info.version = text($('.package-version').first()) + $('.package-info th').filter((_, el) => { const $el = $(el) - const text = $el.text().toLowerCase() - if (text === 'license') { - acc.license = $el.next().text() - return acc + const label = text($el).toLowerCase() + if (label === 'license') { + info.license = text($el.next()) + } else if (label === 'statistics') { + info.downloads = parseInt(text($el.next().find('strong')), 0) } - if (text === 'statistics') { - acc.downloads = parseInt($el.next().find('strong').text(), 0) - return acc - } - return acc - }, info) + return !info.license && isNaN(info.downloads) + }) + return info }