Skip to content

Commit

Permalink
Add Java sample code to README (#27)
Browse files Browse the repository at this point in the history
Hello, crux is such a lovely library! However, I'm using it from Java instead of Kotlin, which adds a little complexity. I figured out some code that works, and I thought it would be useful to everyone else if that example code was included in the README. Hopefully the sample makes sense!
  • Loading branch information
sigpwned committed Feb 11, 2023
1 parent be4fa02 commit f694a94
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Web pages.

## Sample Code

### Kotlin

```kotlin
// Create a reusable object configured with the default set of plugins.
val crux = Crux()
Expand Down Expand Up @@ -46,6 +48,65 @@ assertEquals("https://chimbori.com/media/favicon.png".toHttpUrl(),
assertEquals("https://chimbori.com/media/cover-photo.png", extractedMetadata[BANNER_IMAGE_URL])
```

### Java

```java
import com.chimbori.crux.Crux;
import com.chimbori.crux.api.Resource;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext;
import okhttp3.HttpUrl;
import java.util.concurrent.CompletableFuture;
// Other imports here...

// Create a reusable object configured with the default set of plugins.
var crux = Crux();

var httpURL = HttpUrl.get("https://chimbori.com/");

var htmlContent = """
<html>
<head>
<title>Chimbori</title>
<meta name="twitter:image" property="og:image"
content="https://chimbori.com/media/cover-photo.png">
<meta name="twitter:site" content="ChimboriApps">
<link rel="apple-touch-icon-precomposed" sizes="192x192"
href="https://chimbori.com/media/favicon.png">
</head>
</html>""";
// Scrape page text, if you please
final CompletableFuture<Resource> extractedMetadataFuture = new CompletableFuture<>();
crux.extractFrom(httpURL, Jsoup.parse(htmlContent, httpURL.toString()), new Continuation<Resource>() {
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(Object value) {
Resource extractedMetadata = (Resource) value;
extractedMetadataFuture.complete(extractedMetadata);
}
});
Resource extractedMetadata = extractedMetadataFuture.get();
// Metadata fields such as the Title and Description are available from the
// returned [Resource] object as an indexed collection.
assertEquals("Chimbori", extractedMetadata.get(Fields.TITLE).toString());
// Well-known URLs related to this page are available either as strings.
assertEquals("https://chimbori.com/media/favicon.png", extractedMetadata.get(Fields.FAVICON_URL).toString());

// Extra markup fields like Twitter Cards metadata or Open Graph metadata are
// available as metadata fields as well.
assertEquals("https://chimbori.com/media/cover-photo.png", extractedMetadata.get(Fields.BANNER_IMAGE_URL).toString())
```


# Default Plugins

## HtmlMetadataPlugin
Expand Down

0 comments on commit f694a94

Please sign in to comment.