1
+ const crypto = require ( 'crypto' ) ;
1
2
const fs = require ( 'fs' ) ;
2
3
const path = require ( 'path' ) ;
3
4
const axios = require ( 'axios' ) ;
4
5
const yaml = require ( 'js-yaml' ) ;
5
6
7
+ const ASSETS_HOST = "assets.developers.italia.it" ;
8
+
6
9
const normalizeRepoUrl = ( url ) => url . toLowerCase ( ) . replace ( / .g i t $ / , '' ) ;
7
10
const absoluteUrl = ( url , repo ) => {
8
11
if ( ! url || ( / ^ h t t p ( s ) ? : \/ \/ / i) . test ( url ) ) {
@@ -22,6 +25,17 @@ const absoluteUrl = (url, repo) => {
22
25
}
23
26
} ;
24
27
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
+
25
39
const addSlug = ( software ) => ( { ...software , slug : software . id } ) ;
26
40
const addAliases = ( software ) => {
27
41
const aliases = [ normalizeRepoUrl ( software . url ) , ...software . aliases . map ( a => normalizeRepoUrl ( a ) ) ] ;
@@ -58,7 +72,7 @@ function toElasticSearchBulkFile(software, filename) {
58
72
software
59
73
. map ( software => addSlug ( software ) )
60
74
. 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 ) ) } } ) )
62
76
. forEach ( s => {
63
77
const metadata = {
64
78
'index' : {
@@ -90,18 +104,18 @@ async function run() {
90
104
. map ( software => addAliases ( addSlug ( software ) ) )
91
105
. map ( software => addSlug ( software ) )
92
106
. 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 ) ) } } ) ) ;
94
108
95
109
data . forEach ( software => {
96
- Object . keys ( software . publiccode . description ) . forEach ( lang => {
110
+ Object . keys ( software . publiccode . description ) . forEach ( ( lang ) => {
97
111
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
+ } ) ;
100
114
} ) ;
101
115
102
116
// Remove the "url" key as it's used by Jekyll to hold the generated page's URL
103
117
// 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 ) ;
105
119
106
120
fs . writeFileSync ( '_data/crawler/software.yml' , yaml . dump ( jekyll ) ) ;
107
121
}
0 commit comments