|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.commoncrawl.whirlwind; |
| 19 | + |
| 20 | +import org.netpreserve.jwarc.WarcReader; |
| 21 | +import org.netpreserve.jwarc.cdx.CdxFormat; |
| 22 | +import org.netpreserve.jwarc.cdx.CdxWriter; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.OutputStreamWriter; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | + |
| 30 | +public class CdxjIndexer { |
| 31 | + |
| 32 | + public static void main(String[] args) throws IOException { |
| 33 | + |
| 34 | + if (args.length != 1) { |
| 35 | + System.err.println("Usage: java CdxjIndexer <input-warc-file>"); |
| 36 | + System.exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + Path requested = Path.of(args[0]).toAbsolutePath().normalize(); |
| 40 | + if (!Files.isRegularFile(requested)) { |
| 41 | + throw new SecurityException("Invalid WARC path"); |
| 42 | + } |
| 43 | + |
| 44 | + if (requested.toString().endsWith("gz") || requested.toString().endsWith("gzip")) { |
| 45 | + try { |
| 46 | + ValidateWARC.validateRandomAccessWarcOrFail(requested); |
| 47 | + } catch (IOException e) { |
| 48 | + System.out.println("This file is probably not a multi-member gzip but a single gzip file." + |
| 49 | + "\n" + |
| 50 | + "To allow seek, a gzipped WARC must have each record compressed into a single gzip member and concatenated together." + |
| 51 | + "\n" + |
| 52 | + "\n" + |
| 53 | + "This file is likely still valid and can be fixed by running:" + |
| 54 | + "\n" + |
| 55 | + "mvn -q exec:java -Dexec.mainClass=org.commoncrawl.whirlwind.RecompressWARC -Dexec.args=\"testing.warc testing.warc.gz\""); |
| 56 | + System.exit(-1); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + try ( |
| 61 | + InputStream in = Files.newInputStream(requested); |
| 62 | + CdxWriter cdxjWriter = new CdxWriter(new OutputStreamWriter(System.out)); |
| 63 | + WarcReader reader = new WarcReader(in) |
| 64 | + ) { |
| 65 | + cdxjWriter.setFormat(CdxFormat.CDXJ); |
| 66 | + cdxjWriter.process(reader, requested.toString()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments