From 4fa47b2ac3b7e6c87c727cfa6ffdb34fa5024bdc Mon Sep 17 00:00:00 2001 From: matthieu42morin Date: Fri, 3 Nov 2023 23:59:31 +0100 Subject: [PATCH] WIP Home page + layout --- src/lib/types/contributions.d.ts | 10 +++ src/routes/(home)/+page.svelte | 77 ++++++++++++++++++++ src/routes/(home)/Scene.svelte | 64 +++++++++++++++++ src/routes/+layout.svelte | 96 +++++++++++++++++++++++++ src/routes/api/[user]/[year]/+server.ts | 55 ++++++++++++++ 5 files changed, 302 insertions(+) create mode 100644 src/lib/types/contributions.d.ts create mode 100644 src/routes/(home)/+page.svelte create mode 100644 src/routes/(home)/Scene.svelte create mode 100644 src/routes/+layout.svelte create mode 100644 src/routes/api/[user]/[year]/+server.ts diff --git a/src/lib/types/contributions.d.ts b/src/lib/types/contributions.d.ts new file mode 100644 index 0000000..28a5544 --- /dev/null +++ b/src/lib/types/contributions.d.ts @@ -0,0 +1,10 @@ +type Day = { + count: number; + day: number; + level: number; + month: string; + name: string; + year: number; +}; + +export type Contributions = Array; diff --git a/src/routes/(home)/+page.svelte b/src/routes/(home)/+page.svelte new file mode 100644 index 0000000..897b22b --- /dev/null +++ b/src/routes/(home)/+page.svelte @@ -0,0 +1,77 @@ + + + + +
+
+

I make the wheels turn.

+ +
+
+ +
+ + + +
+

Try editing the following:

+

foo:bar

+

ligma:ass

+
+ Icon +

My github contributions

+
+ +
+ + + +
+
+ + diff --git a/src/routes/(home)/Scene.svelte b/src/routes/(home)/Scene.svelte new file mode 100644 index 0000000..aed217d --- /dev/null +++ b/src/routes/(home)/Scene.svelte @@ -0,0 +1,64 @@ + + + + + + + + + + + + + {#if Array.isArray(contributions) && contributions.length > 0} + {#each contributions as row, i} + {#each row as day, j} + {#if day !== null} + {@const y = normalize(day.level)} + + + + + + + {/if} + {/each} + {/each} + {/if} + diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte new file mode 100644 index 0000000..bc87733 --- /dev/null +++ b/src/routes/+layout.svelte @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/routes/api/[user]/[year]/+server.ts b/src/routes/api/[user]/[year]/+server.ts new file mode 100644 index 0000000..048b615 --- /dev/null +++ b/src/routes/api/[user]/[year]/+server.ts @@ -0,0 +1,55 @@ +import { parseHTML } from 'linkedom'; +import type { RouteParams } from './$types'; +import { json } from '@sveltejs/kit'; + +export async function GET({ params }) { + const html = await getContributions(params); + return json(parseContributions(html)); +} +/** + * Scrape function that fetches the HTML response from GitHub. + * The response contains a table with all the contributions for the given year. + */ +async function getContributions({ user, year }: RouteParams) { + const api = `https://github.com/users/${user}/contributions?from=${year}-12-01&to=${year}-12-31`; + const response = await fetch(api); + + if (!response.ok) { + throw new Error(`Failed to fetch: ${response.status}`); + } + + return await response.text(); +} +/** + * This function parses the HTML response from a GitHub account page and returns an array of a user's contributions. + ** Each contribution is an object with a date and a level. + ** This function may stop working if GitHub changes the HTML structure, like they did in somewhen in October 2023. + */ +function parseContributions(html: string) { + const { document } = parseHTML(html); + const rows = document.querySelectorAll('tbody > tr'); + const contributions: any[] = []; + + for (const row of rows) { + const days = row.querySelectorAll('td.ContributionCalendar-day'); + const currentRow: any[] = []; + + for (const day of days) { + const date = day.getAttribute('data-date'); + const level = day.getAttribute('data-level'); + + if (date && level) { + const contribution = { + date, + level: parseInt(level, 10) + }; + currentRow.push(contribution); + } else { + currentRow.push(null); + } + } + contributions.push(currentRow); + } + + return contributions; +}