-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkyGetWork.js
512 lines (489 loc) · 13 KB
/
mkyGetWork.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
const playwright = require('playwright');
const fs = require('fs');
var request = require('request');
var wID = null;
var sKey = null;
var jID = null;
var pRate = 5;
var timer = null;
try {
let rawdata = fs.readFileSync('config.json');
let cred = JSON.parse(rawdata);
login(cred.user,cred.passw);
}
catch(er){
console.log('config.json file not found or invalid');
}
function getWork(){
var pJ = {
workerID: wID,
jobType: 'dredgeVid'
}
request.post({
headers: {'content-type' : 'application/json'},
uri: 'https://www.bitmonky.com/whzon/dredge/dredgeGetWork.php',
json: pJ
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.statusCode);
try {
j = body;
jID = j.workID;
if (pRate != j.pRate){
pRate = j.pRate;
console.log('Changing Poll Rate To: ',j.pRate);
}
if (j.result){
if(j.work.action == 'fetchWScrShot'){
console.log(j);
fetchWScrShot(j);
}
else if(j.work.action == 'fetchDoc'){
console.log(j);
fetchDoc(j);
}
else if(j.work.action == 'dredgeVid'){
console.log(j);
dredgeYT(j);
}
else {
console.log('Action Not Found: ',j.work.action);
pollForWork();
}
}
else {
console.log(j.msg);
pollForWork();
}
}
catch (err) {
console.log('Get Work Error: ',err)
pollForWork();
}
}
else {
console.log('Error',error);
pollForWork();
}
}
);
}
function pollForWork(){
console.log("Waiting For More Work");
clearTimeout(timer);
timer = setTimeout( ()=>{
getWork();
},pRate*1000);
}
function postWork(j){
j.sKey = sKey;
j.wID = wID;
j.version = "nanbot1.0";
console.log('Posting Work: ');
request.post({
headers: {'content-type' : 'application/json'},
uri: 'https://www.bitmonky.com/whzon/dredge/dredgePostWork.php',
json: j
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('Post Work Response: ',response.statusCode);
console.log('message: ',body);
pollForWork();
}
else {
console.log('Post Work Error');
pollForWork();
}
});
}
async function login(uid,password){
wID = await doLogin(uid,password);
console.log(wID);
var j = null;
try {
j = JSON.parse(wID);
if (j.result == "OK"){
wID = j.MUID;
sKey = j.token;
console.log('logged In As: ' + wID);
console.log('Session Key: ' + sKey);
getWork();
}
else {
console.log('Login Failed');
}
}
catch(err){console.log('LoginFailed - JSON parse error:',wID);}
}
async function doLogin(uid,password){
return new Promise(async(resolve,reject)=>{
var result = null;
await request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'https://www.gogominer.com/whzon/mbr/nanoLogin.php',
body: 'femail='+uid+'&fpassword='+password
},
await function (error, response, body) {
if (!error && response.statusCode == 200) {
result = body;
}
else {
result = response.statusCode;
}
resolve (result);
});
});
}
async function dredgeYT(J) {
console.log('try fetching :',J.work.url);
var j = null;
try {
const browser = await playwright.chromium.launch({
headless: true // set this to true
});
var presult = false;
const page = await browser.newPage({ userAgent: J.work.uAgent });
await page.setViewportSize({ width: parseInt(J.work.width), height: parseInt(J.work.height) }); // set screen shot dimention
var hres = await page.goto('https://youtube.com/watch?v='+J.work.url);
if (hres){
console.log("Status: ", hres.status());
if (hres.status() == 200){
presult = true;
}
}
if (presult){
var html = await page.content();
console.log('html:'+typeof(html));
html = html.replace(/'/g,"\"");
html = html.replace(/\/>/g,">");
html = html.replace(/ >/g,">");
html = html.replace(/< /g,"<");
html = html.replace(/\n/g," ");
//html = trim(preg_replace('/\s\s+/', ' ', html));
var response = hres.status();
if (html.indexOf('"simpleText":"This video has been removed by the uploader"') > 0){
response = 304;
}
j = {
result : presult,
response : response,
job : J,
title : sGetTag('"title":"','",',html),
chanID : sGetTag('"channelId":"','"',html),
pDate : sGetTag('"publishDate":"','"',html),
author : sGetTag('"author":"','",',html),
channel : sGetTag('"ownerChannelName":"','",',html),
desc : sGetTag('"shortDescription":"','",',html)
};
postWork(j);
}
else {
postWork({result:false,response:hres.status(),job:J});
}
//await page.screenshot({ path: 'my_screenshot.png' })
await browser.close();
}
catch(er){
j = {
result : false,
job : J,
response : 000
}
}
postWork(j);
}
async function fetchDoc(J) {
console.log('try fetching :',J.work.url);
var j = null;
try {
let domain = (new URL(J.work.url));
domain = domain.hostname;
const browser = await playwright.chromium.launch({
headless: true // set this to true
});
//const page = await browser.newPage({ userAgent: J.uAgent});
const page = await browser.newPage(
{ userAgent: J.work.uAgent,
storageState: {cookies:[{name:"CONSENT",value:"PENDING+999",domain:domain,path:"/"}]}
});
await page.setViewportSize({ width: parseInt(J.work.width), height: parseInt(J.work.height) });
var hres = await page.goto(J.work.url);
var presult = false;
if (hres){
console.log("Status: ", hres.status());
if (hres.status() == 200){
presult = true;
}
}
var html = await page.content();
j = {
result : presult,
job : J,
response : hres.status(),
html : html
};
await browser.close();
}
catch(er){
j = {
result : false,
job : J,
response : 000
}
}
postWork(j);
}
async function fetchWScrShot(J,res=null) {
console.log('try fetching :',J.work.url);
var j = null;
try {
var domain = null;
domain = new URL(J.work.url);
domain = domain.hostname;
const browser = await playwright.chromium.launch({
headless: true
});
const page = await browser.newPage(
{ userAgent: J.uAgent,
storageState: {cookies:[{name:"CONSENT",value:"PENDING+999",domain:domain,path:"/"}]}
});
var hres = null;
var scrshot = null;
try {
await page.setDefaultTimeout(300000);
await page.setViewportSize({ width: 1280, height: 800 }); // set screen shot dimention
var hres = await getPage(page,J.work.url);
}
catch(er){
console.log('Failed To Fetch Document');
}
if (!hres){
j = {
result : false,
mFound : false,
job : J,
response : 999,
data : null,
scrshot : null,
title : null,
desc : null,
ogImg : null,
furl : null
};
await browser.close()
postWork(j);
}
else {
var takeshot = null;
var scrShot = null;
try {
takeshot = await page.screenshot();
scrshot = takeshot.toString('base64');
console.log("scrshot: length: ",scrshot.toString('base64').length);
}
catch(er) {console.log('Screen Shot Failed');}
var presult = false;
var rstatus = '999';
console.log("Status: ", hres.status());
if (hres.status() == 200 ){
presult = true;
}
rstatus = hres.status();
console.log ('Page ResponseCode: ',rstatus);
//await page.waitForTimeout(5000);
var title = null;
var title = null;
try {title = await page.title();}
catch(er){
console.log('page.title failed');
title="Title Not Found...";
}
console.log('title',title);
const furl = await page.url();
console.log('Final URL:',furl);
const data = await page.evaluate(() => {
try {
const metas = document.querySelectorAll("meta");
const mname = Array.from(metas).map((v) => v.name);
const mcontent = Array.from(metas).map((v) => v.content);
const mTag = Array.from(metas).map((v) => v.outerHTML);
const images = document.querySelectorAll("img");
const urls = Array.from(images).map((v) => v.src);
const height = Array.from(images).map((v) => v.height);
const widths = Array.from(images).map((v) => v.width);
return {urls : urls,h:height,w:widths,mname:mname,mcontent:mcontent,mTag:mTag} ;
}
catch(er) {
return {urls : null,h:null,w:null,mname:null,mcontent:null,mTag:null} ;
}
});
var desc = null;
var fdata = null;
if (typeof data !== 'undefined'){
fdata = data;
desc = getMetaDesc(data);
console.log("Description: ",desc);
if (!title || title==''){
title = getOgTitle(data);
}
//var html = await page.content();
}
var metaFound = true;
if (!title || title == '' || !desc || desc == ''){
scrshot = "empty";
metaFound = false;
console.log("No info: nulling screen shot",scrshot);
}
j = {
result : presult,
mFound : metaFound,
job : J,
response : rstatus,
data : fdata,
scrshot : scrshot,
title : rawUrlEncode(title),
desc : rawUrlEncode(desc),
ogImg : rawUrlEncode(getMetaContent(fdata,'og:image')),
furl : rawUrlEncode(furl)
};
console.log('og:image ->',j.ogImg);
await browser.close()
postWork(j);
}
}
catch(er){
j = {
result : false,
job : J,
response : 000
}
}
postWork(j);
}
function getPage(page, url) {
var res = null;
return new Promise(async(resolve,reject)=>{
try {
res = await page.goto(url, {
timeout: 300 * 1000,
waitUntil: 'load',
});
resolve(res);
}
catch(er){
console.log("myERROR:",er);
resolve(res);
}
});
}
function getMetaContent(j,str){
if ( typeof j === 'undefined'){
return null;
}
if (!j){return;}
var descIndex = null;
if (typeof j.mTag === 'undefined'){
return null;
}
j.mTag.forEach((item,index)=>{
var name = item.toLowerCase();
if (name.indexOf(str) > 0){
descIndex = index;
}
});
if (descIndex){
return j.mcontent[descIndex];
}
return null;
}
function getOgTitle(j){
var descIndex = null;
if (typeof j.mTag === 'undefined'){
return "";
}
j.mTag.forEach((item,index)=>{
var name = item.toLowerCase();
if (name.indexOf('og:title') > 0){
descIndex = index;
}
});
if (descIndex){
return j.mcontent[descIndex];
}
return "";
}
function getMetaDesc(j){
console.log("j.mname: ",typeof j.mname);
var descIndex = null;
if (typeof j.mname !== 'undefined'){
j.mname.forEach((item,index)=>{
var name = item.toLowerCase();
if (name == 'description'){
descIndex = index;
}
});
if (descIndex){
return j.mcontent[descIndex];
}
}
if (typeof j.mTag !== 'undefined'){
j.mTag.forEach((item,index)=>{
var name = item.toLowerCase();
if (name.indexOf('og:description') > 0){
descIndex = index;
}
});
if (descIndex){
return j.mcontent[descIndex];
}
}
return "No Description Found...";
}
function rawUrlEncode(str) {
if (!str){
return str;
}
str = str.replace(/[\"\']/gm, '`');
str = str.replace(/[{}<>]/gm, '');
str = str.replace(/[\r\n]/gm, ' ');
str = addslashes(str);
console.log(str);
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
function addslashes( str ) {
// Escapes single quote, double quotes and backslash characters in a string with backslashes
// * example 1: addslashes("kevin's birthday");
// * returns 1: 'kevin\'s birthday'
return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}
function sGetTag(s,e,doc){
spos = doc.indexOf(s);
console.log('start: ',spos);
if (spos < 0){
return null;
}
spos = spos + s.length;
tag = right(doc,spos);
epos = tag.indexOf(e);
console.log('End: ',epos);
if (epos < 0){
console.log('End Tag Not Found: ',e);
fs.writeFileSync("erlog.txt", tag);
return null;
}
tag = left(tag,epos);
if (tag == '' || tag == ' '){
return null;
}
return tag;
}
function right(str, chr) {
return str.slice(chr,str.length);
}
function left(str, chr) {
return str.slice(0, chr);
}