From 61a2f51e4b96d6018591a938a037a5d87e817c96 Mon Sep 17 00:00:00 2001 From: Yuan Qing Lim Date: Sat, 30 Dec 2023 20:00:33 +0800 Subject: [PATCH] Edit stats script --- packages/website/package.json | 4 ++-- packages/website/scripts/fetch-stats.ts | 20 ++++++++++++------- .../plugins.json} | 1 - .../scripts/scrape/ignored-ids/widgets.json | 1 + packages/website/scripts/scrape/scrape.ts | 8 ++++++-- 5 files changed, 22 insertions(+), 12 deletions(-) rename packages/website/scripts/scrape/{ignored-ids.json => ignored-ids/plugins.json} (98%) create mode 100644 packages/website/scripts/scrape/ignored-ids/widgets.json diff --git a/packages/website/package.json b/packages/website/package.json index ce370a6b1..d92148a72 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -16,8 +16,8 @@ "scrape-plugins": "tsx scripts/scrape/scrape.ts plugin globals/plugins.json 5", "scrape-widgets": "tsx scripts/scrape/scrape.ts widget globals/widgets.json 5", "serve": "sirv build --dev --host --port 4242", - "stats-plugins": "tsx scripts/fetch-stats.ts plugin globals/plugins.json", - "stats-widgets": "tsx scripts/fetch-stats.ts widget globals/widgets.json", + "stats-plugins": "tsx scripts/fetch-stats.ts plugin globals/plugins.json scripts/scrape/ignored-ids/plugins.json", + "stats-widgets": "tsx scripts/fetch-stats.ts widget globals/widgets.json scripts/scrape/ignored-ids/widgets.json", "prewebsite-build": "npm run clean", "website-build": "build-website --minify", "prewebsite-watch": "npm run clean", diff --git a/packages/website/scripts/fetch-stats.ts b/packages/website/scripts/fetch-stats.ts index 6df5c12cc..fee4e0cd8 100644 --- a/packages/website/scripts/fetch-stats.ts +++ b/packages/website/scripts/fetch-stats.ts @@ -20,11 +20,12 @@ async function main() { throw new Error('Need a file path') } const filePath = args[1] - const ids: Array = JSON.parse( - await fs.readFile(filePath, 'utf8') - ).map(function (item: { id: string }) { - return item.id - }) + const ignoredIdsFilePath = args[2] + const ids: Array = JSON.parse(await fs.readFile(filePath, 'utf8')) + .map(function (item: { id: string }) { + return item.id + }) + .concat(JSON.parse(await fs.readFile(ignoredIdsFilePath, 'utf8'))) const stats = await fetchStatsAsync(ids, type) printStats(stats, type) } catch (error: any) { @@ -77,8 +78,12 @@ async function fetchStatAsync( } function printStats(stats: Array, type: 'plugin' | 'widget'): void { - const data = stats.map(function ({ id, name, runCount, likeCount }: Stat) { + const data = stats.map(function ( + { id, name, runCount, likeCount }: Stat, + index: number + ) { return [ + new Intl.NumberFormat().format(index + 1), name.length > NAME_MAX_LENGTH ? `${name.slice(0, NAME_MAX_LENGTH).trim()}…` : name, @@ -87,7 +92,7 @@ function printStats(stats: Array, type: 'plugin' | 'widget'): void { `https://figma.com/community/${type}/${id}` ] }) - data.splice(0, 0, ['name', 'runs', 'likes', 'url']) + data.splice(0, 0, ['', 'name', 'runs', 'likes', 'url']) const totalRunCount = stats.reduce(function ( total: number, { runCount }: Stat @@ -101,6 +106,7 @@ function printStats(stats: Array, type: 'plugin' | 'widget'): void { return total + likeCount }, 0) data.push([ + '', '', new Intl.NumberFormat().format(totalRunCount), new Intl.NumberFormat().format(totalLikeCount), diff --git a/packages/website/scripts/scrape/ignored-ids.json b/packages/website/scripts/scrape/ignored-ids/plugins.json similarity index 98% rename from packages/website/scripts/scrape/ignored-ids.json rename to packages/website/scripts/scrape/ignored-ids/plugins.json index f72c0a59f..bc415f57b 100644 --- a/packages/website/scripts/scrape/ignored-ids.json +++ b/packages/website/scripts/scrape/ignored-ids/plugins.json @@ -41,7 +41,6 @@ "1196638111314691410", "1203372504182342503", "1206354900231909165", - "1206354900231909165", "1209233052747647603", "1211504102085754443", "1212381421658754793", diff --git a/packages/website/scripts/scrape/ignored-ids/widgets.json b/packages/website/scripts/scrape/ignored-ids/widgets.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/packages/website/scripts/scrape/ignored-ids/widgets.json @@ -0,0 +1 @@ +[] diff --git a/packages/website/scripts/scrape/scrape.ts b/packages/website/scripts/scrape/scrape.ts index ca50e2f8e..d3ae218de 100644 --- a/packages/website/scripts/scrape/scrape.ts +++ b/packages/website/scripts/scrape/scrape.ts @@ -10,7 +10,8 @@ const CONCURRENCY = 10 const __dirname = dirname(fileURLToPath(import.meta.url)) const invalidIdsFilePath = join(__dirname, 'invalid-ids.json') -const ignoredIdsFilePath = join(__dirname, 'ignored-ids.json') +const ignoredPluginIdsFilePath = join(__dirname, 'ignored-ids', 'plugins.json') +const ignoredWidgetIdsFilePath = join(__dirname, 'ignored-ids', 'widgets.json') async function main(): Promise { try { @@ -33,7 +34,10 @@ async function main(): Promise { await fs.readFile(invalidIdsFilePath, 'utf8') ) const ignoredIds: Array = JSON.parse( - await fs.readFile(ignoredIdsFilePath, 'utf8') + await fs.readFile( + type === 'plugin' ? ignoredPluginIdsFilePath : ignoredWidgetIdsFilePath, + 'utf8' + ) ) const data: Array> = await fetchDataAsync({ maxPages,