Skip to content

Commit

Permalink
feat(feed): parse favicon for feed's site (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashinch authored Sep 23, 2023
1 parent 6c95213 commit f5c6e5f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import me.ash.reader.domain.repository.AccountDao
import me.ash.reader.domain.repository.ArticleDao
import me.ash.reader.domain.repository.FeedDao
import me.ash.reader.domain.repository.GroupDao
import me.ash.reader.infrastructure.android.NotificationHelper
import me.ash.reader.infrastructure.preference.KeepArchivedPreference
import me.ash.reader.infrastructure.preference.SyncIntervalPreference
import me.ash.reader.infrastructure.rss.RssHelper
import me.ash.reader.infrastructure.android.NotificationHelper
import me.ash.reader.ui.ext.currentAccountId
import me.ash.reader.ui.ext.spacerDollar
import java.util.*
Expand Down Expand Up @@ -146,17 +146,9 @@ abstract class AbstractRssRepository(
private suspend fun syncFeed(feed: Feed): FeedWithArticle {
val latest = articleDao.queryLatestByFeedId(context.currentAccountId, feed.id)
val articles = rssHelper.queryRssXml(feed, latest?.link)
// try {
// if (feed.icon == null && !articles.isNullOrEmpty()) {
// rssHelper.queryRssIcon(feedDao, feed, articles.first().link)
// }
// } catch (e: Exception) {
// Log.e("RLog", "queryRssIcon[${feed.name}]: ${e.message}")
// return FeedWithArticle(
// feed = feed.apply { isNotification = false },
// articles = listOf()
// )
// }
if (feed.icon == null) {
rssHelper.queryRssIcon(feedDao, feed)
}
return FeedWithArticle(
feed = feed.apply { isNotification = feed.isNotification && articles.isNotEmpty() },
articles = articles
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/me/ash/reader/infrastructure/rss/Favicon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.ash.reader.infrastructure.rss

data class Favicon(
val url: String? = null,
val icons: List<Icon>? = null
) {
data class Icon(
val url: String?,
val width: Int?,
val height: Int?,
val format: String?,
val bytes: Long?,
val error: String?,
val sha1sum: String?,
)
}
31 changes: 6 additions & 25 deletions app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.ash.reader.infrastructure.rss
import android.content.Context
import android.text.Html
import android.util.Log
import com.google.gson.Gson
import com.rometools.rome.feed.synd.SyndEntry
import com.rometools.rome.io.SyndFeedInput
import com.rometools.rome.io.XmlReader
Expand Down Expand Up @@ -143,34 +144,14 @@ class RssHelper @Inject constructor(
suspend fun queryRssIcon(
feedDao: FeedDao,
feed: Feed,
articleLink: String,
) {
withContext(ioDispatcher) {
val domainRegex = Regex("(http|https)://(www.)?(\\w+(\\.)?)+")
val request = response(okHttpClient, articleLink)
val request = response(okHttpClient, "https://besticon-demo.herokuapp.com/allicons.json?url=${feed.url}")
val content = request.body.string()
val regex = Regex("""<link(.+?)rel="shortcut icon"(.+?)href="(.+?)"""")
var iconLink = regex
.find(content)
?.groups?.get(3)
?.value
Log.i("rlog", "queryRssIcon: $iconLink")
if (iconLink != null) {
if (iconLink.startsWith("//")) {
iconLink = "http:$iconLink"
}
if (iconLink.startsWith("/")) {
iconLink = "${domainRegex.find(articleLink)?.value}$iconLink"
}
saveRssIcon(feedDao, feed, iconLink)
} else {
domainRegex.find(articleLink)?.value?.let {
Log.i("RLog", "favicon: ${it}")
if (response(okHttpClient, "$it/favicon.ico").isSuccessful) {
saveRssIcon(feedDao, feed, it)
}
}
}
val favicon = Gson().fromJson(content, Favicon::class.java)
favicon?.icons?.first { it.width != null && it.width >= 20 }?.url?.let {
saveRssIcon(feedDao, feed, it)
}?: return@withContext
}
}

Expand Down
64 changes: 38 additions & 26 deletions app/src/main/java/me/ash/reader/ui/component/FeedIcon.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
package me.ash.reader.ui.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import me.ash.reader.ui.component.base.RYAsyncImage

@Composable
fun FeedIcon(
feedName: String,
iconUrl: String?,
size: Dp = 20.dp,
) {
Row(
modifier = Modifier
.size(20.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.outline.copy(alpha = 0.2f))
) {}
if (iconUrl == null) {
Box(
modifier = Modifier
.size(20.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary),
contentAlignment = Alignment.Center,
) {
Text(
text = feedName.ifEmpty { " " }.first().toString(),
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onPrimary,
fontSize = 10.sp,
)
}
} else {
RYAsyncImage(
modifier = Modifier
.size(size)
.clip(CircleShape),
contentDescription = feedName,
data = iconUrl,
placeholder = null,
)
}
}

// val url by remember {
// mutableStateOf(
// "https://ui-avatars.com/api/?length=1&background=random&name=${
// URLEncoder.encode(
// feedName,
// Charsets.UTF_8.toString()
// )
// }"
// )
// }
//
// AsyncImage(
// modifier = Modifier
// .size(size)
// .clip(CircleShape),
// contentDescription = feedName,
// data = url,
// placeholder = null,
// )
@Preview
@Composable
fun FeedIconPrev(){
FeedIcon("AFF", null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun FeedItem(
verticalAlignment = Alignment.CenterVertically,
) {
Row(modifier = Modifier.weight(1f)) {
FeedIcon(feed.name)
FeedIcon(feed.name, feed.icon)
Text(
modifier = Modifier.padding(start = 12.dp, end = 6.dp),
text = feed.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun FeedOptionDrawer(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
FeedIcon(feedName = feed?.name ?: "", size = 24.dp)
FeedIcon(feedName = feed?.name ?: "", iconUrl = feed?.icon, size = 24.dp)
// Icon(
// modifier = Modifier.roundClick { },
// imageVector = Icons.Rounded.RssFeed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun ArticleItem(
) {
// Feed icon
if (articleListFeedIcon.value) {
FeedIcon(articleWithFeed.feed.name)
FeedIcon(articleWithFeed.feed.name, iconUrl = articleWithFeed.feed.icon)
Spacer(modifier = Modifier.width(10.dp))
}

Expand Down

0 comments on commit f5c6e5f

Please sign in to comment.