forked from teles/awesome-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
159 lines (136 loc) · 4.36 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
GLOBAL = {};
GLOBAL.endpoint_url = "https://sheetsu.com/apis/1878d9c1";
GLOBAL.template_url = "template.md";
GLOBAL.readme_url = "readme.md";
var http = require("https");
var handlebars = require('handlebars');
var fs = require('fs');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
handlebars.registerHelper('equal', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlebars Helper equal needs 2 parameters");
if( lvalue!=rvalue ) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
handlebars.registerHelper('lastUpdateBadge', function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
today = dd+' '+ monthNames[mm]+' '+yyyy;
today = encodeURIComponent(today);
var markdownBadge = "![Last update](https://img.shields.io/badge/last%20update-"+today+"-green.svg?style=flat-square)";
return new handlebars.SafeString(markdownBadge);
});
var request = http.get(GLOBAL.endpoint_url, function (response) {
var buffer = "";
var data;
var result;
response.on("data", function (chunk) {
buffer += chunk;
});
response.on("end", function (err) {
data = JSON.parse(buffer);
var results = data.result;
var links = parseResultToLinks(results);
var categoriesMap = arrayLinksToMap(links);
var tagsMap = arrayLinksToTags(links);
imprimeTagsParaREADME(tagsMap);
imprimeLinksParaREADME(categoriesMap);
});
});
function Link(title, href, description, category, language, tags){
this.title = title;
this.href = href;
this.description = description;
this.category = category;
this.language = language;
this.tags = tags;
}
function Category(name, link){
this.name = name;
this.link = link;
}
function Tag(name, link){
this.name = name;
this.link = link;
}
function arrayLinksToMap(links){
var categoriesMap = {};
for(var i=0; i < links.length; i++) {
var link = links[i];
var categoryName = link.category.toString().trim();
var category = new Category(categoryName, link);
if(!categoriesMap[categoryName]){
categoriesMap[categoryName] = [category];
}
else{
categoriesMap[categoryName].push(category)
}
}
return categoriesMap;
}
function arrayLinksToTags(links){
var tagsMap = {};
for(var i=0; i < links.length; i++){
var link = links[i];
link.tags.map(function(tag){
if(tag.toString().length > 1){
var tagName = tag.toString().trim();
var tag = new Tag(tagName, link);
}
if(!tagsMap[tagName]){
tagsMap[tagName] = [tag];
}
else{
tagsMap[tagName].push(tag)
}
});
}
return tagsMap;
}
function parseResultToLinks(results){
var links = [];
for(var i=results.length-1; i >= 0; i--){
var result = results[i];
var title = result['Título'];
var href = result['Link'];
var description = result['Descrição'];
var category = result['Categoria'];
var language = result['Idioma'];
var tags = result['Tags'].split(",");
var link = new Link(title, href, description, category, language, tags);
links.push(link);
}
return links;
}
function imprimeTagsParaREADME(tagsMap){
var i = 0;
for (var tags in tagsMap) {
var tagName = tagsMap[tags][0].name;
if (tagsMap.hasOwnProperty(tags) && typeof(tagName)!== 'undefined') {
// creates the tag folder if it doesnt exists
if (!fs.existsSync(tagName)){
fs.mkdirSync(tagName);
}
}
}
}
function imprimeLinksParaREADME(links){
fs.readFile(GLOBAL.template_url, 'utf-8', function(error, source){
var template = handlebars.compile(source);
var markdown = template(links);
fs.writeFile(GLOBAL.readme_url, markdown, function(err) {
if(err) {
return console.log(err);
}
});
});
}