Skip to content

Instantly share code, notes, and snippets.

@ChuuMong
Last active February 24, 2020 02:10
Show Gist options
  • Select an option

  • Save ChuuMong/9dba16d59fe30e4b9793a6ad3f127b56 to your computer and use it in GitHub Desktop.

Select an option

Save ChuuMong/9dba16d59fe30e4b9793a6ad3f127b56 to your computer and use it in GitHub Desktop.
import com.gargoylesoftware.htmlunit.WebClient
import com.gargoylesoftware.htmlunit.html.HtmlDivision
import com.gargoylesoftware.htmlunit.html.HtmlPage
import org.jsoup.Jsoup
import kotlin.system.measureTimeMillis
data class Currency(val code: String, val engName: String)
fun main() {
val currencies = mutableListOf<Currency>()
val time = measureTimeMillis {
val webClient = WebClient()
val htmlPage = webClient.getPage<HtmlPage>("https://widget-yahoo.ofx.com/")
webClient.waitForBackgroundJavaScript(5 * 60 * 1000)
val htmlDivisions = htmlPage.getByXPath<HtmlDivision>("//div[contains(@class, 'currency-option')]")
htmlDivisions.forEach { div ->
val spans = div.getElementsByTagName("span")
val currencyCode = spans.find { span -> span.getAttribute("class") == "currency-option-code" }?.textContent
val currencyEngName =
spans.find { span -> span.getAttribute("class") == "currency-option-description" }?.textContent
if (!currencyCode.isNullOrEmpty() && !currencyEngName.isNullOrEmpty()) {
currencies.add(Currency(currencyCode, currencyEngName))
}
}
println("total currency size : ${currencies.size}\n\n")
currencies.forEach {
if (it.code == "USD") {
return
}
val currencyMarketUrl = getCurrencyInfoUrl(it.code)
val doc = Jsoup.connect(currencyMarketUrl).get()
val elem = doc.select("div[data-reactid=\"33\"]").select("span[data-reactid=\"34\"]")
println("${it.code} - ${elem.text()}")
}
}
println(time)
}
fun getCurrencyInfoUrl(currency: String): String {
return "https://finance.yahoo.com/quote/USD$currency%3DX?p=USD$currency%3DX"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment