-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ksoup.parseMetaData function to parse website metadata (#85)
* Remove unnecessary suspend from streamParser * Add Ksoup.parseMetaData function to parse website metadata * Add Ksoup.parseMetaData function to parse website metadata
- Loading branch information
Showing
5 changed files
with
197 additions
and
1 deletion.
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
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,66 @@ | ||
package com.fleeksoft.ksoup.meta | ||
|
||
import com.fleeksoft.ksoup.Ksoup | ||
import com.fleeksoft.ksoup.model.MetaData | ||
import com.fleeksoft.ksoup.ported.openSourceReader | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class MetadataTest { | ||
val html = """ | ||
<html> | ||
<head> | ||
<title>Test Page</title> | ||
<meta property="og:title" content="Test OG Title"> | ||
<meta property="og:description" content="Test OG Description"> | ||
<meta property="og:image" content="https://example.com/image.png"> | ||
<meta property="og:url" content="https://example.com"> | ||
<meta name="twitter:title" content="Test Twitter Title"> | ||
<meta name="twitter:description" content="Test Twitter Description"> | ||
<meta name="twitter:image" content="https://example.com/twitter_image.png"> | ||
<meta name="description" content="Test Description"> | ||
<link rel="canonical" href="https://example.com"> | ||
<link rel="icon" href="/favicon.ico"> | ||
</head> | ||
</html> | ||
""".trimIndent() | ||
|
||
@Test | ||
fun testParseMetaDataFromString() { | ||
val metaData = Ksoup.parseMetaData(html, "https://example.com") | ||
|
||
assertMetaData(metaData) | ||
} | ||
|
||
@Test | ||
fun testParseMetaDataFromSourceReader() { | ||
val sourceReader = html.openSourceReader() | ||
val metaData = Ksoup.parseMetaData(sourceReader, "https://example.com") | ||
|
||
assertMetaData(metaData) | ||
} | ||
|
||
|
||
@Test | ||
fun testParseMetaDataFromElement() { | ||
val doc = Ksoup.parse(html, "https://example.com") | ||
val metaData = Ksoup.parseMetaData(doc) | ||
|
||
assertMetaData(metaData) | ||
} | ||
|
||
private fun assertMetaData(metaData: MetaData) { | ||
assertEquals("Test Page", metaData.htmlTitle) | ||
assertEquals("Test OG Title", metaData.ogTitle) | ||
assertEquals("Test OG Description", metaData.ogDescription) | ||
assertEquals("https://example.com/image.png", metaData.ogImage) | ||
assertEquals("https://example.com", metaData.ogUrl) | ||
assertEquals("Test Twitter Title", metaData.twitterTitle) | ||
assertEquals("Test Twitter Description", metaData.twitterDescription) | ||
assertEquals("https://example.com/twitter_image.png", metaData.twitterImage) | ||
assertEquals("Test Description", metaData.description) | ||
assertEquals("https://example.com", metaData.canonical) | ||
assertEquals("https://example.com/favicon.ico", metaData.favicon) | ||
} | ||
|
||
} |
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
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,21 @@ | ||
package com.fleeksoft.ksoup.model | ||
|
||
data class MetaData( | ||
val ogTitle: String? = null, | ||
val ogSiteName: String? = null, | ||
val ogDescription: String? = null, | ||
val ogImage: String? = null, | ||
val ogUrl: String? = null, | ||
val ogType: String? = null, | ||
val ogLocale: String? = null, | ||
val twitterCard: String? = null, | ||
val twitterTitle: String? = null, | ||
val twitterDescription: String? = null, | ||
val twitterImage: String? = null, | ||
val title: String? = null, | ||
val description: String? = null, | ||
val canonical: String? = null, | ||
val htmlTitle: String? = null, | ||
val author: String? = null, | ||
val favicon: String? = null | ||
) |