-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgscraper.js
105 lines (96 loc) · 3 KB
/
pgscraper.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
var XMLHttpRequest = require('xhr2');
var fs = require('fs');
var jsdom = require("jsdom");
var request = require('request');
var visitedLinks = {};
var collectedLinks = [];
var i = 19740;
function getLinks(startingNumber)
{
function crawlURL(URL,callback)
{
setTimeout(function() {
request(URL, function (err, response, body) {
if(err != null)
{
if(err.code == "ETIMEDOUT")
{
console.log(this.uri.href,"timed out. Retrying in 10 seconds...");
setTimeout(function() {crawlURL(URL,callback);}, 10000);
}
else
{
console.log("Unknown error:\n",err,"\nStopping at",startingNumber);
process.exit(1);
}
}
else if (!err && response.statusCode == 200)
{
callback(this.uri.href,body);
}
else if(response.statusCode > 500)
{
console.log(this.uri.href,"returned",response.statusCode,response.statusMessage,"Retrying in 10 seconds...");
setTimeout(function() {crawlURL(URL,callback);}, 10000);
}
else
{
console.log("Unknown error:\n",response.statusCode,response.statusMessage,"\nStopping at",startingNumber);
process.exit(1);
}
});
}, Math.random() * (350 - 275) + 275);
}
crawlURL("http://web.archive.org/web/1/http://portalgraphics.net/pg/profile/?user_id=" + startingNumber, function processData(url,body)
{
jsdom.env(body, function (err, window) {
if(body.includes("ERROR_CHEX") || body.includes("level4_db")) getLinks(++i);
else if(window.document.querySelector(".profile-table"))
{
var homeURL = findHomeURL(window.document.querySelector(".profile-table"),"URL");
if(!homeURL) homeURL = findHomeURL(window.document.querySelector(".profile-table"),"web サイト");
if(homeURL && homeURL.includes("nifty")) collectedLinks.push(homeURL);
console.log("Crawled",url);
if(i == 21159)
{
console.log("Crawl complete");
}
else getLinks(++i);
}
else if(body.includes("現在、回線が込み合っております。時間をおいて再度アクセスしてください"))
{
crawlURL("http://web.archive.org" + window.document.querySelector(".d > td:nth-child(3) > a:nth-child(1)").href,processData);
}
else
{
console.log(url,"is missing the profile table.\nStopping at",startingNumber);
process.exit(1);
}
});
});
function findHomeURL(table,phrase)
{
var possibleURL = Array.prototype.find.call(table.querySelectorAll("th"),th => th.textContent.includes(phrase));
if(possibleURL) return possibleURL.nextElementSibling.textContent;
else return null;
}
}
getLinks(i);
(function myLoop (l) {
getLinks(++i);
setTimeout(function () {
if (--l) myLoop(l);
}, Math.random() * (350 - 275) + 275)
})(4);
process.on ('exit', function (code) {
console.log(collectedLinks.length,"Nifty links found");
console.log("Writing to output");
var out = fs.writeFileSync("output.txt", collectedLinks.join("\r\n"));
console.log("Done");
process.exit (code);
})
process.on('SIGINT', function()
{
console.log("Crawling aborted");
process.exit(0);
})