|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
/* © SRSoftware 2024 */ |
|
|
|
|
package de.srsoftware.cal; |
|
|
|
|
|
|
|
|
|
import static de.srsoftware.tools.Result.transform; |
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8; |
|
|
|
|
|
|
|
|
|
import de.srsoftware.cal.api.*; |
|
|
|
@ -44,8 +45,8 @@ public abstract class BaseImporter implements Importer {
@@ -44,8 +45,8 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
.map(tag -> tag.get("src")) |
|
|
|
|
.filter(Objects::nonNull) |
|
|
|
|
.map(Payload::of) |
|
|
|
|
.map(this::url) |
|
|
|
|
.map(this::toAttachment) |
|
|
|
|
.map(BaseImporter::url) |
|
|
|
|
.map(BaseImporter::toAttachment) |
|
|
|
|
.map(Result::optional) |
|
|
|
|
.flatMap(Optional::stream) |
|
|
|
|
.toList(); |
|
|
|
@ -140,9 +141,9 @@ public abstract class BaseImporter implements Importer {
@@ -140,9 +141,9 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
var text = anchor.inner(0).orElse(href); |
|
|
|
|
Payload //
|
|
|
|
|
.of(href) |
|
|
|
|
.map(this::url) |
|
|
|
|
.map(BaseImporter::url) |
|
|
|
|
.map(url -> link(url,text)) |
|
|
|
|
.optional() |
|
|
|
|
.map(url -> new Link(url, text)) |
|
|
|
|
.ifPresent(links::add); |
|
|
|
|
}); |
|
|
|
|
return links; |
|
|
|
@ -195,7 +196,7 @@ public abstract class BaseImporter implements Importer {
@@ -195,7 +196,7 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
.map(this::extractEventUrls) |
|
|
|
|
.stream(); |
|
|
|
|
return stream //
|
|
|
|
|
.map(this::url) |
|
|
|
|
.map(BaseImporter::url) |
|
|
|
|
.map(this::loadEvent) |
|
|
|
|
.peek(e -> { |
|
|
|
|
if (e instanceof Error<Appointment> err) System.err.println(err); |
|
|
|
@ -216,6 +217,12 @@ public abstract class BaseImporter implements Importer {
@@ -216,6 +217,12 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
return Error.format("Invalid parameter: %s", result.getClass().getSimpleName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected static Result<Link> link(Result<URL> url, String text) { |
|
|
|
|
var opt = url.optional(); |
|
|
|
|
if (opt.isEmpty()) return transform(url); |
|
|
|
|
return Payload.of(new Link(opt.get(),text)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected Result<Appointment> loadEvent(Result<URL> urlResult) { |
|
|
|
|
var link = urlResult //
|
|
|
|
|
.optional().map(url -> new Link(url, "Event-Seite")).orElse(null); |
|
|
|
@ -271,22 +278,19 @@ public abstract class BaseImporter implements Importer {
@@ -271,22 +278,19 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
|
|
|
|
|
protected abstract String programURL(); |
|
|
|
|
|
|
|
|
|
protected Result<Attachment> toAttachment(Result<URL> urlResult) { |
|
|
|
|
switch (urlResult) { |
|
|
|
|
case Payload<URL> payload: |
|
|
|
|
try { |
|
|
|
|
var mime = payload.get().openConnection().getContentType(); |
|
|
|
|
return Payload.of(new Attachment(payload.get(), mime)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return Error.format("Failed to read mime type of %s", payload); |
|
|
|
|
} |
|
|
|
|
case Error<URL> err: |
|
|
|
|
return err.transform(); |
|
|
|
|
default: |
|
|
|
|
return invalidParameter(urlResult); |
|
|
|
|
protected static Result<Attachment> toAttachment(Result<URL> urlResult) { |
|
|
|
|
var opt = urlResult.optional(); |
|
|
|
|
if (opt.isEmpty()) return transform(urlResult); |
|
|
|
|
try { |
|
|
|
|
var mime = opt.get().openConnection().getContentType(); |
|
|
|
|
return Payload.of(new Attachment(opt.get(), mime)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return Error.format("Failed to read mime type of %s", opt.get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static Result<Integer> toNumericMonth(String month) { |
|
|
|
|
month = month.toLowerCase(); |
|
|
|
|
if (month.startsWith("ja")) return Payload.of(1); |
|
|
|
@ -305,24 +309,13 @@ public abstract class BaseImporter implements Importer {
@@ -305,24 +309,13 @@ public abstract class BaseImporter implements Importer {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected <T> Result<T> transform(Result<?> result) { |
|
|
|
|
if (result instanceof Error<?> err) return err.transform(); |
|
|
|
|
return invalidParameter(result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected Result<URL> url(Result<String> urlResult) { |
|
|
|
|
switch (urlResult) { |
|
|
|
|
case Payload<String> payload: |
|
|
|
|
var url = payload.get(); |
|
|
|
|
try { |
|
|
|
|
return Payload.of(new URI(url).toURL()); |
|
|
|
|
} catch (MalformedURLException | URISyntaxException e) { |
|
|
|
|
return de.srsoftware.tools.Error.of("Failed to create URL of %s".formatted(url), e); |
|
|
|
|
} |
|
|
|
|
case de.srsoftware.tools.Error<String> err: |
|
|
|
|
return err.transform(); |
|
|
|
|
default: |
|
|
|
|
return invalidParameter(urlResult); |
|
|
|
|
protected static Result<URL> url(Result<String> urlResult) { |
|
|
|
|
if (urlResult.optional().isEmpty()) return transform(urlResult); |
|
|
|
|
var url = urlResult.optional().get(); |
|
|
|
|
try { |
|
|
|
|
return Payload.of(new URI(url).toURL()); |
|
|
|
|
} catch (MalformedURLException | URISyntaxException e) { |
|
|
|
|
return Error.of("Failed to create URL of %s".formatted(url), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|