-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedParser.kt
38 lines (31 loc) · 1.4 KB
/
FeedParser.kt
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
package com.github.jetbrains.rssreader.core.datasource.network
import com.github.jetbrains.rssreader.core.entity.Feed
import io.ktor.http.*
interface FeedParser {
suspend fun parse(sourceUrl: String, xml: String, isDefault: Boolean): Feed
companion object {
private val imgReg = Regex("<img[^>]+\\bsrc=[\"']([^\"']+)[\"']")
private val htmlTag = Regex("<.+?>")
private val blankLine = Regex("(?m)^[ \t]*\r?\n")
private fun findImageUrl(ownerLink: String, text: String): String? =
imgReg.find(text)?.value?.let { v ->
val i = v.indexOf("src=") + 5 //after src="
val url = v.substring(i, v.length - 1)
if (url.startsWith("http")) url else {
URLBuilder(ownerLink).apply {
encodedPath = url
}.buildString()
}
}
internal fun cleanText(text: String?): String? =
text?.replace(htmlTag, "")
?.replace(blankLine, "")
?.trim()
internal fun cleanTextCompact(text: String?) = cleanText(text)?.take(300)
internal fun pullPostImageUrl(postLink: String?, description: String?, content: String?): String? =
postLink?.let { l ->
description?.let { findImageUrl(l, it) }
?: content?.let { findImageUrl(l, it) }
}
}
}