forked from lukegosnellranken/EpsteinLibraryMediaScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.js
More file actions
194 lines (147 loc) · 5.18 KB
/
Copy pathscrape.js
File metadata and controls
194 lines (147 loc) · 5.18 KB
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
const fs = require("fs");
const { chromium } = require("playwright");
const QUERY = "No Images Produced";
const BASE_URL = "https://www.justice.gov/multimedia-search";
const OUTPUT = "pdf_list.txt";
// ------------------------------------------------------------
// RANGE SELECTION (CLI ARGUMENT)
// Usage:
// node scrape.js 0 → all
// node scrape.js 1 → first entry
// node scrape.js 5 → fifth entry
// node scrape.js 5-12 → entries 5 through 12
// ------------------------------------------------------------
const arg = process.argv[2] || "0";
let startIndex = 0;
let endIndex = Infinity;
if (arg.includes("-")) {
const [start, end] = arg.split("-").map(n => parseInt(n, 10));
// 1-based → 0-based
startIndex = Math.max(start - 1, 0);
endIndex = Math.max(end - 1, 0);
} else {
const n = parseInt(arg, 10);
if (n === 0) {
// 0 → all entries
startIndex = 0;
endIndex = Infinity;
} else {
// 1-based → 0-based
startIndex = Math.max(n - 1, 0);
endIndex = startIndex;
}
}
const startPage = Math.floor(startIndex / 10);
const endPage = isFinite(endIndex) ? Math.floor(endIndex / 10) : Infinity;
const startOffset = startIndex % 10;
const endOffset = endIndex % 10;
function renderTwoLineProgress(current, total, pageCount, totalCount, firstRender) {
const width = 40;
const ratio = Math.min(current / total, 1);
const filled = Math.round(ratio * width);
const bar = "█".repeat(filled) + "░".repeat(width - filled);
if (!firstRender) {
process.stdout.write("\x1b[2A");
}
process.stdout.write(`[${bar}] ${current}/${total}\n`);
process.stdout.write(`${pageCount} PDFs, total so far: ${totalCount}\n`);
}
function makePageUrl(page) {
return `${BASE_URL}?keys=${encodeURIComponent(QUERY)}&page=${page}`;
}
function buildTargetUrl(fileName) {
return `https://www.justice.gov/epstein/files/DataSet%2010/${encodeURIComponent(fileName)}`;
}
async function scrapeAll() {
console.log("Launching browser…");
const browser = await chromium.launch({ headless: false, slowMo: 80 });
const context = await browser.newContext();
const page = await context.newPage();
const startUrl = makePageUrl(startPage);
console.log(`Opening initial page: ${startUrl}`);
await page.goto(startUrl);
console.log("\nSolve Cloudflare and age-gate on THIS PAGE.");
console.log("Wait until you see the JSON payload.");
console.log("Then press Enter here.\n");
await new Promise(resolve => process.stdin.once("data", resolve));
process.stdin.setRawMode(false);
process.stdin.pause();
// Load existing entries so we append instead of overwrite
let existing = [];
if (fs.existsSync(OUTPUT)) {
existing = fs.readFileSync(OUTPUT, "utf8")
.split("\n")
.map(x => x.trim())
.filter(Boolean);
}
const pdfs = new Set(existing);
const existingCount = pdfs.size;
let pageNum = startPage;
let totalPages = null;
let firstRender = true;
console.log(""); // spacer line for clean progress bar area
while (true) {
const url = makePageUrl(pageNum);
// Fetch JSON inside browser context
const data = await page.evaluate(async (url) => {
const res = await fetch(url, { credentials: "include" });
if (!res.ok) return null;
return res.json();
}, url);
if (!data) break;
if (totalPages === null) {
const totalHits = data?.hits?.total?.value || 0;
// Total pages for the FULL dataset
const fullTotalPages = Math.ceil(totalHits / 10);
// Total pages we actually intend to scrape
const rangeTotalPages = isFinite(endPage)
? (endPage - startPage + 1)
: fullTotalPages;
// Use rangeTotalPages for progress bar
totalPages = rangeTotalPages;
}
const hits = data?.hits?.hits || [];
if (!hits.length) break;
const pageFiles = hits
.map(h => {
const key = h?._source?.key;
if (!key) return null;
const [datasetFolder, fileName] = key.split("/");
if (!datasetFolder || !fileName) return null;
const encodedDataset = encodeURIComponent(datasetFolder);
const encodedFile = encodeURIComponent(fileName);
return `https://www.justice.gov/epstein/files/${encodedDataset}/${encodedFile}`;
})
.filter(Boolean);
// Determine which entries on this page we should include
let sliceStart = 0;
let sliceEnd = pageFiles.length;
if (pageNum === startPage) {
sliceStart = startOffset;
}
if (pageNum === endPage && isFinite(endIndex)) {
sliceEnd = endOffset + 1;
}
const selectedFiles = pageFiles.slice(sliceStart, sliceEnd);
for (const url of selectedFiles) {
pdfs.add(url);
}
// Line 1: progress bar
const currentPageIndex = pageNum - startPage + 1;
renderTwoLineProgress(
currentPageIndex,
totalPages,
selectedFiles.length,
pdfs.size,
firstRender
);
firstRender = false;
if (pageNum >= endPage) break;
pageNum++;
}
fs.writeFileSync(OUTPUT, Array.from(pdfs).join("\n") + "\n");
const addedCount = pdfs.size - existingCount;
console.log(`Done. Added ${addedCount} new URLs. Total now: ${pdfs.size}.`);
await browser.close();
}
scrapeAll();