Created
October 24, 2020 08:45
-
-
Save tiwiz/48d16d2b90d74d1c1ce57db67f47f174 to your computer and use it in GitHub Desktop.
Scraper for data about lights on Empire State Building
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
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")}" |
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
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