Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaBalseiro committed Nov 2, 2023
1 parent 953b593 commit 1784b43
Showing 1 changed file with 188 additions and 128 deletions.
316 changes: 188 additions & 128 deletions node-w3c/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,178 +17,238 @@
// "items": "items=" + config.itemsValue
// }


var w3capi = require('node-w3capi');
const fs = require('fs');
var w3capi = require("node-w3capi");
const fs = require("fs");

const handleUsers = async function (err, data) {
const users = [];
const orgs = [];
const orgsMap = {};

if (err) return console.error("[ERROR]", err);

for (let i = 0; i < data.length; i++) {
const user = data[i];
try {
console.log('# GET ' + user.href);
const userData = await fetch(user.href).then(res => res.json());
console.log('# GET ' + userData._links.affiliations.href);
const a = await fetch(userData._links.affiliations.href).then(res => res.json());
let affiliations = [];
if (a._links.affiliations) {
await Promise.all(a._links.affiliations.map(async (af) => {
let aff = await fetch(af.href).then(res => res.json());
affiliations.push(aff);

if (!orgsMap[aff.id]) {
orgsMap[aff.id] = {
...aff,
orgUsers: []
};
}
orgsMap[aff.id].orgUsers.push({
name: userData.name,
id: userData.id,
});
}));
const users = [];
const orgs = [];
const orgsMap = {};

if (err) return console.error("[ERROR]", err);

for (let i = 0; i < data.length; i++) {
const user = data[i];
try {
console.log("# GET " + user.href);
const userData = await fetch(user.href).then((res) => res.json());
console.log("# GET " + userData._links.affiliations.href);
const a = await fetch(userData._links.affiliations.href).then((res) =>
res.json()
);
let affiliations = [];
if (a._links.affiliations) {
await Promise.all(
a._links.affiliations.map(async (af) => {
let aff = await fetch(af.href).then((res) => res.json());
affiliations.push(aff);

if (!orgsMap[aff.id]) {
orgsMap[aff.id] = {
...aff,
orgUsers: [],
};
}
users.push({
name: userData.name,
id: userData.id,
affiliations,
orgsMap[aff.id].orgUsers.push({
name: userData.name,
id: userData.id,
});
} catch (e) {
console.log(e);
}
})
);
}
users.push({
name: userData.name,
id: userData.id,
affiliations,
});
} catch (e) {
console.log(e);
}
}

for (const key in orgsMap) {
if (Object.hasOwnProperty.call(orgsMap, key)) {
orgs.push(orgsMap[key]);
}
for (const key in orgsMap) {
if (Object.hasOwnProperty.call(orgsMap, key)) {
orgs.push(orgsMap[key]);
}
}

fs.writeFileSync('users.json', JSON.stringify(users, null, 2));
fs.writeFileSync('orgs.json', JSON.stringify(orgs, null, 2));
fs.writeFileSync("users.json", JSON.stringify(users, null, 2));
fs.writeFileSync("orgs.json", JSON.stringify(orgs, null, 2));

var html = showOutput(users, orgs, { name: "W3C Solid Community Group", description: "test"});
var html = showOutput(users, orgs, {
name: "W3C Solid Community Group",
description: "test",
});

fs.writeFileSync('participants.html', html);
fs.writeFileSync("participants.html", html);
};


w3capi.group(110151).users().fetch(handleUsers);


function showOutput(users, orgs, groupData) {
console.log('------------showOutput:');
var tr = [];

users.forEach(function (user) {
console.log("### " + user.name);

var userId = user.id;
var userIdAboutType = ' about="https://www.w3.org/users/' + userId + '" id="' + userId + '" typeof="schema:Person"'

var name = '<span property="schema:name">' + user.name + '</span>';

var affiliations = [];
user.affiliations.forEach(function (affiliation) {
affiliations.push(affiliation.name);
});
affiliations = affiliations.join(', ');
console.log("------------showOutput:");
var tr = [];

users.forEach(function (user) {
console.log("### " + user.name);

var userId = user.id;
var userIdAboutType =
' about="https://www.w3.org/users/' +
userId +
'" id="' +
userId +
'" typeof="schema:Person"';

var name = '<span property="schema:name">' + user.name + "</span>";

tr.push('<tr><td' + userIdAboutType + '>' + name + '</td><td>' + affiliations + '</td></tr>');
var affiliations = [];
user.affiliations.forEach(function (affiliation) {
affiliations.push(affiliation.name);
});

var tfoot = [];
tfoot.push('<dl>');
tfoot.push('<dt>Data source</dt><dd><a href="https://api.w3.org/">https://api.w3.org/</a></dd>');
tfoot.push('<dt>Number of participants</dt><dd>' + users.length + '</dd>');
tfoot.push('<dt>Number of organisations</dt><dd>' + orgs.length + '</dd>');
tfoot.push('</dl>');

var html = '';
html = '<table id="participants"><caption>' + groupData.name + ' Participants</caption><thead><tr><th>Individual</th><th>Affiliation</th></tr></thead><tbody>' + tr.join('') + '</tbody><tfoot><tr><td colspan="2">' + tfoot.join('') + '</td></tr></tfoot></table>';

html = '<article>' + html + "\n\n" + showOrganizations(orgs) + '</article>'

var o = {
'prefixes': {
'schema': 'http://schema.org/'
}
}
affiliations = affiliations.join(", ");

tr.push(
"<tr><td" +
userIdAboutType +
">" +
name +
"</td><td>" +
affiliations +
"</td></tr>"
);
});

//TODO: Use info from config when available for title
html = createHTML('W3C Solid Community Group Participants', html, o)
var tfoot = [];
tfoot.push("<dl>");
tfoot.push(
'<dt>Data source</dt><dd><a href="https://api.w3.org/">https://api.w3.org/</a></dd>'
);
tfoot.push("<dt>Number of participants</dt><dd>" + users.length + "</dd>");
tfoot.push("<dt>Number of organisations</dt><dd>" + orgs.length + "</dd>");
tfoot.push("</dl>");

var html = "";
html =
'<table id="participants"><caption>' +
groupData.name +
" Participants</caption><thead><tr><th>Individual</th><th>Affiliation</th></tr></thead><tbody>" +
tr.join("") +
'</tbody><tfoot><tr><td colspan="2">' +
tfoot.join("") +
"</td></tr></tfoot></table>";

html = "<article>" + html + "\n\n" + showOrganizations(orgs) + "</article>";

var o = {
prefixes: {
schema: "http://schema.org/",
},
};

//TODO: Use info from config when available for title
html = createHTML("W3C Solid Community Group Participants", html, o);

console.log(html);

return html;
}

console.log(html);

return html;
}

function showOrganizations(orgs) {
var orgsTable = [];

orgs.forEach(function (org) {
var orgId = org.id;
var orgInfo = org.name;

var orgUsers = [];
org.orgUsers.forEach(function (orgUser) {
orgUsers.push(orgUser.name)
});
orgUsers = sortToLower(orgUsers).join(", ")
function showOrganizations(orgs) {
var orgsTable = [];

orgsTable.push('<tr><td id="' + orgId +'">' + orgInfo + '</td><td>' + orgUsers + '</td><td>' + org.orgUsers.length + '</td></tr>');
orgs.forEach(function (org) {
var orgId = org.id;
var orgInfo = org.name;

var orgUsers = [];
org.orgUsers.forEach(function (orgUser) {
orgUsers.push(orgUser.name);
});

var tfoot = [];
tfoot.push('<dl>');
tfoot.push('<dt>Data source</dt><dd><a href="https://api.w3.org/">https://api.w3.org/</a></dd>');
tfoot.push('</dl>');

orgsTable = '<table id="organizations"><caption>Organizations</caption><thead><tr><td>Name</td><td>Participants</td><td>Number of participants</td></thead><tbody>' + orgsTable.join('') + '</tbody>' + tfoot.join('') + '</table>';

return orgsTable;
}
orgUsers = sortToLower(orgUsers).join(", ");

orgsTable.push(
'<tr><td id="' +
orgId +
'">' +
orgInfo +
"</td><td>" +
orgUsers +
"</td><td>" +
org.orgUsers.length +
"</td></tr>"
);
});

var tfoot = [];
tfoot.push("<dl>");
tfoot.push(
'<dt>Data source</dt><dd><a href="https://api.w3.org/">https://api.w3.org/</a></dd>'
);
tfoot.push("</dl>");

orgsTable =
'<table id="organizations"><caption>Organizations</caption><thead><tr><td>Name</td><td>Participants</td><td>Number of participants</td></thead><tbody>' +
orgsTable.join("") +
"</tbody>" +
tfoot.join("") +
"</table>";

return orgsTable;
}

//from dokieli uri.js
function sortToLower(array) {
return array.sort(function(a, b) {
return array.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
}

//from dokieli doc.js (with modification to include meta and links to CSS)
function createHTML(title, main, options) {
title = title || '';
title = title || "";
options = options || {};
var prefix = ('prefixes' in options && Object.keys(options.prefixes).length > 0) ? ' prefix="' + getRDFaPrefixHTML(options.prefixes) + '"' : '';
var lang = options.lang || 'en';
var prefix =
"prefixes" in options && Object.keys(options.prefixes).length > 0
? ' prefix="' + getRDFaPrefixHTML(options.prefixes) + '"'
: "";
var lang = options.lang || "en";
lang = ' lang="' + lang + '" xml:lang="' + lang + '"';
lang = ('omitLang' in options) ? '' : lang;
lang = "omitLang" in options ? "" : lang;

return '<!DOCTYPE html>\n\
<html' + lang + ' xmlns="http://www.w3.org/1999/xhtml">\n\
return (
"<!DOCTYPE html>\n\
<html" +
lang +
' xmlns="http://www.w3.org/1999/xhtml">\n\
<head>\n\
<meta charset="utf-8" />\n\
<meta content="width=device-width, initial-scale=1" name="viewport" />\n\
<link href="https://dokie.li/media/css/basic.css" media="all" rel="stylesheet" title="Basic" />\n\
<link href="https://dokie.li/media/css/dokieli.css" media="all" rel="stylesheet" />\n\
<title>' + title + '</title>\n\
<title>' +
title +
"</title>\n\
</head>\n\
<body' + prefix + '>\n\
<body" +
prefix +
">\n\
<main>\n\
' + main + '\n\
" +
main +
"\n\
</main>\n\
</body>\n\
</html>\n\
';
"
);
}

//from dokieli doc.js
function getRDFaPrefixHTML(prefixes){
return Object.keys(prefixes).map(function(i){ return i + ': ' + prefixes[i]; }).join(' ');
function getRDFaPrefixHTML(prefixes) {
return Object.keys(prefixes)
.map(function (i) {
return i + ": " + prefixes[i];
})
.join(" ");
}

0 comments on commit 1784b43

Please sign in to comment.