1
0
Fork 0

Add REST API for PlugSurfing parser

This commit is contained in:
John Ahlroos 2021-07-05 09:55:50 +03:00
parent 4b29325f32
commit 6ee822a218
Signed by: john
GPG Key ID: 258D0F70DB84CD5D
4 changed files with 29 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.NumberFormat;
@ -16,7 +17,7 @@ import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;
public class Parser {
public class PlugSurfingParser {
private static final Pattern DATE_TIME_KWH_DURATION_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}) \\((.*), (.*)kWh\\)");
private static final Pattern QUANTITY_PATTERN = Pattern.compile("(\\d+) (\\d+,\\d+) (\\d+)% (\\d+,\\d+) €");
@ -30,8 +31,12 @@ public class Parser {
public static void main(String[] args) {
var filename = args[0];
var file = Paths.get(filename);
System.out.println("Vuosi,Kuukausi,Latauksia,Perusmaksu(€),Total(€),Energia(kwh)");
parse(file).forEach((month, sessions) -> System.out.println(sessions.stream()
run(file, System.out);
}
public static void run(Path file, PrintStream result) {
result.println("Vuosi,Kuukausi,Latauksia,Perusmaksu(€),Total(€),Energia(kwh)");
parse(file).forEach((month, sessions) -> result.println(sessions.stream()
.reduce(Session::add)
.map(session -> String.format("%s,%s,%d,%.02f,%.02f,%.02f",
month.split("-")[0], month.split("-")[1],

View File

@ -0,0 +1,10 @@
<meta charset="UTF-8">
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="plugFile">PlugSurfing File:</label>
<input type="file" id="plugFile" name="plug.pdf" />
<input type="submit" value="Submit">
</form>
</body>
</html>

View File

@ -5,6 +5,7 @@ dependencies {
implementation project(':caruna-invoice')
implementation project(':helen-invoice')
implementation project(':composite-parsers')
implementation project(':plugsurfing-invoice')
implementation 'org.apache.logging.log4j:log4j-core:2.11.0'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.11.0'

View File

@ -3,6 +3,7 @@ package com.devsoap.parsers.api;
import com.devsoap.parsers.caruna.CarunaParser;
import com.devsoap.parsers.composites.CarunaHelenParser;
import com.devsoap.parsers.helen.HelenParser;
import com.devsoap.parsers.plugsurfing.PlugSurfingParser;
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.UploadedFile;
@ -73,6 +74,15 @@ public class Api {
});
});
path(PlugSurfingParser.class.getSimpleName(), () -> {
get(ctx -> ctx.render(renderParserStaticPage(PlugSurfingParser.class, "upload.html")));
post(ctx -> {
withUploadedFile(ctx.uploadedFiles().get(0), plugFile -> {
renderParserOutput(ctx, result -> PlugSurfingParser
.run(plugFile, result), "plugsurfing-report.csv");
});
});
});
});
});