pull/34/head
kunfei 5 years ago
parent 8171c938a0
commit bf77172bbc
  1. 137
      app/src/main/java/io/legado/app/model/rss/RssParser.kt

@ -33,99 +33,62 @@ object RssParser {
// Start parsing the item // Start parsing the item
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
when { when {
xmlPullParser.name.equals( xmlPullParser.name
RSSKeywords.RSS_ITEM, .equals(RSSKeywords.RSS_ITEM, true) -> insideItem = true
ignoreCase = true xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TITLE, true) ->
) -> insideItem = true if (insideItem) currentArticle.title = xmlPullParser.nextText().trim()
xmlPullParser.name.equals( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_LINK, true) ->
RSSKeywords.RSS_ITEM_TITLE, if (insideItem) currentArticle.link = xmlPullParser.nextText().trim()
ignoreCase = true xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_AUTHOR, true) ->
) -> if (insideItem) { if (insideItem) currentArticle.author = xmlPullParser.nextText().trim()
currentArticle.title = xmlPullParser.nextText().trim() xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CATEGORY, true) ->
} if (insideItem) currentArticle.categories.add(xmlPullParser.nextText().trim())
xmlPullParser.name.equals( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_THUMBNAIL, true) ->
RSSKeywords.RSS_ITEM_LINK, if (insideItem) currentArticle.image =
ignoreCase = true
) -> if (insideItem) {
currentArticle.link = xmlPullParser.nextText().trim()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_AUTHOR,
ignoreCase = true
) -> if (insideItem) {
currentArticle.author = xmlPullParser.nextText().trim()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_CATEGORY,
ignoreCase = true
) -> if (insideItem) {
currentArticle.categories.add(xmlPullParser.nextText().trim())
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_THUMBNAIL,
ignoreCase = true
) -> if (insideItem) {
currentArticle.image =
xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL) xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_ENCLOSURE, true) ->
xmlPullParser.name.equals( if (insideItem) {
RSSKeywords.RSS_ITEM_ENCLOSURE, val type =
ignoreCase = true xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE)
) -> if (insideItem) { if (type != null && type.contains("image/")) {
val type = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE) currentArticle.image =
if (type != null && type.contains("image/")) { xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
currentArticle.image = }
xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} }
} xmlPullParser.name
xmlPullParser.name.equals( .equals(RSSKeywords.RSS_ITEM_DESCRIPTION, true) ->
RSSKeywords.RSS_ITEM_DESCRIPTION, if (insideItem) {
ignoreCase = true val description = xmlPullParser.nextText()
) -> if (insideItem) { currentArticle.description = description.trim()
val description = xmlPullParser.nextText() if (currentArticle.image == null) {
currentArticle.description = description.trim() currentArticle.image = getImageUrl(description)
if (currentArticle.image == null) { }
currentArticle.image = getImageUrl(description)
} }
} xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CONTENT, true) ->
xmlPullParser.name.equals( if (insideItem) {
RSSKeywords.RSS_ITEM_CONTENT, val content = xmlPullParser.nextText().trim()
ignoreCase = true currentArticle.content = content
) -> if (insideItem) { if (currentArticle.image == null) {
val content = xmlPullParser.nextText().trim() currentArticle.image = getImageUrl(content)
currentArticle.content = content }
if (currentArticle.image == null) {
currentArticle.image = getImageUrl(content)
} }
} xmlPullParser.name
xmlPullParser.name.equals( .equals(RSSKeywords.RSS_ITEM_PUB_DATE, true) ->
RSSKeywords.RSS_ITEM_PUB_DATE, if (insideItem) {
ignoreCase = true val nextTokenType = xmlPullParser.next()
) -> if (insideItem) { if (nextTokenType == XmlPullParser.TEXT) {
val nextTokenType = xmlPullParser.next() currentArticle.pubDate = xmlPullParser.text.trim()
if (nextTokenType == XmlPullParser.TEXT) { }
currentArticle.pubDate = xmlPullParser.text.trim() // Skip to be able to find date inside 'tag' tag
continue@loop
} }
// Skip to be able to find date inside 'tag' tag xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TIME, true) ->
continue@loop if (insideItem) currentArticle.pubDate = xmlPullParser.nextText()
} xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_GUID, true) ->
xmlPullParser.name.equals( if (insideItem) currentArticle.guid = xmlPullParser.nextText().trim()
RSSKeywords.RSS_ITEM_TIME,
ignoreCase = true
) -> if (insideItem) {
currentArticle.pubDate = xmlPullParser.nextText()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_GUID,
ignoreCase = true
) -> if (insideItem) {
currentArticle.guid = xmlPullParser.nextText().trim()
}
} }
} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals( } else if (eventType == XmlPullParser.END_TAG
"item", && xmlPullParser.name.equals("item", true)
ignoreCase = true
)
) { ) {
// The item is correctly parsed // The item is correctly parsed
insideItem = false insideItem = false

Loading…
Cancel
Save