{"id":15041010,"url":"https://github.com/chimbori/crux","last_synced_at":"2025-10-20T07:39:30.593Z","repository":{"id":44137035,"uuid":"127672595","full_name":"chimbori/crux","owner":"chimbori","description":"Crux offers a flexible plugin-based API \u0026 implementation to extract interesting information from Web pages.","archived":false,"fork":false,"pushed_at":"2025-03-28T14:04:18.000Z","size":4792,"stargazers_count":239,"open_issues_count":12,"forks_count":43,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-04-08T15:10:02.617Z","etag":null,"topics":["kotlin","metadata","opengraph","readability","reader","schema-org","twitter-cards","unfurl"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chimbori.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"chimbori"}},"created_at":"2018-04-01T21:27:37.000Z","updated_at":"2025-03-28T14:04:22.000Z","dependencies_parsed_at":"2025-01-05T07:07:57.229Z","dependency_job_id":null,"html_url":"https://github.com/chimbori/crux","commit_stats":{"total_commits":703,"total_committers":25,"mean_commits":28.12,"dds":0.3172119487908962,"last_synced_commit":"5ff9fbfcdea1a41c7338f5878183e6b96a36d123"},"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chimbori%2Fcrux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chimbori%2Fcrux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chimbori%2Fcrux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chimbori%2Fcrux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chimbori","download_url":"https://codeload.github.com/chimbori/crux/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471061,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin","metadata","opengraph","readability","reader","schema-org","twitter-cards","unfurl"],"created_at":"2024-09-24T20:45:24.047Z","updated_at":"2025-10-20T07:39:25.560Z","avatar_url":"https://github.com/chimbori.png","language":"Kotlin","funding_links":["https://github.com/sponsors/chimbori"],"categories":[],"sub_categories":[],"readme":"# Crux\n\nCrux offers a flexible plugin-based API \u0026 implementation to extract metadata from Web pages.\nAs of v5.0, Crux no longer extracts article information from web page text; read on for recommended alternatives.\n\n## Usage\n\nCrux uses semantic versioning. If the API changes, then the major version will be incremented.\nUpgrading from one minor version to the next minor version within the same major version should\nnot require any client code to be modified.\n\nThe latest release is available via\n[Maven Central](https://search.maven.org/artifact/com.chimbori.crux/crux)\nor\n[GitHub Releases](https://github.com/chimbori/crux/releases).\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.chimbori.crux/crux/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.chimbori.crux/crux)\n\n### Get Crux via Maven\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.chimbori.crux\u003c/groupId\u003e\n  \u003cartifactId\u003ecrux\u003c/artifactId\u003e\n  \u003cversion\u003e0.0.0\u003c/version\u003e   \u003c!-- See the latest version number above. --\u003e\n\u003c/dependency\u003e\n```\n\n### Get Crux via Gradle\n\nProject/`build.gradle.kts`\n\n```kotlin\nallprojects {\n  repositories {\n    mavenCentral()\n  }\n}\n```\n\nModule/`build.gradle.kts`\n\n```kotlin\ndependencies {\n  implementation(\"com.chimbori.crux:crux:0.0.0\")  // See the latest version number above.\n}\n```\n\n## Sample Code\n\n```kotlin\n// Create a reusable object configured with the default set of plugins.\nval crux = Crux()\n\nval httpURL = \"https://chimbori.com/\".toHttpUrl()\n\n// You can provide prefetched raw HTML content yourself, or have Crux fetch\n// it for you.\nval htmlContent = \"\"\"\n  |\u003chtml\u003e\n  |  \u003chead\u003e\n  |    \u003ctitle\u003eChimbori\u003c/title\u003e\n  |    \u003cmeta name=\"twitter:image\" property=\"og:image\"\n  |        content=\"https://chimbori.com/media/cover-photo.png\"\u003e\n  |    \u003cmeta name=\"twitter:site\" content=\"ChimboriApps\"\u003e\n  |    \u003clink rel=\"apple-touch-icon-precomposed\" sizes=\"192x192\"\n  |        href=\"https://chimbori.com/media/favicon.png\"\u003e\n  |  \u003c/head\u003e\n  |\u003c/html\u003e\n  |\"\"\".trimMargin()\n\n// Crux runs inside a `suspend` function as a Kotlin Coroutine.\nval extractedMetadata = runBlocking {\n  crux.extractFrom(originalUrl = httpURL, parsedDoc = Jsoup.parse(htmlContent, httpURL.toString))\n}\n\n// Metadata fields such as the Title and Description are available from the\n// returned [Resource] object as an indexed collection.\nassertEquals(\"Chimbori\", extractedMetadata[TITLE])\n\n// Well-known URLs related to this page are available either as strings or\n// OkHttp [HttpUrl]s.\nassertEquals(\"https://chimbori.com/media/favicon.png\", extractedMetadata[FAVICON_URL])\nassertEquals(\"https://chimbori.com/media/favicon.png\".toHttpUrl(),\n    extractedMetadata.urls[FAVICON_URL])\n\n// Extra markup fields like Twitter Cards metadata or Open Graph metadata are\n// available as metadata fields as well.\nassertEquals(\"https://chimbori.com/media/cover-photo.png\", extractedMetadata[BANNER_IMAGE_URL])\n```\n\n## Design \u0026 Features\n\nCrux is designed as a chain of plugins; each one performs a small specific task.\n\nEach plugin receives as input a `Resource` object, which includes a URL and all the fields\npopulated by previous plugins in the chain. Each plugin can\n\n- extract new pieces of metadata and add them to the output, or\n- overwrite existing fields by setting a new value for the same key, or\n- remove existing fields by setting a `null` value for that key.\n\nA small set of well-known key names are defined in the API as `Fields`, but plugins and clients are\nnot restricted to this set. You can extend Crux for your own applications by defining and using\nyour own string keys for extracted metadata.\n\nPlugins can rewrite URLs, which are then passed on down the chain. This is how HTTP redirects\n(301 and 302) as well as static redirectors (such as those from Google and Facebook) are handled.\n\nEach plugin is independent of others. You can pick and choose the ones you want to use. If you use\nCrux in an Android app, Proguard or other minification tools can strip out the plugins you don’t\nuse.\n\nCrux’s API includes fewer setters/getters (compared to other such libraries), to keep the method\ncount low (this is important for Android). Its plugin-based architecture makes it cleaner \u0026\nleaner, compared to other libraries not explicitly optimized for Android.\n\n## Default Plugins\n\n### HtmlMetadataPlugin\n\nExtracts titles, banner images, \u0026 other metadata from any web page.\n\n- Support for more metadata formats: OpenGraph, Twitter Cards, Schema.org.\n\n### AmpPlugin\n\nRewrites the URL of an AMP page to its canonical (original) URL.\n\n### GoogleStaticRedirectorPlugin\n\nRewrites URLs generated by the Google Redirector Service to their canonical (original) URLs.\n\n### FacebookStaticRedirectorPlugin\n\nRewrites URLs generated by the Facebook Redirector Service to their canonical (original) URLs.\n\n## Optional Plugins\n\n### TrackingParameterRemover\n\nRemoves URL parameters typically used by analytics providers to track users’ behavior across the\nWeb. This plugin is optional because it may break some misconfigured URLs and cause them to\nreturn the wrong content.\n\n## Writing a Custom Plugin\n\n### ArticleExtractorPlugin\n\nAs of v5.0, Crux no longer contains its own article extraction plugin.\nWe recommend [dankito/Readability4J](https://github.com/dankito/Readability4J), a fork of Mozilla’s Readability.js,\nwhich is higher-quality and newer than Crux’s origin, Snacktory.\nWe recommend that you use it instead of relying on Crux’s parser (which has now been removed).\n\nReadability4J strips out sidebars, navigation bars, and other unimportant parts of a page, and extracts the core\narticle content.\n\nbuild.gradle.kts:\n```kotlinscript\ndependencies {\n  implementation(\"net.dankito.readability4j:readability4j:1.0.8\")\n}\n```\n\nReadability4JPlugin.kt:\n```kotlin\nimport com.chimbori.crux.api.Extractor\nimport com.chimbori.crux.api.Fields.DURATION_MS\nimport com.chimbori.crux.api.Fields.TITLE\nimport com.chimbori.crux.api.Resource\nimport com.chimbori.crux.common.estimatedReadingTimeMs\nimport com.chimbori.crux.common.isLikelyArticle\nimport net.dankito.readability4j.extended.Readability4JExtended\nimport okhttp3.HttpUrl\n\nclass Readability4JPlugin : Extractor {\n  override fun canExtract(url: HttpUrl) = url.isLikelyArticle()\n\n  override suspend fun extract(request: Resource): Resource? = if (request.url != null \u0026\u0026 request.document != null) {\n    val readability4J = Readability4JExtended(request.url.toString(), request.document!!)\n    val article = readability4J.parse()\n    Resource(\n      article = article.articleContent,\n      metadata = mapOf(\n        TITLE to article.title,\n        DURATION_MS to article.articleContent?.text()?.estimatedReadingTimeMs()\n      ),\n    )\n  } else {\n    null\n  }\n}\n```\n\nThen add `Readability4JPlugin` to the list of Crux plugins to use it along with Crux’s default plugins.\n\n### CustomerNumberExtractorPlugin\n\nAs an example, one can write a custom plugin to extract specific fields from a URL as follows:\n\n```kotlin\n// If you write a new plugin yourself, you can add any custom fields to the `Resource` object\n// yourself, and consume them in your own app.\nval customerNumberExtractorPlugin = object : Plugin {\n  // Indicate that your plugin can handle all URLs on your site, but no others.\n  override fun canHandle(url: HttpUrl): Boolean = url.topPrivateDomain() == \"your-website.com\"\n\n  // Fields in the returned [Resource] overwrite those in the input [request]. If no changes are\n  // to be made, then return null from your plugin. Otherwise, only return those fields that are\n  // new or changed from the input.\n  override suspend fun handle(request: Resource) = Resource(\n      fields = mapOf(CUSTOMER_NUMBER_FIELD to request.url?.queryParameter(\"customer-number\"))\n  )\n\n  val CUSTOMER_NUMBER_FIELD = \"customer-number\"\n}\n\nval cruxWithCustomPlugin = Crux(DEFAULT_PLUGINS + customerNumberExtractorPlugin)\nval orderDetailsUrl = \"https://www.your-website.com/orders?customer-number=42\".toHttpUrl()\n\nval metadata = runBlocking {\n  cruxWithCustomPlugin.extractFrom(orderDetailsUrl, Document(orderDetailsUrl.toString()))\n}\n\n// Input URL was unchanged and is available in the output metadata.\nassertEquals(orderDetailsUrl, metadata.url)\n// Data extracted by the custom plugin is available as a custom field.\nassertEquals(\"42\", metadata[customerNumberExtractorPlugin.CUSTOMER_NUMBER_FIELD])\n```\n\n## Image URL Extractor API\n\nFrom a single DOM Element root, the Image URL API inspects the sub-tree and returns the best\npossible image URL candidate available within it. It does this by scanning within the DOM tree\nfor interesting `src` \u0026 `style` tags.\n\nAll URLs are resolved as absolute URLs, even if the HTML contained relative URLs.\n\n```kotlin\nImageUrlExtractor(url, domElement).findImage().imageUrl\n```\n\n## Anchor Links Extractor API\n\nFrom a single DOM Element root, the Image URL API inspects the sub-tree and returns the best\npossible link URL candidate available within it. It does this by scanning within the DOM tree\nfor interesting `href` tags.\n\nAll URLs are resolved as absolute URLs, even if the HTML contained relative URLs.\n\n```kotlin\nLinkUrlExtractor(url, domElement).findLink().linkUrl\n```\n\n## URL Heuristics API\n\nThis API examines a given URL (without connecting to the server), and returns\nheuristically-determined answers to questions such as:\n\n- Is this URL likely a video URL?\n- Is this URL likely an image URL?\n- Is this URL likely an audio URL?\n- Is this URL likely an executable URL?\n- Is this URL likely an archive URL?\n\n```kotlin\nval url = \"https://example.com/article.html\".toHttpUrl()\n\nassertTrue(url.isLikelyArticle())\nassertFalse(url.isLikelyImage())\n```\n\n## License\n\n    Copyright 2016, Chimbori, makers of Hermit, the Lite Apps Browser.\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchimbori%2Fcrux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchimbori%2Fcrux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchimbori%2Fcrux/lists"}