Skip to content

Commit 9f0962b

Browse files
refactor: Switch module list from Wiki markdown to JSON API
Replace Wiki markdown parsing with direct JSON API consumption from modules.magicmirror.builders for improved reliability. - Update source to https://modules.magicmirror.builders/data/modules.json - Rewrite parseList() for JSON instead of regex markdown parsing - Auto-download modules.json if missing Solves #336.
1 parent 31eb7d4 commit 9f0962b

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

scripts/download_modules.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const fs = require("node:fs");
1919
const downloadModules = {
2020
defaults: {
2121
modulesFile: path.resolve(__dirname, "../modules.json"), // Path to modules file
22-
sourceUrl: "https://raw.githubusercontent.com/wiki/MagicMirrorOrg/MagicMirror/3rd-Party-Modules.md", // Source url
22+
sourceUrl: "https://modules.magicmirror.builders/data/modules.json", // Source url
2323
refreshRate: 24 * 3600, // Max Refresh of One Day
2424
force: false, // Force the update
2525
callback (result) { console.log(result); } // Callback to run on success or failure
@@ -33,23 +33,28 @@ const downloadModules = {
3333
},
3434

3535
parseList (content) {
36-
const re = /\|\s?\[(.*?)\]\((.*?)\)\s?\|(.*?)\|(.*)\|?/gu;
37-
const modules = [];
36+
try {
37+
const data = JSON.parse(content);
38+
const modules = [];
3839

39-
content.match(re).forEach((line) => {
40-
line.replace(re, (match, name, url, author, desc) => {
41-
const modDetail = {
42-
longname: name.trim(),
43-
id: url.replace(".git", "").replace(/.*\/(.*?\/.*?)$/u, "$1").trim(),
44-
url: url.replace(".git", "").trim(),
45-
author: author.replace(/\[(.*)\]\(.*\)/u, "$1").trim(),
46-
desc: desc.replace(/\|/u, "").trim()
47-
};
48-
modules.push(modDetail);
49-
});
50-
});
40+
if (data.modules && Array.isArray(data.modules)) {
41+
data.modules.forEach((module) => {
42+
const modDetail = {
43+
longname: module.name,
44+
id: module.id,
45+
url: module.url,
46+
author: module.maintainer,
47+
desc: module.description
48+
};
49+
modules.push(modDetail);
50+
});
51+
}
5152

52-
return modules;
53+
return modules;
54+
} catch (error) {
55+
console.error("MODULE LIST ERROR: Failed to parse JSON:", error.message);
56+
return [];
57+
}
5358
},
5459

5560
async getPackages () {
@@ -69,7 +74,7 @@ const downloadModules = {
6974
}
7075
});
7176
} else if (response.status === 401) {
72-
console.error("MODULE LIST ERROR: Could not load module data from wiki. 401 Error");
77+
console.error("MODULE LIST ERROR: Could not load module data from JSON API. 401 Error");
7378
this.config.callback("ERROR_401");
7479
} else {
7580
console.error("MODULE LIST ERROR: Could not load data.", response.statusText);
@@ -93,8 +98,14 @@ const downloadModules = {
9398
this.config.callback("NO_UPDATE_REQUIRED");
9499
}
95100
} catch (err) {
96-
console.error("MODULE LIST ERROR: Could not check last modified time.", err);
97-
this.config.callback("ERROR_CHECKING_LAST_MODIFIED");
101+
// If file doesn't exist or can't be read, download it
102+
if (err.code === "ENOENT") {
103+
console.log("MODULE LIST INFO: modules.json not found, downloading...");
104+
this.getPackages();
105+
} else {
106+
console.error("MODULE LIST ERROR: Could not check last modified time.", err);
107+
this.config.callback("ERROR_CHECKING_LAST_MODIFIED");
108+
}
98109
}
99110

100111
}

0 commit comments

Comments
 (0)