Skip to content

Commit fff1a33

Browse files
authored
feat: use env var GITHUB_TOKEN for API calls (if set) (#65)
1 parent 01b6d34 commit fff1a33

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Based on the original list, we automatically create a website with filter functi
1212

1313
### How do I get my module presented in the new list?
1414

15-
Add your module to [the original list in the wiki](https://github.com/MagicMirrorOrg/MagicMirror/wiki/3rd-party-modules). Twice a day the new list will be updated based on the original list.
15+
Add your module to [the original list in the wiki](https://github.com/MagicMirrorOrg/MagicMirror/wiki/3rd-party-modules). Once a day the new list will be updated based on the original list.
1616

1717
### How do I get my module presented perfectly in the new list?
1818

scripts/updateGitHubApiData.js

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import {getJson} from "./utils.js";
3+
import process from "node:process";
34

45
let queryCount = 0;
56
let maxQueryCount = 58;
@@ -12,8 +13,8 @@ function printProgress (count, total) {
1213
// Function to check whether new data should be retrieved.
1314
function shouldFetch (repository) {
1415
let retrieve = false;
15-
if (repository.url.includes("github.com")) {
16-
if (queryCount < maxQueryCount) {
16+
if (repository.url.includes("github.com") && maxQueryCount > 0) {
17+
if (queryCount < maxQueryCount || process.env.GITHUB_TOKEN) {
1718
retrieve = true;
1819
}
1920
}
@@ -75,6 +76,11 @@ async function updateData () {
7576

7677
sortModuleListByLastUpdate(previousData, moduleList);
7778

79+
const headers = {};
80+
if (process.env.GITHUB_TOKEN) {
81+
headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
82+
}
83+
7884
for (const module of moduleList) {
7985
const repositoryId = module.id;
8086
const repositoryApiUrl = `https://api.github.com/repos/${repositoryId}`;
@@ -85,12 +91,12 @@ async function updateData () {
8591

8692
if (shouldFetchData) {
8793
printProgress(moduleCount, moduleListLength);
88-
const response = await fetch(repositoryApiUrl);
94+
const response = await fetch(repositoryApiUrl, {headers});
8995
const data = await response.json();
9096
queryCount += 1;
9197

9298
const branchUrl = `https://api.github.com/repos/${repositoryId}/commits/${data.default_branch}`;
93-
const branchResponse = await fetch(branchUrl);
99+
const branchResponse = await fetch(branchUrl, {headers});
94100
const branchData = await branchResponse.json();
95101
queryCount += 1;
96102

@@ -129,40 +135,7 @@ async function updateData () {
129135
useHistoricalData(previousData, repositoryId, module, results);
130136
}
131137

132-
// Quick-and-dirty way to include the number of stars for non-GitHub repositories.
133-
if (!module.url.includes("github.com")) {
134-
switch (module.name) {
135-
case "MMM-bergfex":
136-
module.stars = 1;
137-
break;
138-
case "MMM-Flights":
139-
module.stars = 2;
140-
break;
141-
case "MMM-InstagramView":
142-
module.stars = 1;
143-
break;
144-
case "mmm-ratp":
145-
module.stars = 2;
146-
break;
147-
case "MMM-NCTtimes":
148-
module.stars = 1;
149-
break;
150-
case "MMM-RecyclingCalendar":
151-
module.stars = 1;
152-
break;
153-
case "MMM-RepoStats":
154-
module.stars = 2;
155-
break;
156-
case "MMM-YouTubeWebView":
157-
module.stars = 1;
158-
break;
159-
default:
160-
module.stars = 1;
161-
break;
162-
}
163-
// Since far fewer users have accounts with non-GitHub hosts, repos get a small star boost.
164-
module.stars *= 3;
165-
}
138+
setNonGithubStars(module);
166139
}
167140

168141
results.sort((a, b) => a.id.localeCompare(b.id));
@@ -176,12 +149,52 @@ async function updateData () {
176149

177150
fs.writeFileSync(localFilePath, JSON.stringify(updateInfo, null, 2));
178151
fs.writeFileSync("docs/data/modules.stage.2.json", JSON.stringify(sortedModuleList, null, 2));
152+
if (maxQueryCount < queryCount) {
153+
maxQueryCount = 0;
154+
}
179155
console.info("\nGitHub data update completed. queryCount:", queryCount, "maxQueryCount:", maxQueryCount, "results:", results.length, "modules:", moduleListLength);
180156
} catch (error) {
181157
console.error("Error fetching GitHub API data:", error);
182158
}
183159
}
184160

161+
function setNonGithubStars (module) {
162+
// Quick-and-dirty way to include the number of stars for non-GitHub repositories.
163+
if (!module.url.includes("github.com")) {
164+
switch (module.name) {
165+
case "MMM-bergfex":
166+
module.stars = 1;
167+
break;
168+
case "MMM-Flights":
169+
module.stars = 2;
170+
break;
171+
case "MMM-InstagramView":
172+
module.stars = 1;
173+
break;
174+
case "mmm-ratp":
175+
module.stars = 2;
176+
break;
177+
case "MMM-NCTtimes":
178+
module.stars = 1;
179+
break;
180+
case "MMM-RecyclingCalendar":
181+
module.stars = 1;
182+
break;
183+
case "MMM-RepoStats":
184+
module.stars = 2;
185+
break;
186+
case "MMM-YouTubeWebView":
187+
module.stars = 1;
188+
break;
189+
default:
190+
module.stars = 1;
191+
break;
192+
}
193+
// Since far fewer users have accounts with non-GitHub hosts, repos get a small star boost.
194+
module.stars *= 3;
195+
}
196+
}
197+
185198
function useHistoricalData (previousData, repositoryId, module, results) {
186199
// Add the existing data without updating it
187200
const existingRepository = previousData.repositories?.find((repo) => repo.id === repositoryId);

0 commit comments

Comments
 (0)