Skip to content

Instantly share code, notes, and snippets.

@tiwiz
Created October 24, 2020 08:45
Show Gist options
  • Save tiwiz/48d16d2b90d74d1c1ce57db67f47f174 to your computer and use it in GitHub Desktop.
Save tiwiz/48d16d2b90d74d1c1ce57db67f47f174 to your computer and use it in GitHub Desktop.
Scraper for data about lights on Empire State Building
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
private const val ROOT = "https://www.esbnyc.com/"
data class DayLight(
val image: String,
val color: String,
val reason: String,
val date: String
)
fun Document.todayLights() : Pair<String, String> =
todayBackground(this) to todayColor(this)
private fun todayBackground(doc: Document) : String =
doc.select("div.background-image-wrapper")
.attr("style")
.split("url(")
.last()
.replace(")", "")
private fun todayColor(doc: Document) : String =
doc.select("div.is-today")
.select("div.info")
.select("h3")
.html()
fun Document.otherLights() : List<DayLight> =
select("div.view-content > div").map { it.extractDayData() }
private fun Element.extractDayData(): DayLight {
val date = date()
val (color, reason, picture) = with(colorElement()) {
Triple(color(), reason(), picture())
}
return DayLight(
image = picture,
color = color,
reason = reason,
date = date
)
}
private fun Element.date() : String = select("article.lse").attr("data-date")
private fun Element.colorElement() : Element = select("div.field_light").first()
private fun Element.color() : String = select("div > h3 > div").text()
private fun Element.reason() : String = select("div.clearfix.text-formatted.field_description > p").text()
private fun Element.picture() : String =
"$ROOT${select("div.media-image > img").attr("src")}"
val (picture, color) = Jsoup.connect("https://www.esbnyc.com/about/tower-lights")
.get()
.todayLights()
val days = Jsoup.connect("https://www.esbnyc.com/about/tower-lights/calendar").get()
.otherLights()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment