under construction

This commit is contained in:
2021-08-20 18:07:12 +09:00
parent c1d7e9a5ba
commit 2ee272598a
120 changed files with 20026 additions and 129 deletions

1
linkback/README.md Normal file
View File

@@ -0,0 +1 @@
# Linkback

42
linkback/build.gradle.kts Normal file
View File

@@ -0,0 +1,42 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-springboot")
id("org.springframework.boot") version "2.5.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
version = "1.0.0"
description = "Linkback"
tasks.bootJar{
enabled = false
}
tasks.jar {
enabled = true
}
dependencies {
// https://mvnrepository.com/artifact/org.jsoup/jsoup
implementation("org.jsoup:jsoup:1.14.2")
// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
// https://mvnrepository.com/artifact/com.github.spullara.mustache.java/compiler
//implementation("com.github.spullara.mustache.java:compiler:0.9.7")
implementation("org.springframework.boot:spring-boot-starter")
//implementation("org.springframework.boot:spring-boot-starter-mustache")
compileOnly("org.projectlombok:lombok")
//runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

View File

@@ -0,0 +1,19 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package com.elex_project.asgard.linkback;
import lombok.Data;
import java.net.URI;
@Data
public class Backlink {
private URI source, target;
private String title;
private String excerpt;
}

View File

@@ -0,0 +1,96 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package com.elex_project.asgard.linkback;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.Executors;
@Slf4j
public class Linkback {
private static final String UA = "Asgard linkback crawler by Elex (https://www.elex-project.com/)";
private static final HttpClient httpClient;
static {
httpClient = HttpClient.newBuilder()
.executor(Executors.newSingleThreadExecutor())
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
}
private final URI source, target;
public Linkback(final @NotNull URI source, final @NotNull URI target) {
this.source = source;
this.target = target;
}
public void process(final @NotNull LinkbackListener listener) {
if (source.equals(target)) {
listener.onError(new Exception(""));//todo
return;
}
httpClient.sendAsync(HttpRequest.newBuilder(source)
.GET()
.header("User-Agent", UA)
.timeout(Duration.ofSeconds(10))
.build(),
HttpResponse.BodyHandlers.ofString())
.exceptionally(ex -> {
listener.onError(ex);
return null;
})
.thenAccept(resp -> {
final Backlink result = new Backlink();
result.setSource(source);
result.setTarget(target);
try {
final Document document = Jsoup.parse(resp.body());
result.setTitle(document.title());
final Element descElement = document.selectFirst("meta[name='description']");
if (null == descElement) {
final String text = document.wholeText();
result.setExcerpt(text.substring(0, Math.min(text.length(), 200)));
} else {
result.setExcerpt(descElement.attr("value"));
}
final Elements links = document.select("a[href^='" + target.toString() + "']");
if (links.size() > 0) {
listener.onResult(result);
} else {
listener.onError(new Exception("No link found."));
}
// todo
} catch (Throwable e) {
listener.onError(e);
}
});
}
private void crawl(String document){
}
}

View File

@@ -0,0 +1,13 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package com.elex_project.asgard.linkback;
public interface LinkbackListener {
public void onResult(final Backlink backlink);
public void onError(final Throwable e);
}

View File

@@ -0,0 +1,8 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package com.elex_project.asgard.linkback;

View File

@@ -0,0 +1,28 @@
/*
* Project Asgard
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package com.elex_project.asgard.linkback;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class LinkbackTest {
@Test
void uriTest(){
URI a = URI.create("http://example.com/a").normalize();
URI b = URI.create("http://example.com/a/").normalize();
Path pa = Path.of(a.getPath());
Path pb = Path.of(b.getPath());
System.out.println(pa.equals(pb));
}
}