generated from teamclairvoyant/scala3-application-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from teamclairvoyant/REST-145
[REST-145] Added provision to support HTML body format from API response
- Loading branch information
Showing
6 changed files
with
251 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
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
77 changes: 77 additions & 0 deletions
77
...a/com/clairvoyant/data/scalaxy/reader/text/instances/HTMLTableTextToDataFrameReader.scala
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,77 @@ | ||
package com.clairvoyant.data.scalaxy.reader.text.instances | ||
|
||
import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader | ||
import com.clairvoyant.data.scalaxy.reader.text.formats.{CSVTextFormat, HTMLTableTextFormat} | ||
import org.apache.spark.sql.types.StructType | ||
import org.apache.spark.sql.{DataFrame, SparkSession} | ||
import org.jsoup.Jsoup | ||
import org.jsoup.nodes.{Document, Element} | ||
import org.jsoup.select.Elements | ||
|
||
import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable` | ||
import scala.util.Try | ||
|
||
implicit object HTMLTableTextToDataFrameReader extends TextFormatToDataFrameReader[HTMLTableTextFormat] { | ||
|
||
val VALUE_SEPARATOR = "~" | ||
|
||
override def read( | ||
text: Seq[String], | ||
textFormat: HTMLTableTextFormat, | ||
originalSchema: Option[StructType], | ||
adaptSchemaColumns: StructType => StructType | ||
)(using sparkSession: SparkSession): DataFrame = | ||
TextToDataFrameReader.read( | ||
text = text.map(htmlText => convertHTMLTableToCSV(htmlText, textFormat.tableName)), | ||
textFormat = CSVTextFormat( | ||
sep = VALUE_SEPARATOR | ||
), | ||
originalSchema = originalSchema, | ||
adaptSchemaColumns = adaptSchemaColumns | ||
) | ||
|
||
def convertHTMLTableToCSV(htmlText: String, tableName: Option[String] = None): String = { | ||
Try { | ||
val parsedDocument = Jsoup.parse(htmlText) | ||
|
||
val table = getTableFromParsedDocument(parsedDocument, tableName) | ||
|
||
val rows = table.select("tr") | ||
val tableHeader = getTableHeader(rows) | ||
val tableRows = getTableRows(rows) | ||
|
||
tableHeader.concat(tableRows) | ||
}.recover { case ex: Exception => | ||
ex.printStackTrace() | ||
throw ex | ||
}.get | ||
} | ||
|
||
def getTableFromParsedDocument(parsedDocument: Document, tableName: Option[String]): Element = | ||
tableName | ||
.map { tblName => | ||
val tables = parsedDocument.select(tblName) | ||
if (tables.size() > 0) | ||
tables.first() | ||
else | ||
throw new Exception(s"HTML table: $tblName not found") | ||
} | ||
.getOrElse(parsedDocument.getElementsByTag("table").first()) | ||
|
||
def getTableHeader(rows: Elements): String = | ||
Seq(rows.select("th").map(_.text)).flatten | ||
.mkString(VALUE_SEPARATOR) | ||
.concat("\n") | ||
|
||
def getTableRows(rows: Elements): String = | ||
Seq( | ||
rows.map { row => | ||
val flattenRows = Seq(row.select("td").map(_.text())).flatten | ||
if (flattenRows.nonEmpty) | ||
flattenRows.mkString(VALUE_SEPARATOR).concat("\n") | ||
else | ||
"" | ||
} | ||
).flatten.mkString("") | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
...t/scala/com/clairvoyant/data/scalaxy/reader/text/HTMLTableTextToDataFrameReaderSpec.scala
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,121 @@ | ||
package com.clairvoyant.data.scalaxy.reader.text | ||
|
||
import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader | ||
import com.clairvoyant.data.scalaxy.reader.text.formats.{CSVTextFormat, HTMLTableTextFormat} | ||
import com.clairvoyant.data.scalaxy.reader.text.instances.HTMLTableTextToDataFrameReader | ||
import com.clairvoyant.data.scalaxy.test.util.SparkUtil | ||
import com.clairvoyant.data.scalaxy.test.util.matchers.DataFrameMatcher | ||
import com.clairvoyant.data.scalaxy.test.util.readers.DataFrameReader | ||
import org.apache.spark.sql.functions.col | ||
import org.apache.spark.sql.types.* | ||
|
||
class HTMLTableTextToDataFrameReaderSpec extends DataFrameReader with DataFrameMatcher { | ||
|
||
"read() - with html text" should "return a dataframe with correct count and schema" in { | ||
val htmlText = | ||
""" | ||
<!DOCTYPE html> | ||
<div class="ac-card-content ac-card-content-normal ac-card-content-normal-alt"> | ||
<p class="a-spacing-base ac-cms-promo-big"><img alt="" src="https://images-eu.ssl-images-amazon.com/images/G/02/associates/network/holborn/ads-promo._V285961827_.png"> | ||
Are you monetising international traffic? You can now receive earnings in an international bank account. <a href="https://amazon-affiliate.eu/en/receive-international-earnings/" target="_blank">Click here</a> to | ||
learn more. | ||
</p> | ||
<div class="a-dtt-datatable"> | ||
<table class="a-dtt-table"> | ||
<thead class="a-dtt-thead"> | ||
<tr> | ||
<th class="a-dtt-header"> | ||
Date | ||
</th> | ||
<th class="a-dtt-header"> | ||
Transaction | ||
</th> | ||
<th class="a-dtt-header"> | ||
<div class="ac-float-r"> | ||
Amount | ||
</div> | ||
</th> | ||
<th class="a-dtt-header"> | ||
<div class="ac-float-r"> | ||
Balance | ||
</div> | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody class="a-dtt-tbody"> | ||
<tr> | ||
<td> | ||
01/06/2022 | ||
</td> | ||
<td> | ||
04/2022 Advertising Fees | ||
</td> | ||
<td> | ||
<div class="ac-float-r"> | ||
£4,049.19 | ||
</div> | ||
</td> | ||
<td> | ||
<div class="ac-float-r"> | ||
£4,049.19 | ||
</div> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
30/05/2022 | ||
</td> | ||
<td> | ||
Payment by Direct Deposit | ||
</td> | ||
<td> | ||
<div class="ac-float-r"> | ||
<div class="ac-payment-balance-negative"> | ||
-£3,349.17 | ||
</div> | ||
</div> | ||
</td> | ||
<td> | ||
<div class="ac-float-r"> | ||
£0.00 | ||
</div> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
<div class="ac-payment-note ac-standard-alert"> | ||
<div class="a-box a-alert a-alert-info" aria-live="polite" aria-atomic="true"> | ||
<div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i> | ||
<div class="a-alert-content">Please note: | ||
Any transactions that occurred prior to May 10, 2000, will not be visible on this report. | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
""".stripMargin | ||
|
||
val htmlTableTextFormat = HTMLTableTextFormat() | ||
|
||
val actualDF = TextToDataFrameReader.read( | ||
text = htmlText, | ||
textFormat = htmlTableTextFormat | ||
) | ||
|
||
val expectedDF = readCSVFromText( | ||
text = | ||
""" | ||
|Amount~Balance~Date~Transaction | ||
|£4,049.19~£4,049.19~01/06/2022~04/2022 Advertising Fees | ||
|-£3,349.17~£0.00~30/05/2022~Payment by Direct Deposit | ||
""".stripMargin, | ||
csvOptions = Map( | ||
"sep" -> "~" | ||
) | ||
) | ||
|
||
actualDF should matchExpectedDataFrame(expectedDF) | ||
} | ||
|
||
} |