Search in sources :

Example 1 with DumpDownloader

use of org.wikivoyage.listings.input.DumpDownloader in project wikivoyage-listings by baturin.

the class DumpDownloaderTest method isNotPartialDump.

/**
 * Test if the partial dump is detected correctly
 */
@Test
public void isNotPartialDump() throws Exception {
    String dumpStatus = "", partialDumpStatus = "";
    dumpStatus = IOUtils.toString(this.getClass().getResourceAsStream("/dumpstatus.json"), "UTF-8");
    partialDumpStatus = IOUtils.toString(this.getClass().getResourceAsStream("/partial_dumpstatus.json"), "UTF-8");
    DumpDownloader downloader = new DumpDownloader();
    Assert.assertFalse(downloader.isPartialDump(dumpStatus));
    Assert.assertTrue(downloader.isPartialDump(partialDumpStatus));
}
Also used : DumpDownloader(org.wikivoyage.listings.input.DumpDownloader) Test(org.junit.Test)

Example 2 with DumpDownloader

use of org.wikivoyage.listings.input.DumpDownloader in project wikivoyage-listings by baturin.

the class Main method main.

public static void main(String[] args) {
    HashMap<String, OutputFormat> formats = new HashMap<>();
    formats.put("csv", new CSV());
    formats.put("osmand-xml", new OsmXml(false));
    formats.put("osmand-xml-user-defined", new OsmXml(true));
    formats.put("xml", new Xml());
    formats.put("obf", new OBF(false, "tmp", "tmp/pois.xml"));
    formats.put("obf-user-defined", new OBF(true, "tmp", "tmp/pois.xml"));
    formats.put("sql", new SQL());
    formats.put("gpx", new GPX());
    formats.put("osmand.gpx", new OsmAndGPX());
    formats.put("kml", new KML());
    formats.put("validation-report", new ValidationReport());
    CommandLine cl = new CommandLine();
    String[] formatNames = formats.keySet().toArray(new String[formats.keySet().size()]);
    cl.parse(args, formatNames);
    fileNames = new FileNames(cl.listingsDir, cl.dumpsCacheDir, cl.workingDir);
    try {
        if (cl.help) {
            cl.printHelp((String[]) formats.keySet().toArray());
        } else if (cl.dailyUpdate) {
            dailyUpdate(cl, formats);
        } else {
            String inputFilename;
            // To embed in files to provide freshness information to users (example: "Generated from Wikivoyage 2016/07/20 data").
            String dumpDate = "";
            createWorkingDir();
            if (cl.inputFile != null) {
                inputFilename = cl.inputFile;
                log.info("Take POIs from '" + inputFilename + "'");
            } else {
                DumpDownloader downloader = new DumpDownloader();
                if (cl.inputUrl != null) {
                    inputFilename = fileNames.workingDirPath("dump.xml.bz2");
                    downloader.downloadDumpFromUrl(cl.inputUrl, inputFilename);
                } else {
                    createDumpsCacheDir();
                    List<String> dumpDates = downloader.listDumps(cl.inputLatest);
                    dumpDate = dumpDates.get(0);
                    inputFilename = fileNames.dumpCacheFilename(cl.inputLatest, dumpDate);
                    if (!FileUtils.fileExists(inputFilename)) {
                        String dumpUrl = downloader.dumpUrl(cl.inputLatest, dumpDate);
                        downloader.downloadDumpFromUrl(dumpUrl, inputFilename);
                    } else {
                        log.info("Use cached dump");
                    }
                }
            }
            if (cl.outputFormat != null) {
                OutputFormat format = formats.get(cl.outputFormat);
                generateFileForFormat(inputFilename, cl.outputFilename, format, dumpDate);
            }
            UnrecognizeTemplateCounter.getInstance().logUnrecognizeTemplatesSummary();
            log.info("Finished");
        }
    } catch (Exception e) {
        System.err.println("Failure");
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) CSV(org.wikivoyage.listings.output.CSV) OutputFormat(org.wikivoyage.listings.output.OutputFormat) KML(org.wikivoyage.listings.output.KML) GPX(org.wikivoyage.listings.output.GPX) OsmAndGPX(org.wikivoyage.listings.output.OsmAndGPX) FileUtilsException(org.wikivoyage.listings.utils.FileUtilsException) DumpReadException(org.wikivoyage.listings.input.DumpReadException) IOException(java.io.IOException) WriteOutputException(org.wikivoyage.listings.output.WriteOutputException) SQL(org.wikivoyage.listings.output.SQL) DumpDownloader(org.wikivoyage.listings.input.DumpDownloader) OBF(org.wikivoyage.listings.output.OBF) OsmAndGPX(org.wikivoyage.listings.output.OsmAndGPX) ValidationReport(org.wikivoyage.listings.output.ValidationReport) OsmXml(org.wikivoyage.listings.output.OsmXml) Xml(org.wikivoyage.listings.output.Xml) OsmXml(org.wikivoyage.listings.output.OsmXml) List(java.util.List)

Example 3 with DumpDownloader

use of org.wikivoyage.listings.input.DumpDownloader in project wikivoyage-listings by baturin.

the class Main method dailyUpdate.

private static void dailyUpdate(CommandLine cl, HashMap<String, OutputFormat> formats) throws IOException, FileUtilsException {
    createWorkingDir();
    createListingsDir();
    createDumpsCacheDir();
    DumpDownloader downloader = new DumpDownloader();
    for (String language : Languages.getLanguageCodes()) {
        log.info("Processing language " + language);
        List<String> dumpDates = downloader.listDumps(language);
        if (dumpDates.size() == 0) {
            continue;
        }
        String latestDumpDate = dumpDates.get(0);
        if (cl.latestCount != null) {
            log.info("Processing the latest " + cl.latestCount + " dumps");
            dumpDates = dumpDates.subList(0, cl.latestCount);
        }
        for (String dumpDate : dumpDates) {
            log.info("Processing dump " + dumpDate);
            try {
                processDump(downloader, language, latestDumpDate, dumpDate, formats, !cl.doNotUseIntermediateFile);
            } catch (Exception e) {
                log.info("Failed to create dump " + dumpDate);
                log.debug("Exception: ", e);
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                log.debug("Stack trace: " + sw.toString());
            }
        }
    }
    System.exit(0);
}
Also used : DumpDownloader(org.wikivoyage.listings.input.DumpDownloader) StringWriter(java.io.StringWriter) FileUtilsException(org.wikivoyage.listings.utils.FileUtilsException) DumpReadException(org.wikivoyage.listings.input.DumpReadException) IOException(java.io.IOException) WriteOutputException(org.wikivoyage.listings.output.WriteOutputException) PrintWriter(java.io.PrintWriter)

Aggregations

DumpDownloader (org.wikivoyage.listings.input.DumpDownloader)3 IOException (java.io.IOException)2 DumpReadException (org.wikivoyage.listings.input.DumpReadException)2 WriteOutputException (org.wikivoyage.listings.output.WriteOutputException)2 FileUtilsException (org.wikivoyage.listings.utils.FileUtilsException)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Test (org.junit.Test)1 CSV (org.wikivoyage.listings.output.CSV)1 GPX (org.wikivoyage.listings.output.GPX)1 KML (org.wikivoyage.listings.output.KML)1 OBF (org.wikivoyage.listings.output.OBF)1 OsmAndGPX (org.wikivoyage.listings.output.OsmAndGPX)1 OsmXml (org.wikivoyage.listings.output.OsmXml)1 OutputFormat (org.wikivoyage.listings.output.OutputFormat)1 SQL (org.wikivoyage.listings.output.SQL)1 ValidationReport (org.wikivoyage.listings.output.ValidationReport)1 Xml (org.wikivoyage.listings.output.Xml)1