Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lkwg82 committed Oct 5, 2024
1 parent e7975fa commit 8660e15
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 91 deletions.
15 changes: 0 additions & 15 deletions src/main/java/de/lgohlke/homebanking/AccountStatusCSVWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,17 @@
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
public class AccountStatusCSVWriter {
public static final String SUMMARY_CSV = "summary.csv";
private final Path dataDirectory;

public void writeStatusesToCSV(List<AccountStatus> statuses) {
statuses.forEach(this::writeSingleStatusToCSV);
}

@Deprecated(forRemoval = true)
public void writeSummaryToCSV(Collection<AccountStatus> statuses) {
AccountStatusCSVConverter converter = new AccountStatusCSVConverter();
List<String> lines = converter.convert(statuses);
var statusLines = String.join("\n", lines);
try {
log.info("writing {}", SUMMARY_CSV);
Files.writeString(dataDirectory.resolve(SUMMARY_CSV), statusLines);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void writeSingleStatusToCSV(AccountStatus status) {
String iban = status.iban()
.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package de.lgohlke.homebanking;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Slf4j
class AccountStatusSummaryWriter {
private static final String SUMMARY_CSV = "summary.csv";
private final Path baseDir;

@SneakyThrows
public void writeSummaryToCSV() {
Collection<AccountStatus> statuses = collectStatuses();
AccountStatusCSVConverter converter = new AccountStatusCSVConverter();
List<String> lines = converter.convert(statuses);
var statusLines = String.join("\n", lines);
try {
log.info("writing {}", SUMMARY_CSV);
Files.writeString(baseDir.resolve(SUMMARY_CSV), statusLines);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private List<AccountStatus> collectStatuses() throws IOException {
List<AccountStatus> accountStatuses = new ArrayList<>();
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (attrs.isRegularFile() && file.toString().endsWith(".csv")) {
List<String> lines = Files.readAllLines(file);
var status = new AccountStatusCSVConverter().convert(lines);
accountStatuses.add(status);
}
return super.visitFile(file, attrs);
}
};
Files.walkFileTree(baseDir, visitor);

return keepOneStatusPerDateAndIBAN(accountStatuses);
}

private static List<AccountStatus> keepOneStatusPerDateAndIBAN(List<AccountStatus> accountStatuses) {
return accountStatuses
.stream()
.map(status -> {
String key = status.date() + "_" + status.iban();
return Map.entry(key,
status);
})
.collect(Collectors.groupingBy(Map.Entry::getKey))
.values()
.stream()
.map(List::getFirst)
.map(Map.Entry::getValue)
.toList();
}
}
44 changes: 0 additions & 44 deletions src/main/java/de/lgohlke/homebanking/DataFromBankRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,8 @@
import com.microsoft.playwright.Browser;
import org.apache.commons.lang3.NotImplementedException;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface DataFromBankRetriever {
default void fetchData(Browser browser) {
throw new NotImplementedException();
}

Path dataDirectory();

default Set<AccountStatus> collect() {
FileFilter dirFilter = File::isDirectory;
FileFilter csvFileFilter = pathname -> pathname.isFile() && pathname.getPath().endsWith(".csv");
File[] dirs = dataDirectory().toFile().listFiles(dirFilter);
return Stream.of(Objects.requireNonNull(dirs))
.map(dir -> dir.listFiles(csvFileFilter))
.filter(Objects::nonNull)
.flatMap(Arrays::stream)
.map(file -> {
try {
List<String> lines = Files.readAllLines(file.toPath());
if (lines.size() == 2) {
return lines.get(1);
}
throw new IllegalStateException("should have two lines");
} catch (IOException e) {
throw new RuntimeException(e);
}
}).map(line -> {
String[] parts = line.split("\\|");
if (parts.length == 4) {
return AccountStatus.parse(Date.valueOf(parts[0]), parts[1], parts[2], parts[3]);
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
}
16 changes: 5 additions & 11 deletions src/main/java/de/lgohlke/homebanking/MainDataRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import de.lgohlke.homebanking.institutes.dkb.DKBDataRetriever;
import de.lgohlke.homebanking.institutes.quirion.QuirionDataRetriever;
import de.lgohlke.homebanking.institutes.scalablecapital.ScalableCapitalDataRetriever;
import de.lgohlke.homebanking.institutes.traderepublic.TradeRepublicDataRetriever;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;

public class MainDataRetriever {
public static void main(String[] args) {
Expand All @@ -26,19 +24,15 @@ void execute(Path dataDir) {
List<DataFromBankRetriever> retrievers = List.of(
new DKBDataRetriever(dataDir.resolve("dkb")),
new QuirionDataRetriever(dataDir.resolve("quirion")),
new TradeRepublicDataRetriever(dataDir.resolve("traderepublic")),
new ScalableCapitalDataRetriever(dataDir.resolve("scalablecapital"))
// new TradeRepublicDataRetriever(dataDir.resolve("traderepublic"))
);
// end::list_of_institutes[]
try (Browser browser = BrowserLauncher.createChromium()) {
List<AccountStatus> statuses = retrievers.stream()
.map(retriever -> {
retriever.fetchData(browser);
return retriever.collect();
})
.flatMap(Set::stream)
.toList();
new AccountStatusCSVWriter(dataDir).writeSummaryToCSV(statuses);
retrievers.forEach(retriever -> {
retriever.fetchData(browser);
});
new AccountStatusSummaryWriter(dataDir).writeSummaryToCSV();
System.out.println(dataDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void login() {
for (int i = 0; i < 4; i++) {
String pin_at_pos = password.substring(i, i + 1);
locator2.pressSequentially(pin_at_pos);
Thread.sleep(300);
}
Thread.sleep(1_000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,4 @@ void should_write_account_statuses(@TempDir Path tempdir) throws IOException {
2024-08-27|DE75 5001 0517 2221 8623 18|2123,54|Giro1
""");
}

@Deprecated
@Test
void should_write_merged_summary_report(@TempDir Path tempdir) throws IOException {
Date date = Date.valueOf(LocalDate.of(2024, 8, 27));
List<AccountStatus> statuses = List.of(
AccountStatus.parse(date, "DE75 5001 0517 2221 8623 18", "2123,54 €", "Giro1"),
AccountStatus.parse(date, "DE88 5001 0517 2235 7114 53", "5123,54 €", "Giro2")
);

AccountStatusCSVWriter accountStatusCSVWriter = new AccountStatusCSVWriter(tempdir);
accountStatusCSVWriter.writeStatusesToCSV(statuses);

accountStatusCSVWriter.writeSummaryToCSV(statuses); // test

Path summary = tempdir.resolve("summary.csv");
assertThat(summary).isNotEmptyFile();

List<String> lines = Files.readAllLines(summary);
assertThat(lines).hasSize(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.lgohlke.homebanking;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class AccountStatusSummaryWriterTest {
@Test
void name(@TempDir Path tempdir) throws IOException {
List<AccountStatus> statuses = List.of(
AccountStatus.parse("2024-08-27", "DE75 5001 0517 2221 8623 18", "2123,54 €", "Giro1"),
AccountStatus.parse("2024-08-28", "DE75 5001 0517 2221 8623 18", "2123,54 €", "Giro1"),
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "12123,55 €", "Giro1"),
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "12123,55 €", "Giro1"),
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "2123,55 €", "Giro1"),
AccountStatus.parse("2024-08-27", "DE88 5001 0517 2235 7114 53", "5153,54 €", "Giro2")
);

AccountStatusCSVWriter accountStatusCSVWriter = new AccountStatusCSVWriter(tempdir);
accountStatusCSVWriter.writeStatusesToCSV(statuses);

AccountStatusSummaryWriter summaryWriter = new AccountStatusSummaryWriter(tempdir);
summaryWriter.writeSummaryToCSV();

Path summary = tempdir.resolve("summary.csv");
assertThat(summary).isNotEmptyFile();

List<String> lines = Files.readAllLines(summary);
assertThat(lines).hasSize(4);
}

}

0 comments on commit 8660e15

Please sign in to comment.