From 2db014e48c8addd172676276a8134b4a6fa88c95 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Tue, 8 Feb 2022 06:51:20 +0100 Subject: [PATCH] feat(plugins/starlists): add languages statistics supports (#856) --- source/plugins/starlists/index.mjs | 56 ++++++++++++++----- source/plugins/starlists/metadata.yml | 12 ++++ .../templates/classic/partials/starlists.ejs | 37 +++++++++++- source/templates/classic/style.css | 10 +++- 4 files changed, 98 insertions(+), 17 deletions(-) diff --git a/source/plugins/starlists/index.mjs b/source/plugins/starlists/index.mjs index 488cac7c..6a185e09 100644 --- a/source/plugins/starlists/index.mjs +++ b/source/plugins/starlists/index.mjs @@ -7,7 +7,7 @@ export default async function({login, q, imports, data, account}, {enabled = fal return null //Load inputs - let {limit, ignored, only, "limit.repositories":_limit, "shuffle.repositories":_shuffle} = imports.metadata.plugins.starlists.inputs({data, account, q}) + let {limit, ignored, only, "limit.repositories":_limit, languages, "limit.languages":_limit_languages, "shuffle.repositories":_shuffle} = imports.metadata.plugins.starlists.inputs({data, account, q}) ignored = ignored.map(imports.stripemojis) only = only.map(imports.stripemojis) @@ -40,24 +40,52 @@ export default async function({login, q, imports, data, account}, {enabled = fal .slice(0, limit) console.debug(`metrics/compute/${login}/plugins > starlists > extracted ${lists.length} lists`) - //Fetch star list content + //Compute lists content + const colors = {} for (const list of lists) { + //Fetch star list content console.debug(`metrics/compute/${login}/plugins > starlists > fetching ${list.name}`) - await page.goto(list.link) - const repositories = await page.evaluate(() => [...document.querySelectorAll("#user-list-repositories > div:not(.paginate-container)")].map(element => ({ - name:element.querySelector("div:first-child")?.innerText.replace(" / ", "/") ?? "", - description:element.querySelector(".py-1")?.innerText ?? "", - language:{ - name:element.querySelector("[itemprop='programmingLanguage']")?.innerText ?? "", - color:element.querySelector(".repo-language-color")?.style?.backgroundColor?.match(/\d+/g)?.map(x => Number(x).toString(16)).join("") ?? null, - }, - stargazers:Number(element.querySelector("[href$='/stargazers']")?.innerText.trim().replace(/[^\d]/g, "") ?? NaN), - forks:Number(element.querySelector("[href$='/network/members']")?.innerText.trim().replace(/[^\d]/g, "") ?? NaN), - })) - ) + const repositories = [] + for (let i = 1; i <= (languages ? 100 : 1); i++) { + console.debug(`metrics/compute/${login}/plugins > starlists > fetching page ${i}`) + await page.goto(`${list.link}?page=${i}`) + repositories.push(...await page.evaluate(() => [...document.querySelectorAll("#user-list-repositories > div:not(.paginate-container)")].map(element => ({ + name:element.querySelector("div:first-child")?.innerText.replace(" / ", "/") ?? "", + description:element.querySelector(".py-1")?.innerText ?? "", + language:{ + name:element.querySelector("[itemprop='programmingLanguage']")?.innerText ?? "", + color:element.querySelector(".repo-language-color")?.style?.backgroundColor?.match(/\d+/g)?.map(x => Number(x).toString(16).padStart(2, "0")).join("") ?? null, + }, + stargazers:Number(element.querySelector("[href$='/stargazers']")?.innerText.trim().replace(/[^\d]/g, "") ?? NaN), + forks:Number(element.querySelector("[href$='/network/members']")?.innerText.trim().replace(/[^\d]/g, "") ?? NaN), + })) + )) + if (await page.evaluate(() => document.querySelector(".next_page.disabled"))) { + console.debug(`metrics/compute/${login}/plugins > starlists > reached last page`) + break + } + } list.repositories.push(...repositories) if (_shuffle) list.repositories = imports.shuffle(list.repositories) + + //Compute languages statistics + if (languages) { + list.languages = {} + for (const {language:{name, color}} of repositories) { + if (name) + list.languages[name] = (list.languages[name] ?? 0) + 1 + if (color) + colors[name] = color + } + list.languages = Object.entries(list.languages).sort((a, b) => b[1] - a[1]).slice(0, _limit_languages || Infinity) + const visible = list.languages.map(([_, value]) => value).reduce((a, b) => a + b, 0) + list.languages = list.languages.map(([name, value]) => ({name, value, color:name in colors ? `#${colors[name]}` : null, x:0, p:value/visible})) + for (let i = 1; i < list.languages.length; i++) + list.languages[i].x = (list.languages[i-1]?.x ?? 0) + (list.languages[i-1]?.value ?? 0)/visible + } + + //Limit repositories list.repositories = list.repositories.slice(0, _limit) } diff --git a/source/plugins/starlists/metadata.yml b/source/plugins/starlists/metadata.yml index 20378f48..1364cf6b 100644 --- a/source/plugins/starlists/metadata.yml +++ b/source/plugins/starlists/metadata.yml @@ -28,6 +28,18 @@ inputs: min: 0 max: 100 + plugin_starlists_languages: + description: Toggle star list languages statistics + type: boolean + default: no + + plugin_starlists_limit_languages: + description: Disply limit (languages per star list) + type: number + default: 8 + min: 0 + zero: disable + plugin_starlists_shuffle_repositories: description: Shuffle data for varied outputs type: boolean diff --git a/source/templates/classic/partials/starlists.ejs b/source/templates/classic/partials/starlists.ejs index da8f6292..150051f1 100644 --- a/source/templates/classic/partials/starlists.ejs +++ b/source/templates/classic/partials/starlists.ejs @@ -5,14 +5,14 @@ <%= plugins.starlists?.count ?? "" %> Star list<%= s(plugins.starlists?.count ?? 0) %>
-
+
<% if (plugins.starlists.error) { %>
<%= plugins.starlists.error.message %>
<% } else { %> - <% for (const {name, description, count, repositories} of plugins.starlists.lists) { %> + <% for (const {name, description, count, repositories, languages = null} of plugins.starlists.lists) { %>

@@ -20,6 +20,39 @@

<%= count %> repositor<%= s(count, "y") %>
<%= description %>
+ <% if (languages) { const width = 420 * (1 + large), rows = large ? [0, 1, 2, 3] : [0, 1] %> +
+
+ + + + + + <% for (const {name, value, color, x, p} of languages) { %> + "/> + <% } %> + +
+
+ <% for (const row of rows) { %> +
+ <% for (const {name, value, color} of languages.filter((_, i) => i%rows.length === row)) { %> +
+
+ " fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8z"> + <%= name %> +
+ +
<%= f.percentage(value/count) %>
+
<%= f(value) %>★
+
+
+ <% } %> +
+ <% } %> +
+
+ <% } %>
<% for (const repository of repositories) { %>
diff --git a/source/templates/classic/style.css b/source/templates/classic/style.css index 0be57f7a..6090ed7a 100644 --- a/source/templates/classic/style.css +++ b/source/templates/classic/style.css @@ -854,7 +854,8 @@ /* Star lists */ .starlist { - margin-left: 28px; + padding-left: 28px; + width: 460px; } .starlist > .description, .starlist > .count { margin-left: 32px; @@ -876,6 +877,13 @@ .starlist .repository .name { font-size: 14px; } + .starlist .languages { + margin-top: 6px; + padding-left: 13px; + } + .starlist .languages svg.bar { + margin-left: 18px; + } /* Anilist */ .anilist {