-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseTheMonstersKnowBlog.main.kts
53 lines (45 loc) · 1.44 KB
/
ParseTheMonstersKnowBlog.main.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@file:DependsOn("it.skrape:skrapeit:1.2.1")
import it.skrape.core.document
import it.skrape.fetcher.HttpFetcher
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape
import it.skrape.selects.ElementNotFoundException
import it.skrape.selects.html5.div
main()
fun main() {
var url: String? = "https://www.themonstersknow.com/dragon-tactics-part-5-deep-dragons-and-sea-serpents/"
val result = mutableMapOf<String, String>()
while (url?.isNotBlank() == true) {
var (name, prevUrl) = getNameAndLink(url) ?: break
name = name.removePrefix("Previous Post").trim()
println("Found page $name with url $prevUrl")
result[name] = prevUrl
url = prevUrl
}
val markdown = result.entries.asSequence()
.map { it.toPair() }
.sortedBy { it.first }
.map { (name, link) -> "[$name]($link)" }
.joinToString(separator = "\n")
println(markdown)
}
fun getNameAndLink(url: String): Pair<String, String>? {
try {
val result = skrape(HttpFetcher) {
request {
this.url = url
}
response {
document.div {
withClass = "nav-previous"
findFirst {
eachLink
}
}
}
}
return result.entries.firstOrNull()?.toPair()
} catch (e: ElementNotFoundException) {
return null
}
}