fix: Used github releases download count
All checks were successful
Build Gameflow Site / build (push) Successful in 57s
All checks were successful
Build Gameflow Site / build (push) Successful in 57s
This commit is contained in:
parent
08d302fb90
commit
19291076fd
2 changed files with 78 additions and 13 deletions
|
|
@ -11,6 +11,7 @@ import {
|
||||||
emulators,
|
emulators,
|
||||||
appContributorsData,
|
appContributorsData,
|
||||||
storeContributorsData,
|
storeContributorsData,
|
||||||
|
totalDownloads,
|
||||||
} from "../scripts/getters";
|
} from "../scripts/getters";
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -29,7 +30,9 @@ import {
|
||||||
<div class="flex gap-4">
|
<div class="flex gap-4">
|
||||||
<Download class="text-primary" size={48} />
|
<Download class="text-primary" size={48} />
|
||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<span id="downloads" class="text-2xl font-semibold">10+</span>
|
<span id="downloads" class="text-2xl font-semibold"
|
||||||
|
>{totalDownloads}+</span
|
||||||
|
>
|
||||||
<div class="text-base-content/60">Downloads</div>
|
<div class="text-base-content/60">Downloads</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -72,15 +75,3 @@ import {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script lang="ts">
|
|
||||||
const dl = document.getElementById("downloads");
|
|
||||||
|
|
||||||
fetch(
|
|
||||||
"https://api.npmjs.org/downloads/point/last-year/@simeonradivoev/gameflow-store",
|
|
||||||
)
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((data) => {
|
|
||||||
dl.textContent = `${data.downloads}+`;
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,80 @@ export const releaseData = await fetch(
|
||||||
return { tag_name: "unknown" };
|
return { tag_name: "unknown" };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function getTotalDownloads(owner: string, repo: string): Promise<number> {
|
||||||
|
let totalDownloads = 0;
|
||||||
|
let cursor: string | null = null;
|
||||||
|
let hasNextPage = true;
|
||||||
|
|
||||||
|
while (hasNextPage) {
|
||||||
|
const res = await fetch("https://api.github.com/graphql", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
...githubHeaders.headers,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
query: `{
|
||||||
|
repository(owner: "${owner}", name: "${repo}") {
|
||||||
|
releases(first: 100${cursor ? `, after: "${cursor}"` : ""}) {
|
||||||
|
pageInfo {
|
||||||
|
hasNextPage
|
||||||
|
endCursor
|
||||||
|
}
|
||||||
|
nodes {
|
||||||
|
releaseAssets(first: 100) {
|
||||||
|
nodes {
|
||||||
|
downloadCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`GitHub API error: ${res.status} ${res.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const json: any = await res.json();
|
||||||
|
|
||||||
|
if (json.errors) {
|
||||||
|
throw new Error(
|
||||||
|
`GraphQL error: ${json.errors.map((e: any) => e.message).join(", ")}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const releases = json.data?.repository?.releases;
|
||||||
|
|
||||||
|
if (!releases) {
|
||||||
|
throw new Error(
|
||||||
|
`Repository ${owner}/${repo} not found or not accessible`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const release of releases.nodes) {
|
||||||
|
for (const asset of release.releaseAssets.nodes) {
|
||||||
|
totalDownloads += asset.downloadCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hasNextPage = releases.pageInfo.hasNextPage;
|
||||||
|
cursor = releases.pageInfo.endCursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalDownloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const totalDownloads = await getTotalDownloads(
|
||||||
|
"simeonradivoev",
|
||||||
|
"gameflow-deck",
|
||||||
|
).catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
|
||||||
export const appContributorsData = await fetch(
|
export const appContributorsData = await fetch(
|
||||||
"https://api.github.com/repos/simeonradivoev/gameflow-deck/contributors",
|
"https://api.github.com/repos/simeonradivoev/gameflow-deck/contributors",
|
||||||
githubHeaders,
|
githubHeaders,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue