fix(rss): add fetch thumbnail case

This commit is contained in:
Ash 2024-08-09 16:39:18 +08:00
parent d6b2b1cac2
commit b46f70dec5
No known key found for this signature in database
2 changed files with 18 additions and 8 deletions

View File

@ -76,7 +76,7 @@ class RssHelper @Inject constructor(
try {
val accountId = context.currentAccountId
inputStream(okHttpClient, feed.url).use {
SyndFeedInput().apply { isPreserveWireFeed = true }
SyndFeedInput(true, Locale.getDefault()).apply { isPreserveWireFeed = true }
.build(XmlReader(it))
.entries
.asSequence()
@ -117,7 +117,7 @@ class RssHelper @Inject constructor(
date = (syndEntry.publishedDate ?: syndEntry.updatedDate)?.takeIf { !it.isFuture(preDate) } ?: preDate,
title = syndEntry.title.decodeHTML() ?: feed.name,
author = syndEntry.author,
rawDescription = (content ?: desc) ?: "",
rawDescription = content ?: desc ?: "",
shortDescription = Readability.parseToText(desc ?: content, syndEntry.link).take(110),
fullContent = content,
img = findThumbnail(syndEntry) ?: findThumbnail(content ?: desc),
@ -127,10 +127,13 @@ class RssHelper @Inject constructor(
}
fun findThumbnail(syndEntry: SyndEntry): String? {
if (syndEntry.enclosures.isNullOrEmpty()) {
return null
if (syndEntry.enclosures?.firstOrNull()?.url != null) {
return syndEntry.enclosures.first().url
}
return syndEntry.enclosures.first()?.url
if (syndEntry.foreignMarkup.firstOrNull()?.name == "thumbnail") {
return syndEntry.foreignMarkup.firstOrNull()?.attributes?.find { it.name == "url" }?.value
}
return null
}
fun findThumbnail(text: String?): String? {

View File

@ -74,10 +74,17 @@ class RssHelperTest {
@Test
fun testEnclosureNoFilenameExtension() {
val result = "https://pic.clubic.com/v1/images/1882727/raw"
val case = """
<enclosure url="$result" type="image/jpeg" length="0"/>
<enclosure url="$imageUrlString" type="image/jpeg" length="0"/>
"""
Assert.assertEquals(result, rssHelper.findThumbnail(case))
Assert.assertEquals(imageUrlString, rssHelper.findThumbnail(case))
}
@Test
fun testMediaNamespaceThumbnailInRSS20() {
val case = """
<enclosure url="$imageUrlString" type="image/jpeg" length="0"/>
"""
Assert.assertEquals(imageUrlString, rssHelper.findThumbnail(case))
}
}