-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_1page_netskope.com_05.js
654 lines (545 loc) · 20.9 KB
/
get_1page_netskope.com_05.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// export ARCROOT='../arcroot'
'use strict';
const fs = require('fs');
const util = require('util');
const path = require('path');
const axios = require("axios");
const colors = require("colors");
const crypto = require('crypto');
const hashmap = require('hashmap');
const cheerio = require("cheerio");
const execcmd = require('child_process').execFileSync;
const argv = process.execArgv.join();
const isDebug = argv.includes('inspect');
const isVerbs = argv.includes('verbose');
const urlroot = 'https://www.netskope.com/blog/';
const TITLEST = "- Netskope";
const REGULAR = ".blogs_tile";
const FEATURE = ".blogs__featured";
const BODYBLG = ".blog-page__content";
const DATEBLG = ".blog-page__header-date";
const ua_chrm = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36';
var ONEPAGE = false;
var CHCKNEW = false;
var FIRSTRN = false;
if (! process.env.ARCROOT) {
console.log('warning: export ARCROOT first, use ../arcroot as default');
process.env.ARCROOT = '../arcroot';
}
let www = urlroot.match(/http[s]:\/\/(.+?)\//);
let bserver = www[1].replace("www\.", '');
const prjroot = process.env.ARCROOT + '/' + bserver;
const pagroot = process.env.ARCROOT + '/' + bserver + '/page';
const datroot = process.env.ARCROOT + '/' + bserver + '/data';
const blgroot = process.env.ARCROOT + '/' + bserver + '/blog';
const onepage = process.env.ARCROOT + '/' + bserver + '/' + bserver + '.html';
const blogarry = prjroot + '/blogs.array';
const bloghash = prjroot + '/blogs.hash' ;
const blgindex = prjroot + '/index.html' ;
const config = {
headers: { 'User-Agent': ua_chrm },
}
function parse_blog_url($, section){
const posts = $(section);
let blogarr = [];
//console.log($( "li[itemprop='datePublished']").text() );
//console.log(posts.length);
for (let i = 0; i < posts.length; i++) {
let postTitleWrapper = $(posts[i]).find(".blogs_tile-title")[0],
postTitle = $(postTitleWrapper).text();
postTitle = postTitle.replace(/\n/g, '');
postTitle = postTitle.trim();
let title = postTitle.replace(/ /g, '_');
title = title.replace(/[\W]+/g, '');
title = title.replace(/_+/g, '_');
if(title.length < 10) continue;
let postLinkWrapper = $(posts[i]).find("a")[0],
postLink = $(postLinkWrapper).attr("href");
if(! postLink.match('https') ) postLink = www[0] + postLink;
let author = bserver, postDate = 'January 5, 2000', postDesc = title;
let authorWrapper = $(posts[i]).find(".blogs_tile-author")[0];
if(authorWrapper != null )
author = $(authorWrapper).text().replace(/By +/, '').replace(' |', '');
author = author.trim();
postDate = find_post_date($, '.blog-page__header-date');
let postID = "post-" + crypto.createHash('md5').update(author+title, 'utf8').digest('hex').slice(0,6);
let blogFile = dateFormat (new Date(postDate), "%Y-%m-%d", true);
blogFile += '~' + postID + '~' + title;
if(isDebug && isVerbs){
console.log(`${postID.brightGreen}`);
console.log(`${postTitle.yellow}`);
console.log(`${postDate.cyan}`);
console.log(`${author.red}`);
console.log(`${postLink.gray}`);
console.log("----");
}
const item_json = blog_attr(postID, postTitle, author, postLink, postDate, blogFile, postDesc);
//console.log(item_json); process.exit(1);
blogarr.push(item_json);
}
return blogarr;
}
async function find_blog_links(purl){
let num = purl.match(/http[s]:\/\/.+\/(\d+)\//);
let page = pagroot + '/p' + int3(num[1]) + '.html';
let firstp = false;
if (purl.search(/\/1\//) > 0 ){
purl = urlroot; // blog root page with FEATUREed post
firstp = true;
}
var html;
if( ONEPAGE ) {
if(CHCKNEW) {
let resp = await axios.get(purl);
await save_to_file(page, resp.data);
html = resp.data;
}
else{
console.log('reading from: ' + onepage);
html = fs.readFileSync(onepage).toString();
}
}else{
if (fs.existsSync(page) && ! firstp ){ // always read the 1st page
html = fs.readFileSync(page).toString();
}else{
let resp = await axios.get(purl);
await save_to_file(page, resp.data);
html = resp.data;
}
}
var $ = cheerio.load(html)
// parse REGULAR posts
var REGULAR_posts = parse_blog_url($, REGULAR);
if (firstp){ // parse FEATUREd posts in first page
var FEATURE_posts=[];
FEATURE_posts = parse_blog_url($, FEATURE);
REGULAR_posts = [...FEATURE_posts, ...REGULAR_posts];
}
console.log("blog found: %d", REGULAR_posts.length);
return REGULAR_posts;
}
async function save_to_file(file, content){
try {
if (fs.existsSync(file)) {
return;
}else{
await fs.writeFile(file, content, function (err) {if (err) throw err;});
}
} catch(err) {
console.error(err)
}
}
async function save_blog_content(url, file){
let stime = process.hrtime();
let resp = await axios.get(url, config);
var $ = cheerio.load(resp.data)
// parse tags
let btags = $('.blog-page__navigation-back').text().trim();
btags = btags.replace(' < Back', '').replace('< ', '').replace(/ *Back */ig, '');
if(btags.endsWith(',')) btags = btags.slice(0, -1); //remove last ,
// parse date and change file name, when post date not on index page
let bdate = find_post_date($, DATEBLG);
if(file.match('2000-01-20') && bdate != null){
file = file.replace('2000-01-20', bdate);
}
//remove unwanted div/class to keep layout clean
remove_html_sections($);
let title = $("title").text().replace(TITLEST, '-JK CTI');
//let bodyo = $(BODYBLG); // cyolo no clear way to identify main blog, returns 2
let bodym = $(BODYBLG).html() // locate blog main body
// if title/topic not inclued in bodym
let topic = $(".title-page").text();
if(urlroot.match('perimeter81')) bodym = `<br><h1> ${topic} </h1><br>\n` + bodym;
if(bodym == null) bodym = $(".blog-template-grid-expand").html(); // twingate
// no late load
bodym = bodym.replaceAll(/srcset=".*?"/g, ' ');
bodym = bodym.replaceAll(/background-image:url\(.*?\);/g, ' ');
$ = cheerio.load(bodym); // re-parse blog body
const bhead_s =`<!DOCTYPE html>
<html>
<head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="JK" content="SZ Threat Intel" />
<title>${title}</title>
<link rel='stylesheet' id='bootstrap4-css' href='../data/bootstrap.min.css' type='text/css' media='all' />
<link rel='stylesheet' id='divi-plus-styles-css' href='../data/style.min.css' type='text/css' media='all' />
</head><body class="post-template-default single single-post single-format-standard"><br><div class="container">
`;
const bhead_e ='</div></body></html>';
var gg = $('img') // parse all image links and save to local
for(let i=0; i < gg.length; i++ ){
let imgsrc = $(gg[i]).attr('src');
let datsrc = $(gg[i]).attr('data-src');
if(! imgsrc.match(/\w+/)) continue;
let imgurl = imgsrc;
if( imgurl.match(/data:image/) ) imgurl = datsrc;
// skip if already downloaded
let fileimage = imgurl.split('/').slice(-1)[0].split('?')[0];
let filelocal = datroot + '/' + fileimage; // save to
// use local downloaded images
bodym = bodym.replace(imgsrc, '../data/' + fileimage);
if(fs.existsSync(filelocal)) continue;
if(! imgurl.match(/http/)) imgurl = www[0] + imgurl;
const encodedurl = encodeURI(imgurl); // handle special char
try {
const response = await axios({
method: 'GET',
url: encodedurl,
responseType: 'stream',
});
const w = response.data.pipe(fs.createWriteStream(filelocal));
w.on('finish', () => {
process.stdout.write(`.`);
});
} catch (err) {
console.log(" unescaped char: %s", imgurl);
// if fails, try wget
execcmd('wget', [imgurl, '-q', '-O', filelocal], {stdio:'inherit'});
continue;
}
}
console.log("");
// only keep the blog main body, saved space
bodym = bhead_s + bodym + bhead_e;
await save_to_file(file, bodym);
let stopt = process.hrtime(stime);
console.log("-- %s %s %s %s", String(parseInt(bodym.length/1024)+'k').padEnd(7), String(gg.length+'p').padEnd(4), `${(stopt[0] * 1e9 + stopt[1])/1e9}s`, url);
return [btags, bdate, file.split('/').pop()];
}
// firstpage_only for blogs using "load more" style
async function get_all_blog_url2array(urlp, firstpage_only=false, finalpage=0) {
let stime = process.hrtime();
var loadhtml;
if ( fs.existsSync(onepage) ){ // onepage html is saved manually for 1st parsing
ONEPAGE = true;
loadhtml = fs.readFileSync(onepage).toString();
}else{
let resp = await axios.get(urlp, config);
loadhtml = resp.data;
}
var $ = cheerio.load(loadhtml);
var lastpage = 1;
if( ! ONEPAGE ) { // get max page #
if(finalpage > 0) // specified by user
lastpage = finalpage;
else
lastpage = $(".bottompagination .next > a").attr("href").match(/\d+/)[0];
if(isDebug) console.log("\nlast page num:" + lastpage + "\n");
}
// load existing blog urls when checking new - add new to local array
var blogs = [];
if (fs.existsSync(blogarry) && firstpage_only && CHCKNEW){
console.log('read existing blog array');
let buffe = fs.readFileSync(blogarry).toString();
blogs = JSON.parse(buffe);
}
if(blogs.length == 0) FIRSTRN = true; // blog array not created yet
let bfilelist = fs.readdirSync(blgroot).join(' '); // check local blog existence
// find all posts in each blog page
for(let i = 1; i <= lastpage; i++) {
let page = urlroot +'page/' + i + '/';
if( ONEPAGE ) page = urlroot + '/1/';
let pburls = await find_blog_links(page);
if(firstpage_only){ // only check 1st page on new blog
console.log("get %d blogs on 1st page, %d from local array", pburls.length, blogs.length);
pburls.forEach( e => {
let o = JSON.parse(e);
let f = o.idblg + '~' + o.bdesc + '.html';
if(! bfilelist.match(f)){
let action = 'push';
if(FIRSTRN){
blogs.push(e); // add new at end
}else{
blogs.unshift(e); //add new at begin
action = 'unshift';
}
console.log(" %s %s".green, action, f.slice(0, 100) );
}else{
console.log(" skip %s".grey, o.blink);
}
});
}
else{ // full from page 1 to last
blogs = [...blogs, ...pburls];
}
// save blog array for debug
fs.writeFile(blogarry, JSON.stringify(blogs), function (err) { if (err) throw err; });
if(isDebug) console.log('blogs in page %d %d %s', i, blogs.length, page);
if(firstpage_only) break;
}
let stopt = process.hrtime(stime);
console.log(`\nblog array: ${(stopt[0] * 1e9 + stopt[1])/1e9} seconds`.yellow);
}
async function process_blog_cont2file( stopnum = 10000 ){
let stime = process.hrtime();
let buffe = fs.readFileSync(blogarry).toString();
let blogs = JSON.parse(buffe);
var bhmap;
if (fs.existsSync(bloghash)){
buffe = fs.readFileSync(bloghash).toString();
bhmap = new hashmap(JSON.parse(buffe));
}else{
bhmap = new hashmap();
}
let bfilelist = fs.readdirSync(blgroot).join(' ');
// save each individual blog
let tablerow = '';
for(let i = 0, j=1; i < blogs.length; i++) {
if( i+1 > stopnum) break;
try {
let obj = JSON.parse(blogs[i]);
let ofile = blgroot + '/' + obj.bfile + '.html';
let f = obj.idblg + '~' + obj.bdesc + '.html';
let k = obj.authr+' '+obj.bdesc; // hash key
if(bfilelist.match(f)){
obj.btags = bhmap.get(k).btags;
obj.datep = bhmap.get(k).datep;
obj.bfile = bhmap.get(k).bfile;
}else{
[obj.btags, obj.datep, obj.bfile] = await save_blog_content(obj.blink, ofile);
console.log(obj.btags);
}
// add a uniq id
obj.md5sm = crypto.createHash('md5').update(obj.authr+obj.title, 'utf8').digest('hex').slice(0,16);
bhmap.set(k, obj);
var bltgs = '';
if(obj.btags.length > 0)
String(obj.btags).split(',').forEach( e => {
bltgs += `<a class="btn btn-success">${e}</a> `;
});
let rr;
if(rr = bltgs.match(/(.*)( )/)) bltgs = rr[1];
let disp = obj.title.slice(0, 90);
// create index table rows
tablerow += `
<tr >
<td >${j}</td>
<td >${obj.datep}</td>
<td ><a href="blog/${obj.bfile}">${disp}</a></td>
<td >${bltgs}</a></td>
<td ><a href="${obj.blink}" target="_blank" class="extlk"></a></td>
</tr>`;
j++;
//process.stdout.write(`b`.grey);
if( (i+1)%100 == 0 ){ console.log(); }
} catch(err) {
console.log(i.yellow + blogs[i]);
console.error(err)
}
}
// create blogs.hash
fs.writeFile(bloghash, JSON.stringify(bhmap), function (err) { if (err) throw err; });
// create index.html
let tt = blog_index();
fs.writeFile(blgindex, tt[0] + tablerow + tt[1], function (err) { if (err) throw err; });
console.log("\n");
console.log("blog count: " + blogs.length);
let stopt = process.hrtime(stime);
console.log(`total time: ${(stopt[0] * 1e9 + stopt[1])/1e9} seconds`.cyan);
}
function blog_index(){
let tt = [];
tt[0] = `<!DOCTYPE html>
<html lang="en-US"><head><meta charset="utf-8">
<meta name="JK" content="SZ Threat Intel" />
<title>blog index</title>
<style>
.styled-table {
border-collapse: collapse;
margin: auto;
font-size: 0.9em;
font-family: Segoe UI,Arial,sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
border-radius: 5px 5px 0 0;
overflow: hidden;
}
.styled-table caption {
margin-top: 1em;
margin-bottom: 1em;
font-size: 2em;
font-weight: bold;
text-align: center;
color: #327A81;
}
.styled-table thead tr {
background-color: #91CED4;
color: #ffffff;
text-align: left;
font-size: 1.1em;
text-decoration:none;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #91CED4;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #91CED4;
}
a { text-decoration: none; color: #000}
a:hover { color: Orange ; }
.btn {
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
padding: 0.15rem 0.5rem;
border-radius: 0.25rem;
text-decoration:none;
color: #fff;
background-color: #67D5AC;
border-color: #67D5AC; }
.btn-success:hover {
color: #fff;
background-color: #91CED4;
border-color: #25BCAA; }
}
</style></head><body>
<table class="styled-table">
<caption>${bserver}</caption>
<thead>
<tr >
<th style="border-radius: 5px 0 0 0;">seq</th>
<th >Date</th>
<th >Title</th>
<th >Tags</th>
<th style="border-radius: 0 5px 0 0;">org</th>
</tr></thead>
<tbody>
`;
// create svg and add to <a> https://seanyeh.com/pages/creating_svgs_in_javascript/
tt[1] = `</tbody></table>
<script type="text/javascript">
var extlk = document.getElementsByClassName('extlk');
for (var i=0; i < extlk.length; i++) {
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("class", "octicon octicon-star");
svg.setAttribute("viewBox", "0 0 16 16");
svg.setAttribute("width", "16");
svg.setAttribute("height", "16");
svg.setAttribute("fill", "#67D5AC");
var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", "M15 2a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2zM0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2zm5.854 8.803a.5.5 0 1 1-.708-.707L9.243 6H6.475a.5.5 0 1 1 0-1h3.975a.5.5 0 0 1 .5.5v3.975a.5.5 0 1 1-1 0V6.707l-4.096 4.096z");
svg.appendChild(path);
extlk[i].textContent ='';
extlk[i].appendChild(svg);
}
</script></body></html>`;
return tt;
}
function blog_attr(...attr) {
if(attr[6] == undefined ) attr[6]=''
return `{
"idblg" : "${attr[0]}",
"title" : "${attr[1]}",
"authr" : "${attr[2]}",
"blink" : "${attr[3]}",
"datep" : "${attr[4]}",
"bfile" : "${attr[5]}",
"bdesc" : "${attr[6]}"
}`;
}
function int3(num, size=3) {
let s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
function find_post_date($, selector){
let postDate = $(selector).text().trim();
if(! postDate.match(/\w+/) ) postDate = 'January 20, 2000',
postDate = postDate.replace("th", '');
postDate = postDate.replace("st", '');
postDate = postDate.replace("nd", '');
postDate = postDate.replace("rd", '');
if(postDate.match(/^\d+ /)){ // change DD WWW YYYY
postDate = postDate.replace(/'/g, ' ');
let datetemp = postDate.split(" ");
postDate = datetemp[1] + ' ' + datetemp[0] + ', ' + datetemp[2];
}
if(! postDate.match(',')){ // if no ',' format to MM DD, YYYY
let datetemp = postDate.split(" ");
postDate = datetemp[0] + ' ' + datetemp[1] + ', ' + datetemp[2];
}
return dateFormat (new Date(postDate), "%Y-%m-%d", true);
}
function remove_html_sections($){
$(".related-wrapper").remove(); // p81
$(".elementor-grid").remove(); // cyolo
$(".elementor-author-box__avatar").remove();
$(".elementor-post-info").remove();
$(".blogtitle").remove(); // banyan
$(".blog-page__social-share").remove(); // netskope
$(".blog-page__social-icons-inner").remove();
}
function dateFormat (date, fstr, utc) {
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
case '%m': m = 1 + date[utc + 'Month'] (); break;
case '%d': m = date[utc + 'Date'] (); break;
case '%H': m = date[utc + 'Hours'] (); break;
case '%M': m = date[utc + 'Minutes'] (); break;
case '%S': m = date[utc + 'Seconds'] (); break;
default: return m.slice (1); // unknown code, remove %
}
// add leading zero if required
return ('0' + m).slice (-2);
});
}
// --- start ---
// create blog data folder
!fs.existsSync(prjroot) && fs.mkdirSync(prjroot);
!fs.existsSync(pagroot) && fs.mkdirSync(pagroot);
!fs.existsSync(datroot) && fs.mkdirSync(datroot);
!fs.existsSync(blgroot) && fs.mkdirSync(blgroot);
let css = ['bootstrap.min.css', 'style.min.css'];
css.forEach( e => {
if (fs.existsSync(datroot + '/' + e)) return;
fs.copyFile("node_modules/" + e, datroot + '/' + e,
fs.constants.COPYFILE_EXCL, (err) => {
if (err) { console.log("error found:", err); }
}
);
});
const arg = process.argv.slice(2);
switch (arg[0]) {
case '--save-urls':
console.log('== save-urls ==');
get_all_blog_url2array(urlroot, true);
break;
case '--check-new':
console.log('== check-new ==');
CHCKNEW = true;
get_all_blog_url2array(urlroot, true, 1);
break;
case '--save-blog':
console.log('== save-blog ==');
if(arg[1] > 0){
console.log("process first " + arg[1] + " blogs");
process_blog_cont2file(arg[1]);
}else{
if(arg.length ==2 && arg[1].match(/[a-zA-Z]/)) break;
console.log("process all blogs");
process_blog_cont2file();
}
break;
case '--help':
default:
let ndjs = path.basename(__filename);
console.log(`\nUsage:
node --inspect ${ndjs} --save-urls
node --inspect ${ndjs} --save-blog NUM
node --inspect ${ndjs} --check-new`);
}
// publish date and tags in main blog page not in index
// so if need to update index array/hash, blog file need be deleted