-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWgetJava.java
More file actions
982 lines (827 loc) · 38.6 KB
/
Copy pathWgetJava.java
File metadata and controls
982 lines (827 loc) · 38.6 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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/* VERSION FINAL */
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import javax.net.ssl.*;
import java.security.cert.X509Certificate;
import java.util.zip.*;
public class WgetJava {
private static final String USER_AGENT = "WgetJava/1.0";
private static final int DEFAULT_PORT = 80;
private static final int HTTPS_PORT = 443;
private boolean recursiveMode = false;
private int threadPoolSize = 10;
private int maxTries = 3;
private int maxDepth = 10;
private String baseUrl;
private String baseHost;
private String basePath;
private Set<String> visitedUrls = ConcurrentHashMap.newKeySet();
private Set<String> pendingUrls = ConcurrentHashMap.newKeySet();
private ExecutorService threadPool;
private Semaphore downloadSemaphore;
private CountDownLatch downloadLatch;
private Map<String, Integer> urlTypes = new ConcurrentHashMap<>();
public static void main(String[] args) {
WgetJava wget = new WgetJava();
wget.showSyntax();
if (args.length == 0) {
System.out.println("Error: No se proporcionó URL");
return;
}
wget.parseArguments(args);
wget.startDownload();
}
private void showSyntax() {
System.out.println("========================================================================");
System.out.println("\t\t=== WGET JAVA ===");
System.out.println("Sintaxis: wget -r -t 10 --tries 3 --max-depth 10 http://unapagina.io/");
System.out.println(" donde:");
System.out.println(" -r indica uso en modo recursivo");
System.out.println(" -t <num> define el tamaño del pool de descarga");
System.out.println(" --tries <num> indica el número de intentos para descargar el recurso");
System.out.println(" --max-depth <num> profundidad máxima de recursión (default: 5)");
System.out.println("========================================================================");
}
private void parseArguments(String[] args) {
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-r":
recursiveMode = true;
break;
case "-t":
if (i + 1 < args.length) {
threadPoolSize = Integer.parseInt(args[++i]);
}
break;
case "--tries":
if (i + 1 < args.length) {
maxTries = Integer.parseInt(args[++i]);
}
break;
case "--max-depth":
if (i + 1 < args.length) {
maxDepth = Integer.parseInt(args[++i]);
}
break;
default:
if (args[i].startsWith("http")) {
baseUrl = args[i];
parseBaseUrl();
}
break;
}
}
if (baseUrl == null) {
throw new IllegalArgumentException("No se proporcionó URL válida");
}
}
private void parseBaseUrl() {
try {
URL url = new URL(baseUrl);
baseHost = url.getHost();
basePath = url.getPath();
if (basePath.isEmpty()) basePath = "/";
} catch (MalformedURLException e) {
throw new IllegalArgumentException("URL inválida: " + baseUrl);
}
}
private void startDownload() {
threadPool = Executors.newFixedThreadPool(threadPoolSize);
downloadSemaphore = new Semaphore(threadPoolSize);
System.out.println("Iniciando descarga de: " + baseUrl);
System.out.println("Modo recursivo: " + recursiveMode);
System.out.println("Pool de hilos: " + threadPoolSize);
System.out.println("Intentos maximos: " + maxTries);
System.out.println("Profundidad máxima: " + maxDepth);
System.out.println();
downloadLatch = new CountDownLatch(1);
downloadUrl(baseUrl, 0);
waitForAllDownloads();
threadPool.shutdown();
try {
threadPool.awaitTermination(30, TimeUnit.MINUTES);
} catch (InterruptedException e) {
System.err.println("Timeout esperando descargas");
}
printStats();
createNavigationIndex(); // Añadir esta línea
System.out.println("=====================================");
System.out.println("\n=== Descarga completada ===");
System.out.println("URLs descargadas: " + visitedUrls.size());
System.out.println("\n=====================================\n\n");
}
private void printStats() {
System.out.println("\nEstadísticas de descarga:");
System.out.println("URLs visitadas: " + visitedUrls.size());
System.out.println("URLs pendientes: " + pendingUrls.size());
System.out.println("\nTipos de URLs encontradas:");
for (Map.Entry<String, Integer> entry : urlTypes.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Imprimir algunas URLs de ejemplo por tipo
System.out.println("\nEjemplos de URLs por tipo:");
Map<String, List<String>> urlsByType = new HashMap<>();
for (String url : visitedUrls) {
String type = getUrlType(url);
urlsByType.computeIfAbsent(type, k -> new ArrayList<>()).add(url);
}
for (Map.Entry<String, List<String>> entry : urlsByType.entrySet()) {
System.out.println("\n" + entry.getKey() + ":");
int count = 0;
for (String url : entry.getValue()) {
if (count++ < 5) { // Mostrar solo los primeros 5 ejemplos
System.out.println(" - " + url);
}
}
if (entry.getValue().size() > 5) {
System.out.println(" ... y " + (entry.getValue().size() - 5) + " más");
}
}
}
private String getUrlType(String url) {
if (url.contains("/page/") || url.contains("?page=")) return "Página de navegación";
if (url.matches(".*\\.(css|js)$")) return "Recurso de estilo/script";
if (url.matches(".*\\.(jpg|jpeg|png|gif|ico|svg|webp)$")) return "Imagen";
if (url.matches(".*\\.(woff|woff2|ttf|eot)$")) return "Fuente";
return "Página de contenido";
}
private void waitForAllDownloads() {
while (true) {
try {
Thread.sleep(1000);
if (pendingUrls.isEmpty()) {
Thread.sleep(2000);
if (pendingUrls.isEmpty()) {
break;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
private void downloadUrl(String url, int depth) {
if (depth > maxDepth || visitedUrls.contains(url) || pendingUrls.contains(url)) {
return;
}
pendingUrls.add(url);
threadPool.submit(() -> {
try {
downloadSemaphore.acquire();
downloadFile(url, depth);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
pendingUrls.remove(url);
downloadSemaphore.release();
}
});
}
private void downloadFile(String urlString, int depth) {
if (visitedUrls.contains(urlString)) {
return;
}
visitedUrls.add(urlString);
String urlType = getUrlType(urlString);
urlTypes.merge(urlType, 1, Integer::sum);
for (int attempt = 1; attempt <= maxTries; attempt++) {
try {
System.out.println("[Hilo-" + Thread.currentThread().getId() + "] " +
"Descargando (intento " + attempt + "): " + urlString);
URL url = new URL(urlString);
String host = url.getHost();
int port = url.getPort();
boolean isHttps = url.getProtocol().equals("https");
if (port == -1) {
port = isHttps ? HTTPS_PORT : DEFAULT_PORT;
}
String path = url.getPath().isEmpty() ? "/" : url.getPath();
if (url.getQuery() != null) {
path += "?" + url.getQuery();
}
InetAddress address = InetAddress.getByName(host);
System.out.println("Resolviendo " + host + " -> " + address.getHostAddress());
Socket socket;
if (isHttps) {
socket = createSSLSocket(address, port);
} else {
socket = new Socket(address, port);
}
socket.setSoTimeout(30000);
OutputStream out = socket.getOutputStream();
sendHttpRequest(out, host, path);
InputStream in = socket.getInputStream();
HttpResponse response = readHttpResponseComplete(in);
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 308) {
String location = response.location;
if (location != null) {
System.out.println("↳ Redirigiendo a: " + location + " (código " + response.statusCode + ")");
socket.close();
String newUrl = resolveUrl(location, urlString);
if (newUrl != null && attempt == 1) {
visitedUrls.remove(urlString);
downloadFile(newUrl, depth);
return;
}
}
}
if (response.statusCode == 200) {
System.out.println("✓ Respuesta 200 OK para: " + urlString);
System.out.println(" Content-Length: " + response.contentLength);
System.out.println(" Content-Type: " + response.contentType);
System.out.println(" Transfer-Encoding: " + response.transferEncoding);
System.out.println(" Bytes recibidos: " + response.content.length);
// Manejar contenido comprimido
if ("gzip".equalsIgnoreCase(response.contentEncoding)) {
response.content = decompressGzip(response.content);
System.out.println(" Contenido descomprimido: " + response.content.length + " bytes");
}
String filename = saveFile(urlString, response.content, response.contentType);
System.out.println("✓ Archivo guardado: " + filename);
if (recursiveMode) {
List<String> links = new ArrayList<>();
if (isHtmlContent(response.contentType)) {
String content = new String(response.content,
getCharsetFromContentType(response.contentType));
links = extractLinks(content, urlString);
String modifiedContent = modifyLinks(content, urlString);
Files.write(Paths.get(filename),
modifiedContent.getBytes(getCharsetFromContentType(response.contentType)));
System.out.println(" Enlaces encontrados: " + links.size());
for (String link : links) {
if (shouldDownloadResource(link, urlString)) {
downloadUrl(link, depth + 1);
}
}
} else if (isCssContent(response.contentType)) {
String content = new String(response.content,
getCharsetFromContentType(response.contentType));
links = extractCssResources(content, urlString);
String modifiedContent = modifyCssLinks(content, urlString);
Files.write(Paths.get(filename),
modifiedContent.getBytes(getCharsetFromContentType(response.contentType)));
System.out.println(" Recursos CSS encontrados: " + links.size());
for (String link : links) {
if (shouldDownloadResource(link, urlString)) {
downloadUrl(link, depth + 1);
}
}
}
}
socket.close();
return;
} else {
System.err.println("✗ Error HTTP " + response.statusCode + " para: " + urlString);
}
socket.close();
} catch (Exception e) {
System.err.println("✗ Error descargando " + urlString +
" (intento " + attempt + "): " + e.getMessage());
if (attempt == maxTries) {
System.err.println("✗ Falló descarga después de " + maxTries +
" intentos: " + urlString);
}
}
}
}
private byte[] decompressGzip(byte[] compressed) throws IOException {
try (GZIPInputStream gzipInputStream =
new GZIPInputStream(new ByteArrayInputStream(compressed));
ByteArrayOutputStream resultStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[8192];
int length;
while ((length = gzipInputStream.read(buffer)) != -1) {
resultStream.write(buffer, 0, length);
}
return resultStream.toByteArray();
}
}
private Socket createSSLSocket(InetAddress address, int port) throws Exception {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory factory = sc.getSocketFactory();
return factory.createSocket(address, port);
}
private void sendHttpRequest(OutputStream out, String host, String path) throws IOException {
String request = "GET " + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: " + USER_AGENT + "\r\n" +
"Accept: */*\r\n" +
"Accept-Encoding: gzip\r\n" +
"Connection: close\r\n" +
"\r\n";
out.write(request.getBytes());
out.flush();
}
private HttpResponse readHttpResponseComplete(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] tempBuffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(tempBuffer)) != -1) {
buffer.write(tempBuffer, 0, bytesRead);
}
byte[] allData = buffer.toByteArray();
int headerEnd = findHeaderEnd(allData);
if (headerEnd == -1) {
throw new IOException("No se encontró el final de los headers HTTP");
}
byte[] headerBytes = Arrays.copyOfRange(allData, 0, headerEnd);
byte[] contentBytes = Arrays.copyOfRange(allData, headerEnd + 4, allData.length);
String headerString = new String(headerBytes, "UTF-8");
String[] lines = headerString.split("\r?\n");
HttpResponse response = new HttpResponse();
if (lines.length > 0) {
String statusLine = lines[0];
System.out.println("Status: " + statusLine);
String[] parts = statusLine.split(" ");
if (parts.length >= 2) {
response.statusCode = Integer.parseInt(parts[1]);
}
}
for (int i = 1; i < lines.length; i++) {
String line = lines[i].trim();
if (line.isEmpty()) continue;
String[] headerParts = line.split(":", 2);
if (headerParts.length == 2) {
String headerName = headerParts[0].trim().toLowerCase();
String headerValue = headerParts[1].trim();
switch (headerName) {
case "content-length":
try {
response.contentLength = Integer.parseInt(headerValue);
} catch (NumberFormatException e) {}
break;
case "content-type":
response.contentType = headerValue;
break;
case "content-encoding":
response.contentEncoding = headerValue;
break;
case "location":
response.location = headerValue;
break;
case "transfer-encoding":
response.transferEncoding = headerValue;
break;
}
}
}
if ("chunked".equalsIgnoreCase(response.transferEncoding)) {
response.content = decodeChunkedContent(contentBytes);
} else {
response.content = contentBytes;
}
return response;
}
private int findHeaderEnd(byte[] data) {
for (int i = 0; i < data.length - 3; i++) {
if (data[i] == '\r' && data[i + 1] == '\n' &&
data[i + 2] == '\r' && data[i + 3] == '\n') {
return i;
}
}
return -1;
}
private byte[] decodeChunkedContent(byte[] chunkedData) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
ByteArrayInputStream input = new ByteArrayInputStream(chunkedData);
try {
while (true) {
String sizeLine = readLine(input);
if (sizeLine == null || sizeLine.trim().isEmpty()) {
break;
}
int chunkSize;
try {
String sizeStr = sizeLine.split(";")[0].trim();
chunkSize = Integer.parseInt(sizeStr, 16);
} catch (NumberFormatException e) {
break;
}
if (chunkSize == 0) {
break;
}
byte[] chunkData = new byte[chunkSize];
int totalRead = 0;
while (totalRead < chunkSize) {
int bytesRead = input.read(chunkData, totalRead, chunkSize - totalRead);
if (bytesRead == -1) break;
totalRead += bytesRead;
}
result.write(chunkData, 0, totalRead);
readLine(input);
}
} catch (Exception e) {
System.err.println("Error decodificando chunked content: " + e.getMessage());
}
return result.toByteArray();
}
private String readLine(ByteArrayInputStream input) {
StringBuilder line = new StringBuilder();
int b;
while ((b = input.read()) != -1) {
if (b == '\r') {
input.mark(1);
if (input.read() == '\n') {
break;
} else {
input.reset();
line.append((char) b);
}
} else if (b == '\n') {
break;
} else {
line.append((char) b);
}
}
return line.length() == 0 && b == -1 ? null : line.toString();
}
private String getCharsetFromContentType(String contentType) {
if (contentType != null && contentType.contains("charset=")) {
String[] parts = contentType.split("charset=");
if (parts.length > 1) {
return parts[1].split(";")[0].trim();
}
}
return "UTF-8";
}
private String saveFile(String urlString, byte[] content, String contentType) throws IOException {
URL url = new URL(urlString);
String path = url.getPath();
String query = url.getQuery();
// Manejar la página principal y rutas sin extensión
if (path.isEmpty() || path.equals("/")) {
path = "/index.html";
} else if (!path.contains(".")) {
if (!path.endsWith("/")) {
path += "/";
}
path += "index.html";
}
// Manejar parámetros de consulta
if (query != null && !query.isEmpty()) {
String sanitizedQuery = query.replaceAll("[^a-zA-Z0-9]", "_");
int extIndex = path.lastIndexOf(".");
if (extIndex != -1) {
path = path.substring(0, extIndex) + "_" + sanitizedQuery +
path.substring(extIndex);
} else {
path = path + "_" + sanitizedQuery + ".html";
}
}
// Crear estructura de directorios
Path filePath = Paths.get("downloads" + path);
Files.createDirectories(filePath.getParent());
// Guardar el archivo
Files.write(filePath, content);
return filePath.toString();
}
private String getExtensionFromContentType(String contentType) {
if (contentType == null) return ".html";
contentType = contentType.toLowerCase();
Map<String, String> extensionMap = new HashMap<>();
extensionMap.put("text/html", ".html");
extensionMap.put("text/css", ".css");
extensionMap.put("application/javascript", ".js");
extensionMap.put("text/javascript", ".js");
extensionMap.put("image/jpeg", ".jpg");
extensionMap.put("image/png", ".png");
extensionMap.put("image/gif", ".gif");
extensionMap.put("image/svg+xml", ".svg");
extensionMap.put("image/webp", ".webp");
extensionMap.put("image/x-icon", ".ico");
extensionMap.put("image/vnd.microsoft.icon", ".ico");
extensionMap.put("font/woff", ".woff");
extensionMap.put("font/woff2", ".woff2");
extensionMap.put("font/ttf", ".ttf");
extensionMap.put("application/pdf", ".pdf");
for (Map.Entry<String, String> entry : extensionMap.entrySet()) {
if (contentType.contains(entry.getKey())) {
return entry.getValue();
}
}
return ".html";
}
private boolean isHtmlContent(String contentType) {
return contentType != null && contentType.toLowerCase().contains("text/html");
}
private boolean isCssContent(String contentType) {
return contentType != null &&
(contentType.toLowerCase().contains("text/css") ||
contentType.toLowerCase().contains("stylesheet"));
}
private List<String> extractLinks(String html, String baseUrl) {
List<String> links = new ArrayList<>();
Set<String> uniqueLinks = new HashSet<>();
Map<String, Pattern> patterns = new HashMap<>();
patterns.put("href", Pattern.compile("<(?:a|link)[^>]*?href=[\"']([^\"']+)[\"'](?:[^>]*?)>",
Pattern.CASE_INSENSITIVE));
patterns.put("src", Pattern.compile(
"<(?:img|script|source|iframe|embed|audio|video)[^>]*?src=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("srcset", Pattern.compile("<(?:img|source)[^>]*?srcset=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("pagination", Pattern.compile(
"<(?:a|link)[^>]*?(?:class=[\"'][^\"']*?(?:pagination|page)[^\"']*?[\"'])[^>]*?href=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("data", Pattern.compile("<object[^>]*?data=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("poster", Pattern.compile("<video[^>]*?poster=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("background", Pattern.compile("<[^>]*?background=[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("css-import", Pattern.compile("@import\\s*[\"']([^\"']+)[\"']",
Pattern.CASE_INSENSITIVE));
patterns.put("css-url", Pattern.compile("url\\([\"']?([^\"')\\s]+)[\"']?\\)",
Pattern.CASE_INSENSITIVE));
for (Map.Entry<String, Pattern> entry : patterns.entrySet()) {
Matcher matcher = entry.getValue().matcher(html);
while (matcher.find()) {
String link = matcher.group(1).trim();
if (entry.getKey().equals("srcset")) {
for (String srcsetUrl : link.split(",")) {
String url = srcsetUrl.trim().split("\\s+")[0];
processLink(url, baseUrl, uniqueLinks, links);
}
} else {
processLink(link, baseUrl, uniqueLinks, links);
}
}
}
return links;
}
private void processLink(String link, String baseUrl, Set<String> uniqueLinks, List<String> links) {
if (link.isEmpty()) return;
// Manejar URLs relativas numéricas (común en paginación)
if (link.matches("\\d+")) {
link = "./" + link;
}
String absoluteLink = resolveUrl(link, baseUrl);
if (absoluteLink != null && !uniqueLinks.contains(absoluteLink)) {
// Verificar si es una URL de paginación
if (isPaginationUrl(absoluteLink)) {
System.out.println(" Encontrado enlace de paginación: " + link + " -> " + absoluteLink);
}
uniqueLinks.add(absoluteLink);
links.add(absoluteLink);
System.out.println(" Encontrado enlace: " + link + " -> " + absoluteLink);
}
}
private boolean isPaginationUrl(String url) {
return url.matches(".*/(page|p)/\\d+/?.*") ||
url.matches(".*/\\?page=\\d+.*") ||
url.matches(".*/page/\\d+/?.*") ||
url.matches(".*\\?p=\\d+.*");
}
private List<String> extractCssResources(String css, String baseUrl) {
List<String> resources = new ArrayList<>();
Set<String> uniqueResources = new HashSet<>();
Pattern[] patterns = {
Pattern.compile("@import\\s+[\"']([^\"']+)[\"']", Pattern.CASE_INSENSITIVE),
Pattern.compile("@import\\s+url\\([\"']?([^\"')]+)[\"']?\\)", Pattern.CASE_INSENSITIVE),
Pattern.compile("url\\([\"']?([^\"')]+)[\"']?\\)", Pattern.CASE_INSENSITIVE)
};
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(css);
while (matcher.find()) {
String resource = matcher.group(1);
String absoluteResource = resolveUrl(resource, baseUrl);
if (absoluteResource != null && !uniqueResources.contains(absoluteResource)) {
uniqueResources.add(absoluteResource);
resources.add(absoluteResource);
}
}
}
return resources;
}
private String modifyLinks(String html, String baseUrl) {
try {
URL currentUrl = new URL(baseUrl);
String currentPath = currentUrl.getPath();
int depth = currentPath.split("/").length - 1;
Pattern[] patterns = {
Pattern.compile("(<a[^>]+href=[\"'])([^\"']+)([\"'][^>]*>)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(<img[^>]+src=[\"'])([^\"']+)([\"'][^>]*>)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(<link[^>]+href=[\"'])([^\"']+)([\"'][^>]*>)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(<script[^>]+src=[\"'])([^\"']+)([\"'][^>]*>)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(<source[^>]+src=[\"'])([^\"']+)([\"'][^>]*>)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(@import\\s+[\"'])([^\"']+)([\"'])", Pattern.CASE_INSENSITIVE),
Pattern.compile("(url\\([\"']?)([^\"')]+)([\"']?\\))", Pattern.CASE_INSENSITIVE)
};
String result = html;
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(result);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String prefix = matcher.group(1);
String link = matcher.group(2);
String suffix = matcher.group(3);
String localPath;
if (link.endsWith(".css")) {
// Ajustar rutas CSS según la profundidad
String cssPath = link.startsWith("/") ? link : "/" + link;
String relPath = "";
for (int i = 0; i < depth; i++) {
relPath += "../";
}
localPath = relPath + cssPath.substring(1);
} else {
localPath = convertToLocalPath(link, baseUrl);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(prefix + localPath + suffix));
}
matcher.appendTail(sb);
result = sb.toString();
}
return result;
} catch (MalformedURLException e) {
System.err.println("Error modificando enlaces: " + e.getMessage());
return html;
}
}
private String modifyCssLinks(String css, String baseUrl) {
Pattern[] patterns = {
Pattern.compile("(@import\\s+[\"'])([^\"']+)([\"'])", Pattern.CASE_INSENSITIVE),
Pattern.compile("(@import\\s+url\\([\"']?)([^\"')]+)([\"']?\\))", Pattern.CASE_INSENSITIVE),
Pattern.compile("(url\\([\"']?)([^\"')]+)([\"']?\\))", Pattern.CASE_INSENSITIVE)
};
String result = css;
try {
URL baseUrlObj = new URL(baseUrl);
String cssPath = baseUrlObj.getPath();
String cssDir = cssPath.substring(0, cssPath.lastIndexOf('/'));
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(result);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String prefix = matcher.group(1);
String link = matcher.group(2);
String suffix = matcher.group(3);
// Calcular la ruta relativa desde la ubicación del CSS
String absoluteLink;
if (link.startsWith("/")) {
// Ruta absoluta desde la raíz
absoluteLink = "." + link;
} else if (link.startsWith("http")) {
// URL absoluta, mantener si es del mismo dominio
URL linkUrl = new URL(link);
if (linkUrl.getHost().equals(baseUrlObj.getHost())) {
absoluteLink = "." + linkUrl.getPath();
} else {
absoluteLink = link; // Mantener URLs externas sin cambios
}
} else {
// Ruta relativa al CSS
absoluteLink = "../" + link;
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(prefix + absoluteLink + suffix));
}
matcher.appendTail(sb);
result = sb.toString();
}
} catch (MalformedURLException e) {
System.err.println("Error procesando CSS: " + e.getMessage());
}
return result;
}
private String resolveUrl(String link, String baseUrl) {
try {
if (link == null || link.isEmpty()) return null;
if (link.startsWith("#") || link.startsWith("mailto:") ||
link.startsWith("javascript:") || link.startsWith("data:")) {
return null;
}
URL base = new URL(baseUrl);
URL resolved = new URL(base, link);
if (!resolved.getHost().equals(base.getHost())) {
return null;
}
return resolved.toString();
} catch (MalformedURLException e) {
System.err.println("Error resolviendo URL: " + link + " - " + e.getMessage());
return null;
}
}
private String convertToLocalPath(String link, String baseUrl) {
try {
String absoluteLink = resolveUrl(link, baseUrl);
if (absoluteLink != null) {
URL url = new URL(absoluteLink);
String path = url.getPath();
String query = url.getQuery();
// Manejar la página principal
if (path.isEmpty() || path.equals("/")) {
path = "/index.html";
} else if (!path.contains(".")) {
if (!path.endsWith("/")) {
path += "/";
}
path += "index.html";
}
// Calcular la profundidad de la página actual
String currentPath = new URL(baseUrl).getPath();
int depth = currentPath.split("/").length - 1;
String prefix = "";
for (int i = 0; i < depth; i++) {
prefix += "../";
}
// Ajustar las rutas relativas según la profundidad
if (path.startsWith("/")) {
path = prefix + path.substring(1);
}
// Manejar los archivos CSS y sus recursos
if (path.endsWith(".css")) {
// Mantener la estructura de directorios para CSS
return prefix + path.substring(1);
}
// Manejar parámetros de consulta
if (query != null && !query.isEmpty()) {
String sanitizedQuery = query.replaceAll("[^a-zA-Z0-9]", "_");
int extIndex = path.lastIndexOf(".");
if (extIndex != -1) {
path = path.substring(0, extIndex) + "_" + sanitizedQuery + path.substring(extIndex);
} else {
path = path + "_" + sanitizedQuery + ".html";
}
}
return path;
}
} catch (MalformedURLException e) {
System.err.println("Error convirtiendo ruta: " + link + " - " + e.getMessage());
}
return link;
}
private boolean shouldDownloadResource(String url, String currentUrl) {
try {
if (url == null || url.trim().isEmpty()) return false;
String absoluteUrl = resolveUrl(url, currentUrl);
if (absoluteUrl == null) return false;
URL urlObj = new URL(absoluteUrl);
if (!urlObj.getHost().equals(baseHost)) {
return false;
}
if (url.startsWith("#") || url.startsWith("mailto:") ||
url.startsWith("javascript:") || url.startsWith("data:")) {
return false;
}
// Permitir URLs de paginación incluso si ya han sido visitadas
if (isPaginationUrl(absoluteUrl)) {
return !pendingUrls.contains(absoluteUrl);
}
if (visitedUrls.contains(absoluteUrl) || pendingUrls.contains(absoluteUrl)) {
return false;
}
return true;
} catch (Exception e) {
System.err.println("Error evaluando URL: " + url + " - " + e.getMessage());
return false;
}
}
private void createNavigationIndex() {
try {
StringBuilder index = new StringBuilder();
index.append("<!DOCTYPE html>\n");
index.append("<html><head><title>Indice de navegacion</title></head>\n");
index.append("<body>\n");
index.append("<h1>Indice de paginas descargadas</h1>\n");
index.append("<ul>\n");
Path downloadsPath = Paths.get("downloads");
Files.walk(downloadsPath)
.filter(Files::isRegularFile)
.sorted()
.forEach(path -> {
String relativePath = downloadsPath.relativize(path).toString();
index.append(String.format("<li><a href=\"%s\">%s</a></li>\n",
relativePath, relativePath));
});
index.append("</ul></body></html>");
// Guardar el índice
Files.write(Paths.get("downloads/site_index.html"),
index.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
System.err.println("Error creando índice de navegación: " + e.getMessage());
}
}
static class HttpResponse {
int statusCode;
int contentLength = -1;
String contentType;
String contentEncoding;
String location;
String transferEncoding;
byte[] content;
}
}