Skip to content

Commit ab1658d

Browse files
committed
1 parent b0d6f98 commit ab1658d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

scripts/get-software.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
const crypto = require('crypto');
12
const fs = require('fs');
23
const path = require('path');
34
const axios = require('axios');
45
const yaml = require('js-yaml');
56

7+
const ASSETS_HOST = "assets.developers.italia.it";
8+
69
const normalizeRepoUrl = (url) => url.toLowerCase().replace(/.git$/, '');
710
const absoluteUrl = (url, repo) => {
811
if (!url || (/^http(s)?:\/\//i).test(url)) {
@@ -22,6 +25,17 @@ const absoluteUrl = (url, repo) => {
2225
}
2326
};
2427

28+
const assetsImage = (url) => {
29+
if (!url) {
30+
return url;
31+
}
32+
33+
const hash = crypto.createHash('sha1').update(url, 'utf8').digest('hex');
34+
const ext = path.extname(url);
35+
36+
return `https://${ASSETS_HOST}/${hash.slice(0, 2)}/${hash.slice(2)}${ext}`;
37+
};
38+
2539
const addSlug = (software) => ({ ...software, slug: software.id });
2640
const addAliases = (software) => {
2741
const aliases = [normalizeRepoUrl(software.url), ...software.aliases.map(a => normalizeRepoUrl(a))];
@@ -58,7 +72,7 @@ function toElasticSearchBulkFile(software, filename) {
5872
software
5973
.map(software => addSlug(software))
6074
.map(software => addPubliccodeDict(software))
61-
.map(software => ({ ...software, publiccode: { ...software.publiccode, logo: absoluteUrl(software.publiccode.logo, software.url) } }))
75+
.map(software => ({ ...software, publiccode: { ...software.publiccode, logo: assetsImage(absoluteUrl(software.publiccode.logo, software.url)) } }))
6276
.forEach(s => {
6377
const metadata = {
6478
'index': {
@@ -90,18 +104,18 @@ async function run() {
90104
.map(software => addAliases(addSlug(software)))
91105
.map(software => addSlug(software))
92106
.map(software => addPubliccodeDict(software))
93-
.map(software => ({ ...software, publiccode: { ...software.publiccode, logo: absoluteUrl(software.publiccode.logo, software.url) } }));
107+
.map(software => ({ ...software, publiccode: { ...software.publiccode, logo: assetsImage(absoluteUrl(software.publiccode.logo, software.url)) } }));
94108

95109
data.forEach(software => {
96-
Object.keys(software.publiccode.description).forEach(lang => {
110+
Object.keys(software.publiccode.description).forEach((lang) => {
97111
const desc = software.publiccode.description[lang];
98-
software.publiccode.description[lang].screenshots = desc.screenshots?.map(ss => absoluteUrl(ss, software.url));
99-
})
112+
software.publiccode.description[lang].screenshots = desc.screenshots?.map((ss) => assetsImage(absoluteUrl(ss, software.url)));
113+
});
100114
});
101115

102116
// Remove the "url" key as it's used by Jekyll to hold the generated page's URL
103117
// and Searchyll uses it to compare against when the ignore: options are set in _config.yml.
104-
const jekyll = data.map(({ url, ...rest }) => rest)
118+
const jekyll = data.map(({ url, ...rest }) => rest);
105119

106120
fs.writeFileSync('_data/crawler/software.yml', yaml.dump(jekyll));
107121
}

0 commit comments

Comments
 (0)