@@ -10,40 +10,30 @@ import com.fleeksoft.ksoup.Ksoup
1010import dev.msfjarvis.claw.model.LobstersPostDetails
1111
1212private const val BASE_URL = " https://lobste.rs"
13+ private val commentCountRegex = " \\ d+" .toRegex()
14+ private const val STORY_SELECTOR = " ol.stories > li.story"
15+ private const val SUBMITTER_SELECTOR =
16+ " ol.stories > li.story div.byline > a[href^=/~]:not([tabindex]):not([aria-hidden=true])"
1317
1418internal fun parsePostDetails (html : String ): LobstersPostDetails {
1519 val document = Ksoup .parse(html, baseUri = BASE_URL )
20+ val storyElement = document.select(STORY_SELECTOR )
21+ val timestampElement = document.select(" $STORY_SELECTOR div.byline > time" )
22+ val titleElement = document.select(" $STORY_SELECTOR span.link.h-cite > a" )
23+ val commentsElement = document.select(" $STORY_SELECTOR span.comments_label a" )
24+ val submitterElement = document.select(SUBMITTER_SELECTOR )
25+ val tags = document.select(" $STORY_SELECTOR span.tags > a" ).map { it.text() }
1626 return LobstersPostDetails (
17- shortId = document.select(" ol.stories > li.story" ).attr(" data-shortid" ),
18- createdAt =
19- normalizeCreatedAt(
20- document.select(" ol.stories > li.story div.byline > time" ).attr(" data-at-unix" )
21- ),
22- title = document.select(" ol.stories > li.story span.link.h-cite > a" ).text(),
23- url = document.select(" ol.stories > li.story span.link.h-cite > a" ).attr(" abs:href" ),
27+ shortId = storyElement.attr(" data-shortid" ),
28+ createdAt = normalizeCreatedAt(timestampElement.attr(" data-at-unix" )),
29+ title = titleElement.text(),
30+ url = titleElement.attr(" abs:href" ),
2431 description = document.select(" div.story_content div.story_text" ).html(),
25- commentCount =
26- " \\ d+"
27- .toRegex()
28- .find(document.select(" ol.stories > li.story span.comments_label a" ).text())
29- ?.value
30- ?.toInt() ? : 0 ,
31- commentsUrl = document.select(" ol.stories > li.story span.comments_label a" ).attr(" abs:href" ),
32- submitter =
33- document
34- .select(
35- " ol.stories > li.story div.byline > a[href^=/~]:not([tabindex]):not([aria-hidden=true])"
36- )
37- .text(),
38- tags = document.select(" ol.stories > li.story span.tags > a" ).map { it.text() },
32+ commentCount = commentCountRegex.find(commentsElement.text())?.value?.toInt() ? : 0 ,
33+ commentsUrl = commentsElement.attr(" abs:href" ),
34+ submitter = submitterElement.text(),
35+ tags = tags,
3936 comments = parseComments(document),
40- userIsAuthor =
41- document
42- .select(
43- " ol.stories > li.story div.byline > a[href^=/~]:not([tabindex]):not([aria-hidden=true])"
44- )
45- .attr(" class" )
46- .split(' ' )
47- .contains(" user_is_author" ),
37+ userIsAuthor = submitterElement.attr(" class" ).split(' ' ).contains(" user_is_author" ),
4838 )
4939}
0 commit comments