Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rss): first find the enclosure tag as a thumbnail #681

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class FeverRssService @Inject constructor(
shortDescription = Readability.parseToText(it.html, it.url)
.take(110),
fullContent = it.html,
img = rssHelper.findImg(it.html ?: ""),
img = rssHelper.findThumbnail(it.html),
link = it.url ?: "",
feedId = accountId.spacerDollar(it.feed_id!!),
accountId = accountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class GoogleReaderRssService @Inject constructor(
shortDescription = Readability
.parseToText(it.summary?.content, findArticleURL(it)).take(110),
fullContent = it.summary?.content ?: "",
img = rssHelper.findImg(it.summary?.content ?: ""),
img = rssHelper.findThumbnail(it.summary?.content),
link = findArticleURL(it),
feedId = accountId.spacerDollar(
it.origin?.streamId?.ofFeedStreamIdToId()
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import java.io.InputStream
import java.util.*
import javax.inject.Inject

val enclosureRegex = """<enclosure\s+url="([^"]+)"\s+type=".*"\s*/>""".toRegex()
val imgRegex = """img.*?src=(["'])((?!data).*?)\1""".toRegex(RegexOption.DOT_MATCHES_ALL)

/**
* Some operations on RSS.
*/
Expand Down Expand Up @@ -117,19 +120,23 @@ class RssHelper @Inject constructor(
rawDescription = (content ?: desc) ?: "",
shortDescription = Readability.parseToText(desc ?: content, syndEntry.link).take(110),
fullContent = content,
img = findImg((content ?: desc) ?: ""),
img = findThumbnail(content ?: desc),
link = syndEntry.link ?: "",
updateAt = preDate,
)
}

fun findImg(rawDescription: String): String? {
// From: https://gitlab.com/spacecowboy/Feeder
fun findThumbnail(text: String?): String? {
text ?: return null
val enclosure = enclosureRegex.find(text)?.groupValues?.get(1)
if (enclosure?.isNotBlank() == true) {
return enclosure
}
// From https://gitlab.com/spacecowboy/Feeder
// Using negative lookahead to skip data: urls, being inline base64
// And capturing original quote to use as ending quote
val regex = """img.*?src=(["'])((?!data).*?)\1""".toRegex(RegexOption.DOT_MATCHES_ALL)
// Base64 encoded images can be quite large - and crash database cursors
return regex.find(rawDescription)?.groupValues?.get(2)?.takeIf { !it.startsWith("data:") }
return imgRegex.find(text)?.groupValues?.get(2)?.takeIf { !it.startsWith("data:") }
}

suspend fun queryRssIconLink(feedLink: String): String? {
Expand Down
Loading