-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split document fetching responsibilities out of `HtmlMetadataExtracto…
…r` into a new class, `DocumentFetcher`
- Loading branch information
Showing
4 changed files
with
67 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/chimbori/crux/plugins/DocumentFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.chimbori.crux.plugins | ||
|
||
import com.chimbori.crux.api.Extractor | ||
import com.chimbori.crux.api.Fields.CANONICAL_URL | ||
import com.chimbori.crux.api.Resource | ||
import com.chimbori.crux.common.fetchFromUrl | ||
import com.chimbori.crux.common.isLikelyArticle | ||
import com.chimbori.crux.extractors.extractCanonicalUrl | ||
import okhttp3.HttpUrl | ||
import okhttp3.OkHttpClient | ||
|
||
/** | ||
* Fetches an HTML document from a remote URL, if not already fetched. | ||
* If a parsed JSoup Document is already available, this is a no-op. | ||
*/ | ||
public class DocumentFetcher(private val okHttpClient: OkHttpClient) : Extractor { | ||
/** Skip handling any file extensions that are unlikely to be HTML pages. */ | ||
public override fun canExtract(url: HttpUrl): Boolean = url.isLikelyArticle() | ||
|
||
override suspend fun extract(request: Resource): Resource { | ||
val resourceToUse = if (request.document != null) { | ||
request | ||
} else if (request.url != null) { | ||
Resource.fetchFromUrl(request.url, okHttpClient) | ||
} else { | ||
Resource() | ||
} | ||
|
||
val canonicalUrl = resourceToUse.document?.extractCanonicalUrl() | ||
?.let { resourceToUse.url?.resolve(it) } | ||
?: resourceToUse.url | ||
|
||
return Resource( | ||
url = canonicalUrl, | ||
document = resourceToUse.document, | ||
metadata = mapOf(CANONICAL_URL to canonicalUrl) | ||
).removeNullValues() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters