Skip to content
This repository has been archived by the owner on Feb 22, 2021. It is now read-only.

Improve parsing of enclosure and media namespace in feeds #751

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 35 additions & 3 deletions app/src/main/java/net/frju/flym/data/entities/Entry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import androidx.room.PrimaryKey
import com.rometools.rome.feed.synd.SyndEntry
import kotlinx.android.parcel.Parcelize
import net.fred.feedex.R
import net.frju.flym.utils.HtmlUtils
import net.frju.flym.utils.sha1
import java.util.Date
import java.util.UUID
import org.jdom2.Element
import java.util.*


@Parcelize
Expand Down Expand Up @@ -75,8 +76,39 @@ fun SyndEntry.toDbFormat(context: Context, feed: Feed): Entry {
item.title = context.getString(R.string.entry_default_title)
}
item.description = contents.getOrNull(0)?.value ?: description?.value

if (item.description == null) {
foreignMarkup?.forEach {
if (it.namespace?.prefix == "media" && it.name == "group") {
it.children.forEach { mc ->
if (mc.name == "description") item.description = mc.value
if (mc.name == "thumbnail") {
mc.attributes.forEach { tb ->
if (tb.name == "url") item.imageLink = tb.value
}
}
}
}
}
}

item.link = link
//TODO item.imageLink = null

enclosures?.forEach {
if ((it.type != null && it.type.contains("image")) || HtmlUtils.isImageInUrl(it.url)) {
item.imageLink = it.url
}
}
if (item.imageLink == null) {
foreignMarkup?.forEach {
if (it.namespace?.prefix == "media" && (it.name == "thumbnail" || it.name == "content")) {
it.attributes.forEach { mc ->
if (mc.name == "url" && HtmlUtils.isImageInUrl(mc.value)) item.imageLink = mc.value
}
}
}
}

item.author = author

val date = publishedDate ?: updatedDate
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/net/frju/flym/utils/HtmlUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ object HtmlUtils {
private val END_BR_PATTERN = Pattern.compile("(\\s*<br\\s*[/]*>\\s*)*$", Pattern.CASE_INSENSITIVE)
private val MULTIPLE_BR_PATTERN = Pattern.compile("(\\s*<br\\s*[/]*>\\s*){3,}", Pattern.CASE_INSENSITIVE)
private val EMPTY_LINK_PATTERN = Pattern.compile("<a\\s+[^>]*></a>", Pattern.CASE_INSENSITIVE)
private val URL_WITH_IMAGE_PATTERN = Pattern.compile("(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\\.(?:jpg|gif|png))(?:\\?([^#]*))?(?:#(.*))?", Pattern.CASE_INSENSITIVE)

fun improveHtmlContent(content: String, baseUri: String): String {
@Suppress("NAME_SHADOWING")
Expand Down Expand Up @@ -160,4 +161,8 @@ object HtmlUtils {

return false
}

fun isImageInUrl(url:String) : Boolean {
return URL_WITH_IMAGE_PATTERN.matcher(url).matches()
}
}