-
Notifications
You must be signed in to change notification settings - Fork 0
/
psascrape.js
233 lines (191 loc) · 5.9 KB
/
psascrape.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
const rp = require('request-promise');
const cheerio = require('cheerio');
const fs = require('fs');
const leftpad = require('left-pad');
const mysql = require('mysql');
const connection = require('./psamacdb');
// let logStream = fs.createWriteStream("PSAlogALLpart28.txt", {'flags': 'a'});
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function maybe() {
var refreshID;
let go = 28500000;
let increment = 1000000;
let j = go - increment / 2;
let je = go + increment / 2;
//let rate = 2500;
let cut = 10;
let getsql = 'SELECT DISTINCT Field1 FROM psa.psa WHERE Field1 BETWEEN ' + j + ' AND ' + je + ';';
let getquery = connection.query(getsql, (err, result) => {
if (err) {
console.log(8888)
};
//console.log(result[0].Field1);
//console.log(result.length);
var finished = [];
for (let t = 0; t < result.length - 1; t++) {
finished.push(result[t].Field1);
}
//FILL ALL ARRAY TO LENGTH
var all = [];
for (var r = j; r < je; r++) {
all.push(r);
}
//console.log(all);
var a = [];
var missing = [];
//FILL MISSING
for (var x = 0; x < finished.length; x++) {
a[finished[x]] = true;
}
for (var x = 0; x < all.length; x++) {
if (a[all[x]]) {
delete a[all[x]];
} else {
a[all[x]] = true;
}
}
for (var k in a) {
missing.push(Number(k));
}
missing.push(9999999999);
//console.log(arr);
//console.log(arr.length);
//console.log(arr);
let n = 0;
let nend = missing.length - 1;
let nstart = Math.floor(nend / 2);
let nthis = Math.floor(nend / 2);
var grouping = [];
var temp = [];
missing.forEach(function(elem, index) {
if (elem > missing[index - 1] + 1) {
grouping.push(temp);
temp = [];
}
temp.push(elem);
})
//filter groups to cut
var groups = grouping.length;
//console.log(grouping);
grouping.sort(function(a, b) {
return a.length - b.length;
});
grouping = grouping.filter(function(element) {
return element.length > cut; /////////////////////////////////////////////////////////////////////////////////////////////////////////
});
grouping = shuffle(grouping);
missing = [].concat.apply([], grouping);
let rate = 1500;//getRandom(1500, 4000);
console.log('missing length: ' + missing.length + ', rate: ' + rate);
refreshID = setInterval(function() {
//cut down MISSING
// into groups
//console.log(missing);
let paddedID = leftpad(missing[nthis], 8, "0");
rp("https://www.psacard.com/cert/" + paddedID + "/PSA")
.then((html) => {
let $ = cheerio.load(html);
var record = [];
$('.cert-grid-value').each(function(i, element) {
let b = $(this);
if (b.text().startsWith('\n')) {
} else {
let a = $(this);
record.push(a.text());
}
});
if (record.length > 0 && typeof record !== 'undefined') {
console.log(paddedID);
//console.log('' + record[0] + ' ' + (missing.length - n) + '');
if (record[4].includes('TCG')) {
console.log(record);
}
let post = {
Field1: record[0],
Field2: record[1],
Field3: record[2],
Field4: record[3],
Field5: record[4],
Field6: record[5],
Field7: record[6],
Field8: record[7],
Field9: record[8]
}
let sql = 'INSERT INTO psa.psa SET ?';
let query = connection.query(sql, post, (err, result) => {
if (err) {
console.log(9999)
}
})
} else {
console.log(paddedID+'<----');
let post = {
Field1: missing[nthis],
Field2: '',
Field3: '',
Field4: '',
Field5: '',
Field6: '',
Field7: '',
Field8: '',
Field9: ''
}
let sql = 'INSERT INTO psa.psa SET ?';
let query = connection.query(sql, post, (err, result) => {
if (err) {
console.log(9999)
}
})
}
//.catch(console.error.bind(console));
if (nthis == nstart && (typeof record == 'undefined' || record.length == 0)) {
console.log('ON TO THE NEXT');
clearInterval(refreshID);
maybe();
} else if (nthis < nend && nthis >= nstart && record.length > 0 && typeof record !== 'undefined') {
nthis++;
} else if ((nthis == nend && record.length > 0 && typeof record !== 'undefined') || (nthis > nstart && (record.length == 0 || typeof record == 'undefined'))) {
nthis = nstart - 1;
} else if (nthis < nstart && nthis > n && record.length > 0 && typeof record !== 'undefined') {
nthis--;
} else if (nthis == n || (nthis < nstart && (record.length == 0 || typeof record == 'undefined'))) {
console.log('ON TO THE NEXT');
clearInterval(refreshID);
maybe();
}
if (missing.length == 0) {
console.log('NIGGA YOU DONE');
clearInterval(refreshID);
process.exit();
}
})
.catch(function (err) {
let delay = 20*60*1000;
console.log('err');
clearInterval(refreshID);
setTimeout(function(){
maybe();
}, delay);
});
}, rate) //INTERVAL 1000/SEC
});
};
maybe();