mirror of
https://github.com/jlengrand/OpenGraphKt.git
synced 2026-03-10 08:31:23 +00:00
Fixes #8
Starts fixing #8. Removes required dependency to JSoup for users of the library.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# OpenGraphKt
|
||||
|
||||

|
||||
|
||||
|
||||
[OpenGraphKt](https://github.com/jlengrand/OpenGraphKt) is a minimalist Kotlin library to work with the [Open Graph tags](https://ogp.me/) protocol.
|
||||
OpenGraphKt is a tiny wrapper on top of JSoup.
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
package fr.lengrand.opengraphkt
|
||||
|
||||
import java.io.File
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* Example demonstrating how to use the OpenGraphParser to extract Open Graph data from HTML.
|
||||
*/
|
||||
fun main() {
|
||||
val parser = OpenGraphParser()
|
||||
val fetcher = DocumentFetcher()
|
||||
|
||||
// Example 1: Parse Open Graph data from a URL
|
||||
println("Example 1: Parsing from URL")
|
||||
try {
|
||||
val document = fetcher.fromUrl("https://www.imdb.com/title/tt0068646/")
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(URI("https://www.imdb.com/title/tt0068646/").toURL())
|
||||
|
||||
println("Title: ${openGraphData.title}")
|
||||
println("Is valid: ${openGraphData.isValid()}")
|
||||
@@ -28,8 +27,7 @@ fun main() {
|
||||
val resourceFile = File(resourceUrl.toURI())
|
||||
|
||||
// Parse the file
|
||||
val document = fetcher.fromFile(resourceFile)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(resourceFile)
|
||||
|
||||
println("Title: ${openGraphData.title}")
|
||||
println("Is valid: ${openGraphData.isValid()}")
|
||||
@@ -59,8 +57,7 @@ fun main() {
|
||||
</html>
|
||||
""".trimIndent()
|
||||
|
||||
val document = fetcher.fromString(html)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(html)
|
||||
|
||||
println("Title: ${openGraphData.title}")
|
||||
println("Is valid: ${openGraphData.isValid()}")
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package fr.lengrand.opengraphkt
|
||||
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* DocumentFetcher's job is to take any type of input and transform it into a JSoup document for the Parser to then do its job
|
||||
*/
|
||||
class DocumentFetcher {
|
||||
|
||||
fun fromUrl(url: String): Document {
|
||||
return Jsoup.connect(url).get()
|
||||
}
|
||||
|
||||
fun fromString(html: String): Document {
|
||||
return Jsoup.parse(html)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses HTML from a file and returns a JSoup Document
|
||||
* @param file The file to parse
|
||||
* @param charsetName The charset to use for parsing (default is UTF-8)
|
||||
* @return A JSoup Document representing the parsed HTML
|
||||
*/
|
||||
fun fromFile(file: File, charsetName: String = "UTF-8") : Document {
|
||||
return Jsoup.parse(file, charsetName)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package fr.lengrand.opengraphkt
|
||||
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import org.jsoup.select.Elements
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
|
||||
data class OpenGraphTag(
|
||||
val property: String,
|
||||
@@ -149,6 +152,40 @@ class OpenGraphParser {
|
||||
return buildOpenGraphData(openGraphTags)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all Open Graph tags from a URL and returns a structured OpenGraphData object.
|
||||
*
|
||||
* @param url The URL to be parsed for Open Graph information.
|
||||
* @return An OpenGraphData object containing all extracted Open Graph data.
|
||||
*/
|
||||
fun parse(url: URL) : OpenGraphData {
|
||||
val doc = Jsoup.connect(url.toString()).get()
|
||||
return parse(doc)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all Open Graph tags from a raw HTML String and returns a structured OpenGraphData object.
|
||||
*
|
||||
* @param html The raw HTML String to be parsed for Open Graph information.
|
||||
* @return An OpenGraphData object containing all extracted Open Graph data.
|
||||
*/
|
||||
fun parse(html: String) : OpenGraphData {
|
||||
val doc = Jsoup.parse(html)
|
||||
return parse(doc)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all Open Graph tags from a raw HTML String and returns a structured OpenGraphData object.
|
||||
*
|
||||
* @param file The file to parse
|
||||
* @param charset The charset to use for parsing (default is UTF-8)
|
||||
* @return An OpenGraphData object containing all extracted Open Graph data.
|
||||
*/
|
||||
fun parse(file: File, charset: String = "UTF-8") : OpenGraphData {
|
||||
val doc = Jsoup.parse(file, charset)
|
||||
return parse(doc)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts Open Graph tags from JSoup Elements and converts them to OpenGraphTag objects.
|
||||
*
|
||||
|
||||
@@ -8,7 +8,6 @@ import kotlin.test.assertTrue
|
||||
class OpenGraphParserTest {
|
||||
|
||||
private val parser = OpenGraphParser()
|
||||
private val fetcher = DocumentFetcher()
|
||||
|
||||
// Sample HTML with all required OpenGraph tags and some structured properties
|
||||
private val completeHtml = """
|
||||
@@ -139,8 +138,7 @@ class OpenGraphParserTest {
|
||||
|
||||
@Test
|
||||
fun `test parse with complete OpenGraph tags`() {
|
||||
val document = fetcher.fromString(completeHtml)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(completeHtml)
|
||||
|
||||
// Verify that all required properties are extracted correctly
|
||||
assertEquals("The Rock", openGraphData.title)
|
||||
@@ -184,8 +182,7 @@ class OpenGraphParserTest {
|
||||
|
||||
@Test
|
||||
fun `test parse with article-specific tags`() {
|
||||
val document = fetcher.fromString(articleHtml)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(articleHtml)
|
||||
|
||||
// Verify basic properties
|
||||
assertEquals("Breaking News", openGraphData.title)
|
||||
@@ -208,8 +205,7 @@ class OpenGraphParserTest {
|
||||
|
||||
@Test
|
||||
fun `test parse with profile-specific tags`() {
|
||||
val document = fetcher.fromString(profileHtml)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(profileHtml)
|
||||
|
||||
// Verify basic properties
|
||||
assertEquals("John Doe", openGraphData.title)
|
||||
@@ -227,8 +223,7 @@ class OpenGraphParserTest {
|
||||
|
||||
@Test
|
||||
fun `test parse with book-specific tags`() {
|
||||
val document = fetcher.fromString(bookHtml)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(bookHtml)
|
||||
|
||||
// Verify basic properties
|
||||
assertEquals("The Great Novel", openGraphData.title)
|
||||
@@ -249,8 +244,7 @@ class OpenGraphParserTest {
|
||||
|
||||
@Test
|
||||
fun `test parse with multiple images`() {
|
||||
val document = fetcher.fromString(multipleImagesHtml)
|
||||
val openGraphData = parser.parse(document)
|
||||
val openGraphData = parser.parse(multipleImagesHtml)
|
||||
|
||||
// Verify basic properties
|
||||
assertEquals("Photo Gallery", openGraphData.title)
|
||||
|
||||
Reference in New Issue
Block a user